HEAPS • Heaps • Properties of Heaps • HeapSort

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Heaps © 2004 Goodrich, Tamassia Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of.
Advertisements

COL 106 Shweta Agrawal and Amit Kumar
Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
Problem of the Day  To get into club, must get past bouncer  Recorded correct responses of those before you  Guard said five & response was four  Guard.
Data Structures Lecture 7 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
The Priority Queue Abstract Data Type. Heaps. Adaptable Priority Queue. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich,
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
© 2004 Goodrich, Tamassia Priority Queues1 Heaps: Tree-based Implementation of a Priority Queue.
© 2004 Goodrich, Tamassia Heaps © 2004 Goodrich, Tamassia Heaps2 Priority Queue Sorting (§ 8.1.4) We can use a priority queue to sort a set.
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
Priority Queues1 Part-D1 Priority Queues. Priority Queues2 Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is.
1 Priority Queues CPS212 Gordon College VIP. 2 Introduction to STL Priority Queues Adaptor container - underlying container may be either: – a template.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
Heaps and Priority Queues Priority Queue ADT (§ 2.4.1) A priority queue stores a collection of items An item is a pair (key, element) Main.
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
Binary Heap.
Chapter 21 Binary Heap.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
CSC 213 – Large Scale Programming Lecture 15: Heap-based Priority Queue.
PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24,
1 Heaps A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient.
Priority Queues and Heaps. Outline and Reading PriorityQueue ADT (§8.1) Total order relation (§8.1.1) Comparator ADT (§8.1.2) Sorting with a Priority.
Chapter 2.4: Priority Queues and Heaps PriorityQueue ADT (§2.4.1) Total order relation (§2.4.1) Comparator ADT (§2.4.1) Sorting with a priority queue (§2.4.2)
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
CPSC 252 Binary Heaps Page 1 Binary Heaps A complete binary tree is a binary tree that satisfies the following properties: - every level, except possibly.
CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010.
Lecture 15 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Heaps & Priority Queues
CS 2468: Assignment 2 (Due Week 9, Tuesday. Drop a hard copy in Mail Box 75 or hand in during the lecture) Use array representation (double a[]) to implement.
1 Heaps A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient.
Problem of the Day  You are trapped alone in a dark room with:  Candle;  Wood stove; and  Gas lamp (with full tank).  You only have one match; what.
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
CSC 213 – Large Scale Programming. Priority Queue ADT  Prioritizes Entry s using their keys  For Entry s with equal priorities, order not specified.
1 Heaps II Implementation HeapSort Bottom-Up Heap Construction Locators.
Heaps, Heap Sort, and Priority Queues. Background: Binary Trees * Has a root at the topmost level * Each node has zero, one or two children * A node that.
Lecture on Data Structures(Trees). Prepared by, Jesmin Akhter, Lecturer, IIT,JU 2 Properties of Heaps ◈ Heaps are binary trees that are ordered.
Heaps and Priority Queues What is a heap? A heap is a binary tree storing keys at its internal nodes and satisfying the following properties:
"Teachers open the door, but you must enter by yourself. "
Heaps (8.3) CSE 2011 Winter May 2018.
Priority Queues and Heaps
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
Heaps 8/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
CSCE 3100 Data Structures and Algorithm Analysis
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Heaps and Priority Queues
Priority Queues and Heaps
Part-D1 Priority Queues
Heaps and Priority Queues
Heaps 11/27/ :05 PM Heaps Heaps.
Tree Representation Heap.
Heaps A heap is a binary tree.
Heaps and Priority Queues
CSCE 3110 Data Structures and Algorithm Analysis
© 2013 Goodrich, Tamassia, Goldwasser
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
Ch. 8 Priority Queues And Heaps
Heaps and Priority Queues
"Teachers open the door, but you must enter by yourself. "
Heaps II Implementation HeapSort Bottom-Up Heap Construction Locators.
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
CSCE 3110 Data Structures and Algorithm Analysis
Heaps and Priority Queues
CSCE 3110 Data Structures and Algorithm Analysis
CS210- Lecture 14 July 5, 2005 Agenda Inserting into Heap
The Heap ADT A heap is a complete binary tree where each node’s datum is greater than or equal to the data of all of the nodes in the left and right.
Heaps 9/29/2019 5:43 PM Heaps Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

HEAPS • Heaps • Properties of Heaps • HeapSort • Bottom-Up Heap Construction • Locators

Heaps •A Heap is a Binary Tree H that stores a collection of keys at its internal nodes and that satisfies two additional properties: -1)Relational Property -2)Structural Property

Heap Properties Heap-Order Property (Relational): In a heap H, for every node v (except the root), the key stored in v is greater than or equal to the key stored in v’s parent. A structure fulfilling the Heap-Order Property A structure which fails to fulfill the Heap-Order Property

Heap Properties • Complete Binary Tree (Structural): A Binary Tree T is complete if each level but the last is full, and, in the last level, all of the internal nodes are to the left of the external nodes. • A structure fulfilling the Structural Property • A structure which fails to fulfill the Structural Property

Heaps (contd.)

Height of a Heap • Proposition: A heap H storing n keys has height h = [ log(n+1)ù ( ceiling of n+1 ) • Justification: Due to H being complete, we know: -# i of internal nodes is at least : 1 + 2 + 4 + ... 2 h-2 + 1 = 2 h-1 -1 + 1 = 2 h-1 -# i of internal nodes is at most: 1 + 2 + 4 + ... 2 h-1 = 2 h - 1 - Therefore: 2 h-1 £ n and n £ 2 h - 1 - Which implies that: log(n + 1) £ h £ log n + 1 - Which in turn implies: h = | log(n+1)ù - Q.E.D.

Height of a Heap (contd.) • Let’s look at that graphically: • Consider this heap which has height h = 4 and n =13 • Suppose two more nodes are added. To maintain completeness of the tree, the two external nodes in level 4 will become internal nodes: i.e. n = 15, h = 4 = log (15+1) • Add one more: n = 16, h = 5= ceiling {log(16+1)}

Heap Insertion So here we go ... The key to insert is “6”

Heap Insertion Add the key in the next available spot in the heap.

Upheap • Upheap checks if the new node is smaller than its parent. If so, it switches the two • Upheap continues up the tree

Removal From a Heap: RemoveMinElement() • The removal of the top key leaves a hole • We need to fix the heap • First, replace the hole with the last key in the heap • Then, begin Downheap

Downheap Downheap compares the parent with the smallest child. If the child is smaller, it switches the two.

Downheap Continues

Downheap Continues

End of Downheap • Downheap terminates when the key is greater than the keys of both its children or the bottom of the heap is reached. • (total #switches) < (height of tree - 1) =log N

Implementation of a Heap public class HeapSimplePriorityQueue implements SimplePriorityQueue { BinaryTree T; Position last; Comparator comparator; ... }

Implementation of a Heap(cont.) • Two ways to find the insertion position z in a heap:

Heap Sort • All heap methods run in logarithmic time or better • If we implement PriorityQueueSort using a heap for our priority queue, insertItem and removeMinElement each take O(logk), k being the number of elements in the heap at a given time. • We always have n or less elements in the heap, so the worst case time complexity of these methods is O(logn). • Thus each phase takes O(nlogn) time, so the algorithm runs in O(nlogn) time also. • This sort is known as heap-sort. • The O(nlogn) run time of heap-sort is much better than the O(n 2 ) run time of selection and insertion sort.

Bottom-Up Heap Construction • If all the keys to be stored are given in advance we can build a heap bottom-up in O(n) time. • Note: for simplicity, we describe bottom-up heap construction for the case for n keys where: n = 2 h –1 h being the height. • Steps: 1) Construct (n+1)/2 elementary heaps with one key each. 2) Construct (n+1)/4 heaps, each with 3 keys, by joining pairs of elementary heaps and adding a new key as the root. The new key may be swapped with a child in order to perserve heap- order property. 3) Construct (n+1)/8 heaps, each with 7 keys, by joining pairs of 3-key heaps and adding a new key. Again swaps may occur. ... 4) In the ith step, 2< i < h, we form (n+1)/2 i heaps, each storing 2 i -1 keys, by joining pairs of heaps storing (2 i-1 -1) keys. Swaps may occur.

Bottom-Up Heap Construction (cont.)

Bottom-Up Heap Construction (cont.)

Bottom-Up Heap Construction (cont.)

Bottom-Up Heap Construction (cont.)

Analysis of Bottom-Up Heap Construction • Proposition: Bottom-up heap construction with n keys takes O(n) time. - Insert (n + 1)/2 nodes - Insert (n + 1)/4 nodes - Upheap at most (n + 1)/4 nodes 1 level. - Insert (n + 1)/8 nodes - ... - Insert 1 node. - Upheap at most 1 node 1 level.

Locators • Locators can be used to keep track of elements as they are moved around inside a positional container. • A Locator sticks with a specific element, even if that element changes positions in the container. • The Locator ADT supports the following fundamental methods: element(): Return the element of the item associated with the Locator. key(): Return the key of the item associated with the Locator. isContained(): Return true if and only if the Locator is associated with a container. container(): Return the container associated with the Locator.

Locators and Positions • At this point, you may be wondering what the difference is between locators and positions, and why we need to distinguish between them. • It’s true that they have very similar methods • The difference is in their primary usage • Positions are used primarily to represent the internal form of a data structure • Locators are used primarily to keep track of data, regardless of what position it’s in (and even if it’s not in any position right now)

Locators and Positions: Example • For example, consider the cs16 Valet Parking Service (started by the teaching assistants because they had too much free time on their hands). • When they began their business, Mike and Ryan decided to create a data structure to keep track of where exactly the cars were. • Ryan suggested having Positions represent what parking space (or part of the green) the car was in. • However, Mike realized that the car-owners coming into the CIT to reclaim their car wouldn’t remember what space the TAs had placed their car in. And the TA’s might want to move cars from space to space. • So he suggested using the UTAs as Locators. Each UTA would be able to get a car upon demand (when element() was called on them) and would know to come running when the car’s owner came by. (the owner would be their key) • And it was done, and the staff were able to dispense with the last remainders of their free time.

Priority Queue with Locators • It is easy to extend the sequence-based and heap-based implementations of a Priority Queue to support Locators. • A Locator holds the key, element pair. • The Priority Queue ADT can be extended to implement the Locator ADT • A Locator in this implementation must have a instance variable pointing to its Position. • All of the methods of the Locator ADT can then be implemented in O(1) time.