BINARY HEAPS 2 cs2420 Introduction to Algorithms and Data Structures Spring 2015 1
adistrivia 2
-assignment 10 is due tonight -assignment 11 is up, due next Thursday 3
assignment 7 scores number of students score 4
let s chat about ethics 5
// djb2 hash function unsigned long hash(unsigned char *str) { unsigned long hash = 5381; int c; while (c = *str++) hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ } return hash; 6
last time 7
complete trees 8
-a complete binary tree has its levels completely filled, with the possible exception of the bottom level -bottom level is filled from left to right -each level has twice as many nodes as the previous level 9
-a N-node complete tree has at most (logn) height -operations are thus at most O(logN) -each level has twice as many nodes as the previous one -how do we use this knowledge to simplify the implementation? 10
complete trees as an array -if we are guaranteed that tree is complete, we can implement it as an array instead of a linked structure -the root goes at index 0, its left child at index 1, its right child at index 2 -for any node at index i, it two children are at index (i*2) + 1 and (i*2) + 2 11
a b c d e f g h i j a b c d e f g h i j index: 0 1 2 3 4 5 6 7 8 9 10 -for example, d s children start at (3*2) + 1 -how do we that f has no children? 12
a b c d e f g h i j a b c d e f g h i j index: 0 1 2 3 4 5 6 7 8 9 10 -any node s parent is at index (i-1) / 2 13
remember the priority queue? 14
-a priority queue is a data structure in which access is limited to the imum item in the set -add -findmin -deletemin -add location is unspecified, so long as the the above is always enforced -what are our options for implementing this? 15
binary heap 16
-a binary heap is a binary tree with two special properties -structure: it is a complete tree -order: the data in any node is less than or equal to the data of its children -this is also called a -heap -a -heap would have the opposite property 17
3 22 16 31 43 17 92 100 56 45 -where is the smallest item? 18
adding to a heap -we must be careful to maintain the two properties when adding to a heap -structure and order -deal with the structure property first where can the new item go to maintain a complete tree? -then, percolate the item upward until the order property is restored -swap upwards until > parent 19
adding 14 put it at the end of the tree 3 22 16 31 43 17 92 100 56 45 20
adding 14 put it at the end of the tree 3 22 16 31 43 17 92 100 56 45 14 21
adding 14 put it at the end of the tree percolate up the tree to fix the order 3 22 16 31 43 17 92 100 56 45 14 22
adding 14 put it at the end of the tree percolate up the tree to fix the order 3 22 16 31 43 17 92 100 56 45 14 23
adding 14 put it at the end of the tree percolate up the tree to fix the order 3 22 16 31 14 17 92 100 56 45 43 24
adding 14 put it at the end of the tree percolate up the tree to fix the order 3 22 16 31 14 17 92 100 56 45 43 25
adding 14 put it at the end of the tree percolate up the tree to fix the order 3 14 16 31 22 17 92 100 56 45 43 26
adding 14 put it at the end of the tree percolate up the tree to fix the order 3 14 16 31 22 17 92 100 56 45 43 27
adding 14 put it at the end of the tree percolate up the tree to fix the order 3 14 16 31 22 17 92 100 56 45 43 28
cost of add -percolate up until smaller than all nodes below it -how many nodes are there on each level (in terms of N)? -about half on the lowest level -about 3/4 in the lowest two levels 29
-if the new item is the smallest in the set, cost is O(logN) -must percolate up every level to the root -complete trees have logn levels -is this the worst, average, or best case? -it has been shown that on average, 1.6 comparisons are needed for any N -thus, add terates early, and average cost is O(1) 30
-you may find conflicting information on the average cost value -some sources may say O(logN) -it depends on the level of the analysis and how tight of a bound we want -don t always believe what you read, test for yourself! 31
remove from a heap -priority queues only support removing the smallest item -in heap this is always what? -remove and return the root -we have a hole at the top, structure property is violated -fill the whole with another item in the tree -which one? -percolate down 32
let s remove the smallest item Take out 3 3 14 16 31 22 17 92 100 56 45 43 33
let s remove the smallest item Take out 3 14 16 31 22 17 92 100 56 45 43 34
let s remove the smallest item Take out 3 fill with last item on last level. why? 14 16 31 22 17 92 100 56 45 43 35
let s remove the smallest item Take out 3 fill with last item on last level. why? 14 16 31 22 17 92 100 56 45 43 36
let s remove the smallest item Take out 3 fill with last item on last level. why? 43 14 16 31 22 17 92 100 56 45 37
let s remove the smallest item Take out 3 fill with last item on last level. why? percolate down 43 14 16 31 22 17 92 100 56 45 38
let s remove the smallest item Take out 3 fill with last item on last level. why? percolate down 43 14 16 31 22 17 92 100 56 45 39
let s remove the smallest item Take out 3 fill with last item on last level. why? percolate down 14 43 16 31 22 17 92 100 56 45 40
let s remove the smallest item Take out 3 fill with last item on last level. why? percolate down 14 43 16 31 22 17 92 100 56 45 41
let s remove the smallest item Take out 3 fill with last item on last level. why? percolate down 14 22 16 31 43 17 92 100 56 45 42
let s remove the smallest item Take out 3 fill with last item on last level. why? percolate down 14 22 16 31 43 17 92 100 56 45 43
let s remove the smallest item Take out 3 fill with last item on last level. why? percolate down 14 22 16 31 43 17 92 100 56 45 44
cost of remove -worst case is O(logN) -percolating down to the bottom level -average case is also O(logN) -rarely terates more than 1-2 levels from the bottom why? 45
back to the priority queue 46
-option 1: a linked list -add: O(1) -findmin: O(N) -deletemin: O(N) (including finding) -option 2: a sorted linked list -add: O(N) -findmin: O(1) -deletemin: O(1) -option 3: a self-balancing BST -add: O(logN) -findmin: O(logN) -deletemin: O(logN) -option 4: a binary heap -add: O(1) (percolate up average of 1.6 swaps) -findmin: O(1) (just access the root) -deletemin: O(logN) (percolate down but rarely terates before bottom) 47
today 48
-- heaps -add -delete -delete -delete algorithm 49
- heaps 50
-a - heap further extends the heap order property -for any node E at even depth, E is the imum element in its subtree -for any node O at odd depth, O is the imum element in its subtree -the root is considered to be at even depth (zero) 51
1 11 14 2 4 3 6 5 8 9 52
1 11 14 2 4 3 6 5 8 9 where is the smallest item? where is the largest item? 53
add 54
-AGAIN, we must ensure the heap property structure -must be a complete tree -add an item to the next open leaf node -THEN, restore order with its parent -does it belong on a level or a level? -swap if necessary -the new location deteres if it is a or node -percolate up the appropriate levels -if new item is a node, percolate up levels -else, percolate up levels 55
want to add 13 1 11 14 2 4 3 6 5 8 9 56
want to add 13 add to first open space 1 11 14 2 4 3 6 5 8 9 13 57
parent on a level, and 13 is greater than parent, so no swap necessary 13 is now a node 1 11 14 2 4 3 6 5 8 9 13 58
percolate up the levels compare to grandparent 1 11 14 2 4 3 6 5 8 9 13 59
percolate up the levels compare to grandparent 1 13 14 2 4 3 6 5 8 9 11 60
percolate up the levels compare to grandparent does 13 have another grandparent? done! 1 13 14 2 4 3 6 5 8 9 11 61
-if the parent is on a level, new node must be greater than the parent -if the parent is on a level, new node must be less than the parent -percolate up like normal, except skip every other level 62
delete 63
- node is one of the two children of the root -replace node with the last leaf node in the tree -preserve structure property! -restore order with the new node s children -if any child is larger, swap -percolate swapped child down the levels -if no child was larger, percolate the new node down the levels -if the node reaches the second to last level of tree, may require one more swap with direct children 64
want to delete the compare children of root 1 22 14 2 4 3 6 5 10 9 7 65
want to delete the compare children of root 22 is 1 22 14 2 4 3 6 5 10 9 7 66
want to delete the compare children of root 22 is 1 14 2 4 3 6 5 10 9 7 67
replace with last leaf node 1 14 2 4 3 6 5 10 9 7 68
replace with last leaf node 1 14 2 4 3 6 5 10 9 7 69
replace with last leaf node 1 7 14 2 4 3 6 5 10 9 70
restore order with children 7 is on a level, so must be > than children 1 7 14 2 4 3 6 5 10 9 71
restore order with children 7 is on a level, so must be > than children no swap required 1 7 14 2 4 3 6 5 10 9 72
7 is now a node percolate down levels 1 7 14 2 4 3 6 5 10 9 73
compare to all 4 grandchildren (or, 3 in this case) is 7 greater than all? 1 7 14 2 4 3 6 5 10 9 74
compare to all 4 grandchildren (or, 3 in this case) is 7 greater than all? swap with largest 1 7 14 2 4 3 6 5 10 9 75
compare to all 4 grandchildren (or, 3 in this case) is 7 greater than all? swap with largest 1 10 14 2 4 3 6 5 7 9 76
does 7 have more grandchildren? 1 10 14 2 4 3 6 5 7 9 77
done! 1 10 14 2 4 3 6 5 7 9 78
-if 7 had been less than one of its DIRECT children we would have swapped them -we would have then percolated that child down the levels instead of 7 -very similar to a regular -heap, except we skip every other level when percolating 79
deleting the 80
-the node is always the root -replace it with last leaf node -restore order with direct children -then, percolate new root down the levels 81
want to delete the 1 22 6 2 4 3 5 5 10 9 7 82
want to delete the 22 6 2 4 3 5 5 10 9 7 83
want to delete the replace with the last leaf node 22 6 2 4 3 5 5 10 9 7 84
want to delete the replace with the last leaf node 22 6 2 4 3 5 5 10 9 7 85
want to delete the replace with the last leaf node 7 22 6 2 4 3 5 5 10 9 86
restore order with direct children 7 22 6 2 4 3 5 5 10 9 87
restore order with direct children is 7 < both children? 7 22 6 2 4 3 5 5 10 9 88
restore order with direct children is 7 < both children? swap with smallest 7 22 6 2 4 3 5 5 10 9 89
restore order with direct children is 7 < both children? swap with smallest 6 22 7 2 4 3 5 5 10 9 90
percolate 6 down levels 6 22 7 2 4 3 5 5 10 9 91
percolate 6 down levels compare to all grandchildren is 6 < all the grandchildren? 6 22 7 2 4 3 5 5 10 9 92
percolate 6 down levels compare to all grandchildren is 6 < all the grandchildren? swap with smallest 6 22 7 2 4 3 5 5 10 9 93
percolate 6 down levels compare to all grandchildren is 6 < all the grandchildren? swap with smallest 6 22 7 2 4 3 5 5 10 9 94
percolate 6 down levels compare to all grandchildren is 6 < all the grandchildren? swap with smallest 2 22 7 6 4 3 5 5 10 9 95
does 6 have grandchildren? 2 22 7 6 4 3 5 5 10 9 96
6 has no grandchildren, but is not a leaf node compare with direct children to ensure order property 2 22 7 6 4 3 5 5 10 9 97
6 is on a level, so it must be smaller than children swap with smallest 2 22 7 6 4 3 5 5 10 9 98
6 is on a level, so it must be smaller than children swap with smallest 2 22 7 6 4 3 5 5 10 9 99
6 is on a level, so it must be smaller than children swap with smallest 2 22 7 5 4 3 5 6 10 9 100
6 is on a level, so it must be smaller than children swap with smallest done! 2 22 7 5 4 3 5 6 10 9 101
delete algorithm 102
delete ( is analogous) 1.locate node X (node containing item) 2.replace X with last node in tree (last index in array!) 3.detere if new X is violating order property with direct children - if so, swap contents of X with the largest child 4.percolate new item X down levels 5.if lowest level reached, restore order with lowest level (if applicable) 103
doubly-ended priority queue 104
-add -findmin -findmax -deletemin -deletemax - AND items can be found in constant time with a - heap -BUT lots of special cases -tutorial on website with diagrams and step-by-step explanations 105
next time 106
-reading -chapter 12 in book -homework -assignment 10 due tonight -assignment 11 is out 107