240 likes | 758 Views
The Post-Order Heap. Nick Harvey & Kevin Zatloukal. Binary Heap Review. Simple p riority queue “Heap-ordered”: parent key < children keys Insert , DeleteMin require O(log n) time. keys. 1. 3. 5. 12. 9. 11. 7. …. 15. 13. 18. 20. 12. 23. Binary Heap Review.
E N D
The Post-Order Heap Nick Harvey & Kevin Zatloukal
Binary Heap Review • Simple priority queue • “Heap-ordered”: parent key < children keys • Insert, DeleteMinrequire O(log n) time keys 1 3 5 12 9 11 7 … 15 13 18 20 12 23
Binary Heap Review • “Implicit”: stored in array with no pointers • Array index given by level-order traversal array indices 1 Implicittree structure: 2 3 4 5 6 7 … 8 9 10 11 12 13 … Array: 1 2 3 4 5 6 7 8 9 10 11 12 13
The Heapify Operation fix order • Combine 2 trees of height h + new root • Fix up heap-ordering • Produces tree of height h+1 • Time: O(h) +
The BuildHeap Operation • Batch insert of n elements • Initially n/2 trees of height 0 • Repeatedly Heapify to get: • n/4 trees of height 1 • n/8 trees of height 2 … • Total time:
The FUN begins… • BuildHeap: O(n) batch insert of n elements. Cannot FindMin efficiently until done. • Is O(1) Insert possible? Yes: • Fibonacci Heaps • Implicit Binomial Queues (Carlsson et al. ’88) • Is there a simple variant of binary heap with O(1) Insert? Yes: • The Post-Order Heap
FindMin during BuildHeap • During BuildHeap, a FindMin is slow • Many small trees must search for min • But BuildHeap can heapify in other orders • “Children before parents” sufficient • Idea: Change order to reduce # trees! min?
O(logn) trees 7 3 6 10 8 9 11 12 A Better Ordering • BuildHeap: new node either parent or leaf • Parent is good reduces # trees by 1 • Idea: add parent whenever possible 1 2 4 5 • This is a Post-Order insertion order
Insert • Insert: • Run BuildHeap incrementally • Insertion order = post-order
FindMin & DeleteMin • FindMin • Enumerate the log n roots • O(log n) time (assumingenumeration is easy) • DeleteMin: like binary heap • Find min, swap it with last element • Heapify to fix up heap order • O(log n) time
Insert Analysis • Potential function = Sum of tree heights • Insert leaf • 0 comparisons, unchanged • Insert parent at height h • h comparisons for heapify • Decrease in = 2(h-1) - h = h - 2 Amortized cost = 2 • Total time: O(1) amortized
Expand and Contract! No Don,Potential Functions! Fun with Sums • Write BuildHeap sum as • How can we evaluate this exactly?
Fun with Sums • How can we evaluate exactly? • Consider BuildHeap with n = 2k+1 - 1 • The 2k leafs pay €0 • The 2k - 1 internal nodes pay €2 • Potential at end is k (height of final heap) • Consider BuildHeap with n = 2k+1 - 1 • The 2k leafs pay $0 • The 2k - 1 internal nodes pay $2 • Total = 2k+1 - 2 - k
Back to Post-Order Heaps • Problem: Array sparse not implicit • Why? Tree-array map = level-order Insertion order = post-order • Solution: use post-order for tree-array map Tree: Array:
7 3 6 10 8 9 11 12 Navigating with Post-Order where are the children? • For node x at height h • Right child = x - 1 • Left child = x - 1 - (size of right subtree)= x - 2h • Must know height to navigate! x ? ? 1 2 4 5
x+1 height h x+1 Height of New Nodes • Where is node x+1? x • 1) x is left child x+1 is leaf height 0 • 2) x is right child x+1 is parent height h+1 • Must know ancestry to update height!
z … … 0 0 0 1 0 h zero bits y = left child x = right child Ancestry String • For node x at height h: • Bits below h are 0 • ith bit describes x’s ancestor at height i 0 = left child, 1 = right child … y x height h
x’s ancestry string: height h … … … … 0 0 0 0 0 0 1 0 x = left child no ancestors y x x+1’s ancestry string: x+1 y = right child all left children Updating Ancestry String • One ancestry string, for last node in heap • Must update after Insert
x’s ancestry string: x+1 … … … … 0 0 0 0 0 0 0 1 x = right child no ancestors x+1’s ancestry string: no ancestors Updating Ancestry String • One ancestry string, for last node in heap • Must update after Insert height h x
Updating Ancestry String • One ancestry string, for last node in heap • Must update after Insert • O(1) time • Must update after DeleteMin • Easy to do in O(log n) time
Problem Solution Too many trees Post-order insertion order Not implicit Post-order tree-array map Need height to navigate Maintain height for last node Updating height Maintain ancestry string Recap: Problems & Solutions • Main idea: Do Insert by incremental BuildHeap
Pseudocode enumerate roots find children height & ancestry bookkeeping
Experiments • 1 million Inserts (300 times): • Post-Order Heap • improves worst-case • reduces # comparisons • larger constant factor due to bookkeeping Post-Order Heap Binary Heap
Summary • Potential function analysis of BuildHeap • Post-Order Heaps: • Implicit: O(1) extra space • Insert: O(1) amortized time • DeleteMin: O(log n) time • Simple, practical and FUN!