Heaps 1 Background Priority Queues z Queues are
Heaps 1
Background: Priority Queues. z. Queues are a First-In, First-Out data structure; z. Priority Queues are similar, except those of highest priority must be removed first. z. How should we implement this? 2
Priority Queue Implementation z. Arrays y. Inserts: O(logn) to find spot; O(n) to move all others down. y. Deletes: O(1) to remove; O(n) to move others. z. Linked Lists : y. Inserts: O(n) to find spot; O(1) to do insert. y. Deletes: O(1) to remove. 3
Priority Queue Implementation z. AVL-Trees y. Inserts: O(logn) to find spot; O(logn) to do rotation(s). y. Deletes: O(logn) to remove; O(logn) to do rotations. z. Heaps : y. Inserts: O(1) to find spot; O(logn) to do insert. y. Deletes: O(1) to find; O(logn) to delete/rotate. 4
Heaps Vs. AVL-Trees z. AVL-Trees have a lot of overhead to balance. z. AVL-Trees store the values in order. z. Heaps only provide access to the least/greatest. z. This requires less overhead and is easier to implement. 5
What is a Heap? z. BINARY MIN HEAP is a complete binary tree in which each node is empty or has a key value that is less than both of its children. Both children are Binary Min Heaps z. A COMPLETE BINARY TREE is a binary tree where the root is either empty OR TL is a perfect tree of height h-1 and TR is a complete tree of height h-1 OR TL is a complete tree of height h-1 and TR is a perfect tree of height h-2 6
More Definitions z. A PERFECT TREE is one where the root is either empty or has HL – HR = 0 and both sub trees are also perfect trees. zi. e. the last level (and all levels) are completely filled. 7
Heap as a Tree z. As a tree, inserts would fill a level entirely, left to right before going on to the next level: A B D C E F G 8
Heap Insert Algorithm z. The previous slide discussed where the new node would go, but how to maintain the order of the heap? z. Algorithm: y. Compare new node to parent; if the new node is greater than the parent, stop. y. Else, swap new node and parent. Repeat algorithm at parent (percolate up). 9
Heap Example z. Now let’s do an extended example, inserting a series of priority values into a heap. z. We will be inserting 90, 50, 25, 10, 15, 20, 75. z. Let’s start with 90: 10
Heap Example 90 Left to insert: 50, 25, 10, 15, 20, 75 z 90 is no problem. z. Now, insert 50: 11
Heap Example 90 50 Left to insert: 25, 10, 15, 20, 75 z. Does this require a swap? z. Yes: 12
Heap Example 50 90 Left to insert: 25, 10, 15, 20, 75 z. Done. Now insert 25: 13
Heap Example 50 90 25 Left to insert: 10, 15, 20, 75 z. Swap? z. Yes: 14
Heap Example 25 90 50 Left to insert: 10, 15, 20, 75 z. Now insert 10: 15
Heap Example 25 90 50 10 Left to insert: 15, 20, 75 z. Swap? z. Yes: 16
Heap Example 25 10 50 90 Left to insert: 15, 20, 75 z. Swap again? z. Yes: 17
Heap Example 10 25 50 90 Left to insert: 15, 20, 75 z. Done. Now insert 15: 18
Heap Example 10 25 90 50 15 Left to insert: 20, 75 z. Swap? z. Yes: 19
Heap Example 10 15 90 50 25 Left to insert: 20, 75 z. Swap again? z. No. 15 is where it needs to be. Now 20: 20
Heap Example 10 15 90 50 25 20 Left to insert: 75 z. Swap? z. Yes: 21
Heap Example 10 15 90 20 25 50 Left to insert: 75 z. Swap again? z. No. 20 is good. Finally, insert 75: 22
Heap Example 10 15 90 20 25 50 75 Left to insert: done z. Swap? z. No. 75 is good; we are done. 23
Deleting from a Heap z. The minimum value is at the root of the tree; thus, it can be found easily, but how do we rebalance? z. Algorithm: y. Remove Root’s value as minimum value y. Replace root with last node; delete last. y. If root is less than both its children, stop. y. Else, swap root and min child; repeat from min child node (percolate down). 24
Heap Delete Example 10 15 90 20 25 50 75 z. First, 10 must be removed and replaced with 75: 25
Heap Delete Example 75 15 90 20 25 50 z. Is 75 less than both of its children? z. No. Swap with minimum (15): 26
Heap Delete Example 15 75 90 20 25 50 z. Is 75 less than both of its children? z. No. Swap with minimum (25): 27
Heap Delete Example 15 25 90 20 75 50 z 75 is at the terminal level; done. 28
Implementation Problem z. The problem with the tree/pointer representation is how to maintain the “next” pointer. z. I think this can be done by keeping a counter and manipulating it to decide left or right from the root. z. But there is another way: 29
Array Implementation z. Any binary tree may be represented in an array. z. The only questions really are y. Where are the two children of a node? y. Where is a node’s parent? z. Answer (for a node at position i): y. The two children are at 2*i and 2*i+1 y. The parent is at i div 2. 30
Array Example: z. For this tree: 10 15 90 20 25 50 75 z. Here’s the array: 1 2 3 4 5 6 7 10 15 20 90 25 50 75 31
The End Slide z 32
- Slides: 32