Heaps and Priority Queues James Moscola Department of Engineering & Computer Science York College of Pennsylvania James Moscola
Priority Queue An abstract data type of a queue that associates a priority with each of the elements inserted Elements are enqueued with some priority Elements are dequeued in priority order - Highest priority elements are dequeued first 2
Binary Heaps Great for an implementation of a priority queue Similar in structure to a binary search tree Sorting of elements in a heap is much weaker than in a BST, however it is sufficient to implement a priority queue A binary heap can be either a Min Heap or a Max Heap - Min Heap - smallest key values have highest priority - Max Heap - largest key values have highest priority 3
Complete Binary Tree A heap is implemented as a complete binary tree which can be stored efficiently in an array A complete binary tree has the following properties: - Tree is filled in level-order from left to right - Tree has the maximum number of nodes at every level except for possibly the bottom level - There are no holes allowed in the tree 4
Complete Binary Tree A D B E F C G This IS a complete binary tree H I J K L A B C D E F G This IS NOT a complete binary tree H I J K L N O 5
Complete Binary Tree Using a complete binary tree to represent a heap makes traversing the heap easy The complete binary tree can easily be stored in an array A B C D E F G H I J K L left child = 2 * i right child = 2 * i + 1 A B C D E F G H I J K L 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 parent = i / 2 6
Binary Heap Properties Just as with all other data structures, there are properties that must be maintained for the binary heap Elements in the heap must maintain the heap-order property - Heap-order property (for a Min Heap): In a heap, for every node X with parent P, the key in P is less than or equal to the key in X (i.e. the parent s key is less than a child s key) P X P X 7
Binary Heap Operations: insert The basic idea for insert: - Create a hole (an empty node) in the next available complete tree location - If the new element can be inserted into the hole without violating the heap-order property, then insert the new element into the hole - If inserting the new element into the hole will violate the heap-order property, then move the hole up the heap until a location is found where the new element can be inserted without violating the heaporder property This operation is called percolateup 8
Binary Heap Operations: percolateup Select the node that is to be percolated up the heap Compare the node with its parent If the node is less than it s parent, then swap the node with its parent Compare the node to its new parent Continue moving the node up the tree until the node is greater than or equal to its parent node (i.e. it s parent is smaller) 9
Binary Heap Operations: insert Example Insert node 14 First create a hole in the next available heap location 13 21 16 24 31 19 68 65 26 32 Can 14 be inserted into new hole without violating heap the property? 10
Binary Heap Operations: insert Example Insert node 14 Swap the hole with its parent to move it up the heap 13 21 16 24 19 68 65 26 32 31 Can 14 be inserted into this location without violating heap the property? 11
Binary Heap Operations: insert Example Insert node 14 Continually swap the hole with its parent to move the hole up the heap until a valid location is found to insert the new node 13 This is typically referred to as percolateup 16 24 21 19 68 65 26 32 31 Can 14 be inserted into this location without violating heap the property? 12
Binary Heap Operations: insert Example Insert node 14 Done with insertion 13 14 16 24 21 19 68 65 26 32 31 Insert node 14 into this location Done with insertion since (14 > 13) 13
Binary Heap Operations: insert Time required to do insertion could be as much as O(log N) if the value getting inserting is the new minimum value in the heap - A newly inserted value that is the minimum value must percolate all the way up the tree 14
Binary Heap Operations: deletemin The basic idea for deletemin: - Delete the root node of the heap (will be the minimum node) - Create a hole in the location where the root node was... this hole will eventually be filled with the last node in the heap (the rightmost node on the bottom level) - Move the hole down the tree until a location is found that permits the last node in the heap to get moved while still maintaining the heaporder property This operation is called percolatedown 15
Binary Heap Operations: percolatedown Select the node that is to be percolated down the heap Compare the node the lesser of its two children If the node is greater than the lesser of its two children, then swap the node with that child Compare the node to its new children Continue moving the node down the tree until the node is less than or equal to both of its children 16
Binary Heap Operations: deletemin Example Delete the minimum node The minimum node is node 13 13 In a minheap, the minimum node will always be at the top of the heap 14 16 19 21 19 68 65 26 32 31 17
Binary Heap Operations: deletemin Example Delete the minimum node A hole is created where the minimum value was removed The size of the heap decreases After node 13 is removed, a hole is created in its place 14 16 19 21 19 68 65 26 32 31 Because the size of the heap is decremented after a node is deleted, the last node in the heap is removed. Must find a new location to store the value that was in the last node 18
Binary Heap Operations: deletemin Example Delete the minimum node Swap the hole with the smaller of its children to move it down the heap Move the hole down the tree 14 Node 14 < Node 16 16 19 21 19 68 65 26 32 31 Can the 31 we re trying to place be moved to this location without violating the heap property? 19
Binary Heap Operations: deletemin Example Delete the minimum node Continually swap the hole with its smaller child to move the hole down the heap until a valid location is found to place the last value 14 This is typically referred to as percolatedown 19 16 21 19 68 65 26 32 31 Can the 31 we re trying to place be moved to this location without violating the heap property? 20
Binary Heap Operations: deletemin Example Delete the minimum node Continually swap the hole with its smaller child to move the hole down the heap until a valid location is found to place the last value 14 This is typically referred to as percolatedown 19 16 26 21 19 68 65 32 31 Can the 31 we re trying to place be moved to this location without violating the heap property? 21
Binary Heap Operations: deletemin Example Delete the minimum node Once a location is found for the 31, fill the hole with that value 14 19 16 26 21 19 68 65 31 32 Fill the hole with the value that was last in the heap prior to the deletemin operation 22
Binary Heap Operations: buildheap The buildheap operation takes a tree that is not in heap-order and puts it into heap-order Idea: - Call percolatedown on all non-leaf nodes in reverse level-order No need to call percolatedown on leaf nodes since they cannot be moved down The first non-leaf node is located in the array index currentsize/2 Easily implemented by iteratively visiting each node in the heap array in reverse order starting at the first non-leaf node 23
Binary Heap Operations: buildheap Example Fixing the heap order No need to call percolatedown on the leaf nodes 1 92 2 3 47 21 4 5 6 7 20 12 45 63 8 9 10 11 12 13 14 15 61 17 55 37 25 64 83 73 24
Binary Heap Operations: buildheap Example Heap size is 15, so first non-leaf node is 15/2 = 7 Processing node at array index 7 1 92 2 3 47 21 Compare with lesser of children, no swap needed 4 5 6 7 20 12 45 63 8 9 10 11 12 13 14 15 61 17 55 37 25 64 83 73 25
Binary Heap Operations: buildheap Example Processing node at array index 6 1 92 2 3 47 21 4 5 6 7 20 12 45 63 8 9 10 11 12 13 14 15 61 17 55 37 25 64 83 73 Compare with lesser of children, need to swap 26
Binary Heap Operations: buildheap Example 1 92 2 3 47 21 4 5 6 7 20 12 25 63 8 9 10 11 12 13 14 15 61 17 55 37 45 64 83 73 Swapped with child 27
Binary Heap Operations: buildheap Example Processing node at array index 5 1 92 2 3 47 21 4 5 6 7 20 12 25 63 8 9 10 11 12 13 14 15 61 17 55 37 45 64 83 73 Compare with lesser of children, no swap needed 28
Binary Heap Operations: buildheap Example Processing node at array index 4 1 92 2 3 47 21 4 5 6 7 20 12 25 63 8 9 10 11 12 13 14 15 61 17 55 37 45 64 83 73 Compare with lesser of children, need to swap 29
Binary Heap Operations: buildheap Example 1 92 2 3 47 21 4 5 6 7 17 12 25 63 8 9 10 11 12 13 14 15 61 20 55 37 45 64 83 73 Swapped with child 30
Binary Heap Operations: buildheap Example Processing node at array index 3 1 92 2 3 47 21 Compare with lesser of children, no swap needed 4 5 6 7 17 12 25 63 8 9 10 11 12 13 14 15 61 20 55 37 45 64 83 73 31
Binary Heap Operations: buildheap Example Processing node at array index 2 Compare with lesser of children, need to swap 17 12 1 92 2 3 47 21 4 5 6 7 8 9 10 11 12 13 14 15 25 63 61 20 55 37 45 64 83 73 32
Binary Heap Operations: buildheap Example 1 92 2 3 12 21 Swapped with child 4 5 6 7 17 47 25 63 8 9 10 11 12 13 14 15 61 20 55 37 45 64 83 73 33
Binary Heap Operations: buildheap Example 1 92 2 3 12 21 4 5 6 7 17 47 25 63 8 9 10 11 12 13 14 15 61 20 55 37 45 64 83 73 Continue to compare with lesser of children, Need another swap 34
Binary Heap Operations: buildheap Example 1 92 2 3 12 21 4 5 6 7 17 37 25 63 8 9 10 11 12 13 14 15 61 20 55 47 45 64 83 73 Swapped with child 35
Binary Heap Operations: buildheap Example Processing node at array index 1 1 92 Compare with lesser of children, need to swap 2 3 12 21 4 5 6 7 17 37 25 63 8 9 10 11 12 13 14 15 61 20 55 47 45 64 83 73 36
Binary Heap Operations: buildheap Example 1 Swapped with child 12 2 3 92 21 4 5 6 7 17 37 25 63 8 9 10 11 12 13 14 15 61 20 55 47 45 64 83 73 37
Binary Heap Operations: buildheap Example Continue to compare with lesser of children, Need another swap 17 37 1 12 2 3 92 21 4 5 6 7 8 9 10 11 12 13 14 15 25 63 61 20 55 47 45 64 83 73 38
Binary Heap Operations: buildheap Example 1 12 Swapped with child 2 3 17 21 4 5 6 7 92 37 25 63 8 9 10 11 12 13 14 15 61 20 55 47 45 64 83 73 39
Binary Heap Operations: buildheap Example 1 12 2 3 17 21 4 5 6 7 92 37 25 63 8 9 10 11 12 13 14 15 61 20 55 47 45 64 83 73 Continue to compare with lesser of children, Need another swap 40
Binary Heap Operations: buildheap Example 1 12 2 3 17 21 4 5 6 7 20 37 25 63 8 9 10 11 12 13 14 15 61 92 55 47 45 64 83 73 Swapped with child 41
Binary Heap Operations: buildheap Example Fixing the heap order Now in heap order 1 12 2 3 17 21 4 5 6 7 20 37 25 63 8 9 10 11 12 13 14 15 61 92 55 47 45 64 83 73 42