heaps and Hufgman codes 1
priority queues: motivation dynamically changing list of events with dates want to fjnd next event quickly list of running programs, some more important (e.g. what user will notice being slow) choose most important to run fjrst want to fjnd most important quickly list of connections, some interactive (video call), some not (download) want quick way to choose which one to service data structure: priority queue 2
priority queue ADT insert(priority, item) findMin() — return item with lowest (fjrst) priority deleteMin() — remove item with lowest (fjrst) priority 3
priority queue implementations sorted vector strict Fibannoci heap Fibannoci heap binary heap balanced tree structure sorted linked list 4 unsorted linked list deleteMin insert unsorted vector fjndMin Θ(1) (amortized) Θ( n ) Θ( n ) Θ(1) Θ( n ) Θ( n ) Θ( n ) Θ(1) Θ(1) Θ( n ) Θ(1) Θ(1) Θ(log n ) Θ(log n ) Θ(log n ) Θ(log n ) Θ(1) Θ(log n ) amortized Θ(1) Θ(1) amortized Θ(log n ) Θ(1) Θ(1) Θ(log n )
priority queue implementations sorted vector strict Fibannoci heap Fibannoci heap binary heap balanced tree structure sorted linked list 4 unsorted linked list deleteMin insert unsorted vector fjndMin Θ(1) (amortized) Θ( n ) Θ( n ) Θ(1) Θ( n ) Θ( n ) Θ( n ) Θ(1) Θ(1) Θ( n ) Θ(1) Θ(1) Θ(log n ) Θ(log n ) Θ(log n ) Θ(log n ) Θ(1) Θ(log n ) amortized Θ(1) Θ(1) amortized Θ(log n ) Θ(1) Θ(1) Θ(log n )
priority queue implementations sorted vector strict Fibannoci heap Fibannoci heap binary heap balanced tree structure sorted linked list 4 unsorted linked list deleteMin insert unsorted vector fjndMin Θ(1) (amortized) Θ( n ) Θ( n ) Θ(1) Θ( n ) Θ( n ) Θ( n ) Θ(1) Θ(1) Θ( n ) Θ(1) Θ(1) Θ(log n ) Θ(log n ) Θ(log n ) Θ(log n ) Θ(1) Θ(log n ) amortized Θ(1) Θ(1) amortized Θ(log n ) Θ(1) Θ(1) Θ(log n )
additional, optional operations unsorted linked list strict Fibannoci heap Fibannoci heap binary heap balanced tree not necessary to have a priority queue, but useful… sorted vector sorted linked list 5 decreaseKey decreaseKey — change value of key given index/pointer remove — remove value with given index/pointer unsorted vector structure remove Θ(1) Θ( n ) Θ(1) Θ( n ) Θ( n ) Θ( n ) Θ( n ) Θ(1) Θ(log n ) Θ(log n ) Θ(log n ) Θ(log n ) amortized Θ(1) amortized Θ(1) Θ(1) Θ(1)
additional, optional operations unsorted linked list strict Fibannoci heap Fibannoci heap binary heap balanced tree not necessary to have a priority queue, but useful… sorted vector sorted linked list 5 decreaseKey decreaseKey — change value of key given index/pointer remove — remove value with given index/pointer unsorted vector structure remove Θ(1) Θ( n ) Θ(1) Θ( n ) Θ( n ) Θ( n ) Θ( n ) Θ(1) Θ(log n ) Θ(log n ) Θ(log n ) Θ(log n ) amortized Θ(1) amortized Θ(1) Θ(1) Θ(1)
additional, optional operations unsorted linked list strict Fibannoci heap Fibannoci heap binary heap balanced tree not necessary to have a priority queue, but useful… sorted vector sorted linked list 5 decreaseKey decreaseKey — change value of key given index/pointer remove — remove value with given index/pointer unsorted vector structure remove Θ(1) Θ( n ) Θ(1) Θ( n ) Θ( n ) Θ( n ) Θ( n ) Θ(1) Θ(log n ) Θ(log n ) Θ(log n ) Θ(log n ) amortized Θ(1) amortized Θ(1) Θ(1) Θ(1)
aside: min v max can also have ADT with fjndMax/etc. instead of fjndMin/etc. same complexities, etc. (use difgerent comparisons) terms for heaps: “min-heap” (fjndMin version) or “max-heap” (fjndMax version) 6
binary heaps binary heap is a binary tree structure: almost a perfect tree 7 binary tree is not a binary search tree ordering: parent < child (everywhere in tree)
perfect binary trees A B D E C F G a binary tree is perfect or complete if all leaves have same depth all nodes have zero children (leaf) or two children 8 exactly the trees that achieve 2 h − 1 nodes
almost perfect/complete binary trees F only missing bottom-rightmost slots heaps are almost complete trees Xf Xe G Xd Xc C A Xb J E I H D B 9
almost perfect/complete binary trees F only missing bottom-rightmost slots heaps are almost complete trees Xf Xe G Xd Xc C A Xb J E I H D B 9
almost complete formally E C E D B A F C D single node tree is almost complete B A otherwise: almost complete if either 10 left child is complete with height h and right child almost complete with height h ; OR left child is almost complete with height h and right child is complete with height h − 1
leftChild = index * 2 rightChild = index * 2 + 1 trees as arrays 5 0 1 2 3 4 7 6 A 8 9 string theTree[17] = {"", "A", "B", ....} parentIndex = index / 2 index J 11 node B D H I E J Xb C F Xc Xd G Xe Xf A B C D E F G H I 10 11 12 13 14 15 16
trees as arrays A parentIndex = index / 2 string theTree[17] = {"", "A", "B", ....} 9 8 7 6 5 4 3 2 1 0 index J node Xb B D H I E J C F Xc Xd G Xe Xf 11 leftChild = index * 2 rightChild = index * 2 + 1 A B C D E F G H I 10 11 12 13 14 15 16
trees as arrays A parentIndex = index / 2 string theTree[17] = {"", "A", "B", ....} 9 8 7 6 5 4 3 2 1 0 index J node Xb B D H I E J C F Xc Xd G Xe Xf 11 leftChild = index * 2 rightChild = index * 2 + 1 A B C D E F G H I 10 11 12 13 14 15 16
why arrays single array — less storage/memory allocation represent tree as single vector 12
the heap property 10 20 40 50 700 60 80 99 85 13 heap property: parent ≤ any of its children
a non-heap 10 20 30 15 80 14 heap property: parent ≤ any of its children
heap code linked ofg slides page of repo class binary_heap { ... private : // heap[1] is root // leftChildIndex // parentIndex = index / 2 vector< int > heap; int heap_size; } 15 = index * 2 // rightChildIndex = index * 2 + 1
heap insert 10 40 50 70 25 60 80 99 85 25 10 40 50 700 30 60 80 99 85 insert(25) 30 85 add new node as leaf node 99 10 30 40 50 70 60 X 80 99 85 10 30 40 50 70 60 25 80 16 while new node < parent node: swap with parent
heap insert 10 40 50 70 25 60 80 99 85 25 10 40 50 700 30 60 80 99 85 insert(25) 30 85 add new node as leaf node 99 10 30 40 50 70 60 X 80 99 85 10 30 40 50 70 60 25 80 16 while new node < parent node: swap with parent
heap insert 10 40 50 70 25 60 80 99 85 25 10 40 50 700 30 60 80 99 85 insert(25) 30 85 add new node as leaf node 99 10 30 40 50 70 60 X 80 99 85 10 30 40 50 70 60 25 80 16 while new node < parent node: swap with parent
heap insert 10 40 50 70 25 60 80 99 85 25 10 40 50 700 30 60 80 99 85 insert(25) 30 85 add new node as leaf node 99 10 30 40 50 70 60 X 80 99 85 10 30 40 50 70 60 25 80 16 while new node < parent node: swap with parent
heap insert 10 40 50 70 25 60 80 99 85 25 10 40 50 700 30 60 80 99 85 insert(25) 30 85 add new node as leaf node 99 10 30 40 50 70 60 X 80 99 85 10 30 40 50 70 60 25 80 16 while new node < parent node: swap with parent
insert(int) void binary_heap::insert( int x) { ++heap_size; heap.push_back(x); percolateUp(x); } 17
percolateUp(int) void binary_heap::percolateUp( int index) { int newValue = heap[index]; // while not at root and // less than parent... while (index > 1 && newValue < heap[index / 2]) { // move parent down heap[index] = heap[index / 2]; // advance up the tree index /= 2; } heap[index] = newValue; } 18
insert runtime 19 worst case: log 2 N nodes changed
insert average case? average case is better assuming random keys: intuition: leafs have bottom half of values (on average) …so usually don’t need to move up …and if we do, parents of leafs have 25th to 50th percentile of values …so need to move up two steps even less about 2 steps moved up on average 20
heap deleteMin 99 is a heap 80 30 85 not a heap 30 40 85 60 80 81 85 30 40 50 85 70 60 80 99 81 deleteMin() 80 30 replace root with last leaf node 30 while node greater than children: swap with smallest child 10 30 40 85 60 80 99 81 85 40 81 X 60 80 99 81 30 85 40 60 80 99 21
heap deleteMin 99 is a heap 80 30 85 not a heap 30 40 85 60 80 81 85 30 40 50 85 70 60 80 99 81 deleteMin() 80 30 replace root with last leaf node 30 while node greater than children: swap with smallest child 10 30 40 85 60 80 99 81 85 40 81 X 60 80 99 81 30 85 40 60 80 99 21
heap deleteMin 99 is a heap 80 30 85 not a heap 30 40 85 60 80 81 85 30 40 50 85 70 60 80 99 81 deleteMin() 80 30 replace root with last leaf node 30 while node greater than children: swap with smallest child 10 30 40 85 60 80 99 81 85 40 81 X 60 80 99 81 30 85 40 60 80 99 21
Recommend
More recommend