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