Family Trees

advertisement
The Post-Order Heap
Nick Harvey &
Kevin Zatloukal
Binary Heap Review
keys
1
3
12
9
5
11
15 13 18 20 12 23



7
…
Simple priority queue
“Heap-ordered”: parent key < children keys
Insert, DeleteMin require O(log n) time
Binary Heap Review
array indices
1
Implicit
tree structure:
2
4
8
Array:


9
5
3
6
10 11 12 13
7
…
1 2 3 4 5 6 7 8 9 10 11 12 13
…
“Implicit”: stored in array with no pointers
Array index given by level-order traversal
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
…
logn
Total time:
i
i

n
2
 O ( n)

i 1

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
min?

During BuildHeap, a FindMin is slow
–

But BuildHeap can heapify in other orders
–

Many small trees  must search for min
“Children before parents” sufficient
Idea: Change order to reduce # trees!
A Better Ordering
O(log n) trees
7
3
1

4
10
5
8
9
11 12
BuildHeap: new node either parent or leaf
–

2
6
Parent is good  reduces # trees by 1
Idea: add parent whenever possible
–
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 (assuming enumeration 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
Fun with Sums
k

Write BuildHeap sum as
i 2
k i
i 1

How can we evaluate this exactly?
Expand and
Contract!
No Don,
Potential
Functions!
Fun with Sums
k

k i
i

2
How can we evaluate 
exactly?
i 1

Consider BuildHeap with n = 2k+1 - 1
–
–
–

The 2k leafs pay €0
$0
The 2k - 1 internal nodes pay €2
$2
Potential at end is k (height of final heap)
Total = 2k+1 - 2 - k
Back to Post-Order Heaps
Tree:
Array:



Problem: Array sparse  not implicit
Why? Tree-array map = level-order
Insertion order = post-order
Solution: use post-order for tree-array map
Navigating with Post-Order
x
?
?
where are the
children?
7
3
1

4
5
10
8
For node x at height h
–
–

2
6
Right child = x - 1
Left child = x - 1 - (size of right subtree)
= x - 2h
Must know height to navigate!
9 11 12
Height of New Nodes
x+1
height h
x
x+1

Where is node x+1?
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!
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
z
0 0 … 0 1 0 …
h zero bits
y = left child
x = right child
y
x
height h
Updating Ancestry String


One ancestry string, for last node in heap
Must update after Insert
x’s ancestry string:
0 0 … 0 0 …
height h
y
x
x+1
no ancestors
x = left child
x+1’s ancestry string:
0 0 … 0 1 …
all left children y = right child
Updating Ancestry String


One ancestry string, for last node in heap
Must update after Insert
height h
x+1
x
x’s ancestry string:
0 0 … 0 1 …
no ancestors
x = right child
x+1’s ancestry string:
0 0 … 0 0 …
no ancestors
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
Recap: Problems & Solutions

Main idea: Do Insert by incremental BuildHeap
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
Pseudocode
enumerate roots
find children
height & ancestry
bookkeeping
Experiments

1 million Inserts (300 times):
Ordering

Binary Heap
Comparisons Time
Post-Order Heap
Comparisons
Time
Increasing
1.00
2.31
2.00
7.59
Random
2.38
5.66
1.87
7.53
Decreasing
17.32
27.09
1.00
5.00
Post-Order Heap
–
–
–
improves worst-case
reduces # comparisons
larger constant factor due to bookkeeping
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!
Download