0% found this document useful (0 votes)
37 views

Binomial Trees Exaample

This document discusses binomial queues, a data structure that maintains a forest of binomial trees to represent a priority queue. It consists of 3 or fewer sentences: Binomial queues represent a priority queue as a forest of binomial trees, where each tree has a heap-like structure and merging two queues combines trees level-by-level like adding binary numbers. Common priority queue operations like insertion, deletion of minimum, and merging take O(log n) time due to the shallow height of O(log n) in an n-element queue.

Uploaded by

Numan Akhtar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Binomial Trees Exaample

This document discusses binomial queues, a data structure that maintains a forest of binomial trees to represent a priority queue. It consists of 3 or fewer sentences: Binomial queues represent a priority queue as a forest of binomial trees, where each tree has a heap-like structure and merging two queues combines trees level-by-level like adding binary numbers. Common priority queue operations like insertion, deletion of minimum, and merging take O(log n) time due to the shallow height of O(log n) in an n-element queue.

Uploaded by

Numan Akhtar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

CSE 326: Data Structures

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?

What’s a binomial tree?

• 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!

Write n in binary: n = 1101 (base 2) = 13 (base 10)

1 B3 1 B2 No B1 1 B0

10/14/2007 4
Properties of Binomial Queue
• At most one binomial tree of any height

• n nodes ⇒ binary representation is of size ?


⇒ deepest tree has height ?
⇒ number of trees is ?

Define: height(forest F) = maxtree T in F { height(T) }

Binomial Q with n nodes has height Θ(log n)

10/14/2007 5
Operations on Binomial Queue
• Will again define merge as the base operation
– insert, deleteMin, buildBinomialQ will use merge

• Can we do increaseKey efficiently?


decreaseKey?

• What about findMin?

10/14/2007 6
Merging Two Binomial Queues
Essentially like adding two binary numbers!

1. Combine the two forests


2. For k from 0 to maxheight {
a. m ← total number of Bk’s in the two BQs # of 1’s
b. if m=0: continue; 0+0 = 0
c. if m=1: continue; 1+0 = 1
d. if m=2: combine the two Bk’s to form a Bk+1 1+1 = 0+c
e. if m=3: retain one Bk and 1+1+c = 1+c
combine the other two to form a Bk+1
}
Claim: When this process ends, the forest
10/14/2007
has at most one tree of any height 7
Example: Binomial Queue Merge
H1: H2:

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

⇒ worst case running time = Θ( )

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( )

Average case complexity: O(1)


Why?? Hint: Think of adding 1 to 1101

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

find and delete


smallest root merge BQ
(without
the shaded part)
8 5 and BQ’
BQ’
7
10/14/2007 17
deleteMin: Example
Result:
7
4

8 5

runtime:
10/14/2007 18
10/14/2007 19

You might also like