Está en la página 1de 24

15-211

Fundamental Structures of Computer


Science

Binomial Heaps

March 02, 2006


Ananda Guna
In this Lecture

Binomial Trees
Definition
properties
Binomial Heaps
efficient merging
Implementation
Operations
About Midterm
Binary Heaps

Binary heap is a data structure that


allows
insert in O(log n)
deleteMin in O(log n)
findMin in O(1)
How about merging two heaps
complexity is O(n)
So we discuss a data structure that
allows merge in O(log n)
Applications of Heaps

Binary Heaps
efficient findMin, deleteMin
many applications
Binomial Heaps
Efficient merge of two heaps
Merging two heap based data
structures
Binomial Heap is build using a
structure called Binomial Trees
Binomial Trees

A Binomial Tree Bk of order k is


defined as follows
B0 is a tree with one node
Bk is a pair of Bk-1 trees, where root of
one Bk-1 becomes the left most child of
the other (for all k 1)

B B B B
0 1 2 3
Merging two binomial trees

Merging two equal binomial trees of


order j

=
+

New tree has order j + 1


Properties of Binomial trees

The following properties hold for a


binomial tree of order k
Bk has 2k nodes
The height of Bk is k
Bk has kCi nodes at level i for i = 0,1,k
The root of Bk has k-children B0, B1, Bk-1 (in
that order) where the ith child is a binomial
tree of order i.
If binomial tree of order k has n nodes, then
k log n
Proofs
Lemma 1: BK has 2k nodes
Proof: (by induction). True for k=0, assume true for k=r.
Consider Br+1
Br+1 has 2r + 2r = 2r+1 nodes

Lemma 2: Bk has height k


Proof: homework

Lemma 3: Bk has kCi nodes at level i for i = 0,1,k


Proof: Let T(k,i) be the number of nodes at depth i. Then
T(k,i) = T(k-1,i) + T(k-1,i-1)
= k-1Ci + k-1Ci-1 = kCi
Binomial Heap

Binomial Heap is a collection of


binomial trees that satisfies the
following properties
No two binomial trees in the collection
have the same size
Each node in the collection has a key
Each binomial tree in the collection
satisfies the heap order property
Roots of the binomial trees are
connected and are in increasing order
Example
A binomial heap of n=15 nodes
containing B0, B1, B2 and B3 binomial trees

What is the connection between n


and the binomial trees in the heap?
Lemma

Given any integer n, there exists a


binomial heap that contain n nodes

Proof:
implementation
Implementation Binomial Tree Node

Fields in a binomial tree node


Key
number of children (or degree)
Left most child
Right most sibling
A pointer P to parent
Implementation Binomial Heap

head

root1 root2 root3 root4


Operations
Operations on Binomial Heaps

Merge is the key operation on


binomial heaps
merge()
insert()
findMin()
find the min of all children O(log n)
deleteRoot()
deleteNode()
decreaseKey()
Merging two binomial heaps

Suppose H1 and H2 are two binomial


heaps
Merge H1 and H2 into a new heap H
Algorithm:
Let A and B be pointers to H1 and H2
for all orders i
If there is one order i tree, merge it to H
If there are two order i trees, merge them into a
new tree of order i+1 and store them in a temp tree
T
If there are three order i trees in H1,H2 and T,
merge two of them, store as T and add the
remainder to H
Example
Binary Heap Operations

Insert
make a new heap H0 with the new
node
Merge(H0, H)
FindMin
min is one of the children connected to
the root
cost is O(log n)
Binary Heap Operations

DeleteRoot()
Find the tree with the given root
Split the heap into two heaps H1 and
H2
Binary Heap Operations

DeleteRoot() ctd..
Rearrange binomial trees in heap H2
Merge the two heaps

v v
Example
DeleteNode()

To delete a node, decrease its key


to -, percolate up to root, then
delete the root

DecreaseKey(): Decrease the key


and percolate up until heap order
property is satisfied
Summary

Two heaps
Binary Heaps
deleteMin()
Binomial Heaps
mergeHeaps()

Next Week
Midterm on Tuesday
Strings and Tries on thursday

También podría gustarte