The PostOrder Heap - PowerPoint PPT Presentation

About This Presentation
Title:

The PostOrder Heap

Description:

Ancestry String. For node x at height h: Bits below h are 0 ... Updating Ancestry String. One ancestry string, for last node in heap. Must update after Insert ... – PowerPoint PPT presentation

Number of Views:219
Avg rating:3.0/5.0
Slides: 25
Provided by: nickh8
Category:

less

Transcript and Presenter's Notes

Title: The PostOrder Heap


1
The Post-Order Heap
  • Nick Harvey
  • Kevin Zatloukal

2
Binary Heap Review
keys
1
3
5
12
9
7
11

15
13
18
20
23
12
  • Simple priority queue
  • Heap-ordered parent key
  • Insert, DeleteMin require O(log n) time

3
Binary Heap Review
array indices
1
Implicittree structure
2
3
4
5
7
6

8
9
10
11
13
12

Array
2
3
1
4
5
7
6
8
9
10
11
13
12
  • Implicit stored in array with no pointers
  • Array index given by level-order traversal

4
The Heapify Operation
fix order

?
?
  • Combine 2 trees of height h new root
  • Fix up heap-ordering
  • Produces tree of height h1
  • Time O(h)

5
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

6
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

7
FindMin during BuildHeap
min?
  • 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!

8
A Better Ordering
1
2
4
5
  • BuildHeap new node either parent or leaf
  • Parent is good ? reduces trees by 1
  • Idea add parent whenever possible
  • This is a Post-Order insertion order

9
Insert
  • Insert
  • Run BuildHeap incrementally
  • Insertion order post-order

10
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

11
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

12
Fun with Sums
  • Write BuildHeap sum as
  • How can we evaluate this exactly?

13
Fun with Sums
  • How can we evaluate exactly?
  • Consider BuildHeap with n 2k1 - 1
  • The 2k leafs pay 0
  • The 2k - 1 internal nodes pay 2
  • Consider BuildHeap with n 2k1 - 1
  • The 2k leafs pay 0
  • The 2k - 1 internal nodes pay 2
  • Potential at end is k (height of final heap)
  • Total 2k1 - 2 - k

14
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

15
Navigating with Post-Order
where are the children?
x
?
?
1
2
4
5
  • 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!

16
Height of New Nodes
x
  • Where is node x1?

1) x is left child ? x1 is leaf ?
height 0
2) x is right child ? x1 is parent ? height
h1
  • Must know ancestry to update height!

17
Ancestry String
  • For node x at height h
  • Bits below h are 0
  • ith bit describes xs ancestor at height i
  • 0 left child, 1 right child


y
x
height h
18
Updating Ancestry String
  • One ancestry string, for last node in heap
  • Must update after Insert

19
Updating Ancestry String
  • One ancestry string, for last node in heap
  • Must update after Insert

height h
x
20
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

21
Recap Problems Solutions
  • Main idea Do Insert by incremental BuildHeap

22
Pseudocode
23
Experiments
  • 1 million Inserts (300 times)

Post-Order Heap
Binary Heap
  • Post-Order Heap
  • improves worst-case
  • reduces comparisons
  • larger constant factor due to bookkeeping

24
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!
Write a Comment
User Comments (0)
About PowerShow.com