Binomial Trees Exaample
Binomial Trees Exaample
Binomial Queues
Peter Henry
on behalf of James Fogarty
Autumn 2007
10/14/2007 1
Yet Another Data Structure:
Binomial Queues
• Structural property
– Forest of binomial trees with at most
one tree of any height
What’s a forest?
• Order property
– Each binomial tree has the heap-order property
10/14/2007 2
The Binomial Tree, Bh
• Bh has height h and exactly 2h nodes
• Bh is formed by making Bh-1 a child of
another Bh-1
• Root has exactly h children
⎛h⎞
• Number of nodes at depth d is binomial coeff. ⎜⎜ ⎟⎟
⎝d ⎠
– Hence the name; we will not use this last property
B0 B1 B2 B3
10/14/2007 3
Binomial Queue with n elements
Binomial Q with n elements has a unique structural
representation in terms of binomial trees!
1 B3 1 B2 No B1 1 B0
10/14/2007 4
Properties of Binomial Queue
• At most one binomial tree of any height
10/14/2007 5
Operations on Binomial Queue
• Will again define merge as the base operation
– insert, deleteMin, buildBinomialQ will use merge
10/14/2007 6
Merging Two Binomial Queues
Essentially like adding two binary numbers!
21 1 -1 3 5
7 2 1 3 9 6
8 11 5 7
10/14/2007 8
Example: Binomial Queue Merge
H1: H2:
1 -1 3 5
7 2 1 3 21 9 6
8 11 5 7
10/14/2007 9
Example: Binomial Queue Merge
H1: H2:
1 -1 5
7 3 2 1 3 9 6
21 8 11 5 7
10/14/2007 10
Example: Binomial Queue Merge
H1: H2:
1 -1
7 3 5 2 1 3
21 9 6 8 11 5
7 6
10/14/2007 11
Example: Binomial Queue Merge
H1: H2:
-1
2 1 3 1
8 11 5 7 3 5
6 21 9 6
10/14/2007 12
Example: Binomial Queue Merge
H1: H2:
-1
2 1 3 1
8 11 5 7 3 5
6 21 9 6
10/14/2007 13
Complexity of Merge
Constant time for each height
Max number of heights is: log n
10/14/2007 14
Insert in a Binomial Queue
Insert(x): Similar to leftist or skew heap
runtime
Worst case complexity: same as merge
O( )
10/14/2007 15
deleteMin in Binomial Queue
Similar to leftist and skew heaps….
10/14/2007 16
deleteMin: Example
BQ 7
3
4
8 5
8 5
runtime:
10/14/2007 18
10/14/2007 19