Chapter 19 Binomial Heaps 19.0.1
Data structures known as "mergeable heaps"
support these five operations:
MAKEHEAP() creates & returns a new empty heap
INSERT(H,x) inserts node x, whose key field
has been set, into a heap H.
MINIMUM(H) returns a pointer to the node in H
whose key is minimum.
EXTRACTMIN(H) deletes the node from H whose
key is minimum, returning a pointer to it.
UNION(H1,H2) creates and returns a new heap
containing all nodes of H1 and H2. H1 and
H2 are "destroyed" by this operation.
Binomial and Fibonacci heaps are mergeable
heaps which also support the two operations:
DECREASEKEY(H,x,k) assigns to node x the new
key value k, assumed to be <= current key.
DELETE(H,x) deletes node x from heap H.
As shown in Figure 19.1 (page 456) binary
heaps work well ( O(lg n) ) except for UNION,
which takes Theta(n) time (by concatenating
the two heaps and then running MIN_HEAPIFY).
Figure 19.1 (page 456) 19.0.2
Binomial Fibonacci
Binary heap heap heap
Function (worstcase) (worstcase) (amortized)

MAKEHEAP Theta(1) Theta(1) Theta(1)
INSERT Theta(lg n) O(lg n) Theta(1)
MINIMUM Theta(1) O(lg n) Theta(1)
EXTRACTMIN Theta(lg n) Theta(lg n) O(lg n)
UNION Theta(n) Theta(lg n) Theta(1)
DECREASEKEY Theta(lg n) Theta(lg n) Theta(1)
DELETE Theta(lg n) Theta(lg n) O(lg n)
Note that binomial heaps take only O(lg n)
for a UNION. Also, the times for Fibonacci
heaps are amortized time.
The SEARCH operation is inefficient for each
of these heaps, so DECREASEKEY and DELETE
each require a pointer to the given node,
which can be given as a handle from the
corresponding application object. Of course
each of the nodes would also have a handle to
the application object too.
19.1 Binomial trees and binomial heaps 19.1.1
A binomial heap is a set of binomial trees,
so we define them first.
19.1.1 Binomial trees
The binomial tree B is an ordered tree (see
k
Section B.5.2) defined recursively. As shown
in Figure 19.2(a) (page 458) the binomial tree
B consists of a single node. Inductively, B
0 k
consists of two B s linked together: the
k1
root of one is the leftmost child of the other
as shown in Figure 19.2(b) for B through B .
0 4
Figure 19.2(a) ____o
/ / \
o o / \
/ \ 
B / \ B
0  k1
B ___o
k1 / /
Figure 19.2(b) o o o o
o / / 
o  o o o o o
o  
B o o
0 B
1 B B
2 3
Figure 19.2(c) ______o_____ 19.1.2
/ / \ \
/ / \ \
/ / \ \
o o . . . o o
/ \ / \ 
/ \ / \ o B
  0
B B B
k1 k2 1
Lemma 19.1 (Properties of binomial tree B )
k
1. There are 2^k nodes.
2. The height of the tree is k.
3. There are C(k,i) nodes at depth i, for
i = 0, 1, 2, ... , k, where C(k,i) is the
binomial coefficient  hence the name
"binomial heap"
4. The root has degree k, which is greater
than any other node; and if the children of
the root are numbered left to right by k1,
k2, ... , 1, 0, child i is the root of a B
i
Proof: by induction on k. It is easy to check
that the properties hold for k = 0. For the
induction step, assume B satisfies the
properties. k1
1. B consist of two copies of B 19.1.3
k k1 k1 k k1
and so has 2 + 2 = 2 nodes.
2. In B one B is a child of the other, so
k k1
the height of the tree is one more than the
the height of B i.e. (k  1) + 1 = k.
k1
3. Let D(k,i) be the number of nodes of depth
i in B . That number is the number at depth
k
i in the "parent" B in B plus the number
k1 k
at depth i1 in the the "child" B . So:
k1
D(k,i) = D(k1,i) + D(k1,i1)
= C(k1,i) + C(k1,i1) by Ind. Hyp.
= C(k,i) by Exercise C.17 page 1099
4. The only node with greater degree than
B in B is the root, which has one more
k1 k
child than B , and since the root of B
k1 k1
has degree k1, the degree of the root is k.
By Ind. Hyp. the children of the root of
B are roots of B , B , ... , B , B .
k1 k2 k3 1 0
So the children of the root of B are:
B , B , ... , B , B . k
k1 k2 1 0
Corollary 19.2 The maximum degree of 19.1.4
any node in an nnode binomial tree is lg(n).
Proof: follows from properties 1 and 4.
19.1.2 Binomial heaps
A binomial heap H is a set of binomial trees
that satisfies the binomialheap properties:
1. Each binomial tree in H obeys the minheap
property: the key of a node is greater than
or equal to the key of its parent. Such a
tree is said to be minheapordered.
2. For any nonnegative integer k, there is at
most one binomial tree in H whose root has
degree k.
The first property says that the root of each
tree contains the smallest key in the tree.
The second property implies that an nnode
binomial heap contains at most floor(lg n) + 1
trees. Note that the binary representation of
floor(lg n) i
n = Sum ( b_i * 2 ) where b_i = 0 or 1,
i = 0
so that B appears in H if and only if b_i = 1
i
which proves the claim.
Figure 19.3(a) 19.1.5
A binomial heap with 13 nodes.
head[H]>(10)>(1)>(6)
/  __//
/  __/ / 
/  / / 
(12) (25) (8) (14) (29)
 /  
(18) /  
/  
(11) (17) (38)

(27)
The binary representation of 13 is 1101 and H
consists of the trees B , B , and B , with 8,
3 2 0
4, and 1 nodes respectively for a total of 13.
Representing binomial heaps
As in Figure 19.3(b), each binomial tree is
stored using the leftchild, rightsibling
representation, so each node x has pointers
child[x] to its leftmost child, sibling[x] to
its immediate right sibling, and p[x] to its
parent. Each node also contains a key field
and other application satellite data, and the
number of children in degree[x].
A sample node: 19.1.6
parent pointer
^

____
 * 

key value > 10/ 0 < degree value

 **> right sibling
/ pointer
/
left child pointer 
V
As Figure 19 shows, the roots of the trees
are organized in a linked list, the root list.
The degrees of the roots strictly increase as
we traverse the root list. For a root, x,
sibling[x] points to the next root in the root
list. Undefined pointers are NIL as usual.
A binomial heap H is accessed by head[H],
which is a pointer to the first root of the
root list, or NIL if the heap is empty.
Figure 19.3(b) shows the representation of
the binomial heap of Figure 19.3(a).
Figure 19.3(b) 19.1.7
_____ _____ _____
 /   /   / 
  
10/ 0  1/ 2  6/ 3
  
head[H]> / >  >  /
____/ ________/
/ ^ ^ / ^ ^ ^
/   / _______  
 ___/   / __/ 
___V_/ ____ _V__/ ____/ ___
         
    
12/ 1 25/ 0  8/2 14/1 29/0
    
  > / /  > > //
 ___/  
 ^ / ^ ^  ^
_V__  ___   
  __V_/ ___ V__
      
18/ 0   
 11/1 17/0 38/0
 / /   
  > //  //
  
 ^
_V_
 

27/0

 //

19.2 Operations on binomial heaps 19.2.1
We show implementations of the operations that
give the upper bounds as shown in the table of
Figure 19.1; see Ex. 19.210 for lower bounds.
Creating a new binomial heap
To make an empty heap, MAKEBINOMIALHEAP
allocates and returns an object H, where
head[H] = NIL. It runs in Theta(1) time.
Finding the minimum key
BINOMIALHEAPMINIMUM returns a pointer to
the node with the minimum key. We assume no
key has value infinity (see Exercise 19.25).
Since a binomial heap is minheapordered,
the minimum must be in a root, so we need only
check at most floor(lg(n)) + 1 nodes in the
root list, and so the running time is O(lg n).
BINOMIALHEAPMINIMUM(H)
1 y < NIL > a pointer to the current min
2 x < head[H]
3 min < infinity
4 while x not = NIL
5 do if key[x] < min
6 then min < key[x]
7 y < x
8 x < sibling[x]
9 return y
Uniting two binomial heaps 19.2.2
BINOMIALHEAPUNION repeatedly links binomial
trees of the same size. BINOMIALLINK links
the B rooted at y with the B rooted at z,
k1 k1
making z the parent of y, so z becomes the
root of a B_k tree. It makes y the new head
of the linked list of z's children. It works
because the left child, rightsibling
representation matches the tree ordering
property. It runs in O(1) time.
BINOMIALLINK(y,z)
1 p[y] < z
2 sibling[y] < child[z]
3 child[z] < y
4 degree[z] < degree[z] + 1
BINOMIALHEAPUNION unites heaps H1 and H2,
destroying them. There are two phases: first
BINOMIALHEAPMERGE (Exercise 19.21) merges
the roots of H1 and H2 into a single linked
list H in monotonically increasing order by
degree of the roots. There may be up to two
roots of the same degree, so the second phase
links roots of the same degree until there is
at most one root of each degree.
BINOMIALHEAPUNION works something like
addition of two binary numbers  the 1bits
in bit position i correspond to B_i's in the
root lists. As with binary addition, there
may be a "carry", giving three B_i's in a row.
BINOMIALHEAPUNION(H1,H2) 19.2.3
1 H < MAKEBINOMIALHEAP()
2 head[H] < BINOMIALHEAPMERGE(H1,H2)
3 free H1 and H2 but not lists they point to
4 if head[H] = NIL
5 then return H
6 prevx < NIL
7 x < head[H]
8 nextx < sibling[x]
9 while nextx not = NIL
10 do if (degree[x] not = degree[nextx]) or
(sibling[nextx] not = NIL and
degree[sibling[nextx]] = degree[x])
11 then prevx < x > Cases 1 and 2
12 x < nextx > Cases 1 and 2
13 else if key[x] <= key[nextx]
14 then sibling[x] < sibling[nextx]
15 BINOMIALLINK(nextx,x) > 3
16 else if prevx = NIL > 4
17 then head[H] < nextx > 4
18 else sibling[prevx] < nextx
19 BINOMIALLINK(x,nextx) > 4
20 x < nextx > 4
21 nextx < sibling[x]
22 return H
Lines 13 do the merging of phase 1, which
runs in O(m) time, where m is the total number
of roots in H1 and H2.
BINOMIALHEAPUNION simply returns the empty
heap in lines 45 if both H1 and H2 are empty.
19.2.4
Lines 622 do the "fixup" of phase 2, which
involves maintaining three root list pointers:
 x points to the current root being examined,
 prevx points to the root before x on the
root list, so that sibling[prevx] = x, and
 nextx points to the root after x on the
root list, so that sibling[x] = nextx.
BINOMIALHEAPMERGE guarantees that if two
roots have the same degree, they are adjacent
in the root list. In fact, due to a "carry"
from the previous combining of roots, we can
have three adjacent roots of the same degree.
There are four cases:
Case 1, shown in Figure 19.6(a), occurs when
degree[x] < degree[nextx], in which case
there is nothing to do and we simply advance
the pointers along the root list. This is
handled in lines 11, 12, and 21.
Case 2, shown in Figure 19.6(b), occurs when
degree[x] = degree[nextx] =
degree[sibling[nextx]], which we handle by
advancing the pointers as in case 1. We then
combine the last two trees in cases 3 or 4.
19.2.5
Cases 3 and 4, shown in Figures 19.6(c) and
19.6(d), occur when degree[x] = degree[nextx]
which is less than degree[sibling[nextx]];
they are distinguished by which is larger:
key[x] (case 4, lines 1620) or key[nextx]
(case 3, lines 1415).
Figure 19.5, pages 464465, shows an example
in which all four cases occur.
To find the running time, assume H1 contains
n1 nodes and H2 contains n2 nodes, so that H
contains n = n1 + n2 nodes. Then H1 contains
at most floor(lg n1) + 1 roots and H2 contains
at most floor(lg n2) + 1 roots, so H contains
at most floor(lg n1) + floor(lg n2) + 2
<= 2*floor(lg n) + 2 = O(lg n) roots.
Thus the run time for BINOMIALHEAPMERGE is
O(lg n). Also, each iteration of the while
loop takes O(1) time, and there are at most
floor(lg n1) + floor(lg n2) + 2 iterations,
since in each iteration the pointers are
advanced one root or one root is removed (by
becoming a left child). Thus phase 2 takes
O(lg n) time also, and so BINOMIALHEAPUNION
takes O(lg n) time overall.
Inserting a node 19.2.6
Assuming that x has been allocated and key[x]
has been set, BINOMIALHEAPINSERT inserts it
into a heap H by putting it in a 1element
heap H', which takes O(1) time, and unioning
it with H, which takes O(lg n) time.
BINOMIALHEAPINSERT(H,x)
1 H' < MAKEBINOMIALHEAP()
2 p[x] < NIL
3 child[x] < NIL
4 sibling[x] < NIL
5 degree[x] < 0
6 head[H'] < x
7 H < BINOMIALHEAPUNION(H,H')
Extracting the node with minimum key
BINOMIALHEAPEXTRACTMIN(H)
1 find the root x with the minimum key in the
root list of H and remove x from the root
list
2 H' < MAKEBINOMIALHEAP()
3 reverse the order of the linked list of x's
children, and set head[H'] to point to
the head of the resulting list
4 H < BINOMIALHEAPUNION(H,H')
5 return x
Figure 19.7 (page 469) shows how 19.2.7
it works. The original heap is shown in
Figure 19.7(a). Figure 19.7(b) shows the
situation after x has been removed in line 1.
Figure 19.7(c) shows the heap H' formed by the
reversed list of x's children from line 3.
And Figure 19.7(d) shows the resulting union
from line 4. Since lines 1, and 4 each take
O(lg n) time, line 3 takes Theta(lg n) in the
worst case, and lines 2 and 5 take O(1) time,
the whole procedure takes Theta(lg n) time.
Decreasing a key
BINOMIALHEAPDECREASEKEY(H,x,k)
1 if k > key[x]
2 then error "new key > current key"
3 key[x] < k
4 y < x
5 z < p[y]
6 while z not = NIL and key[y] < key[z]
7 do exchange key[y] <> key[z]
8 > also exchange any satellite data
9 y < z
10 z < p[y]
As shown in Figure 19.8 (page 471), it works
the same way as it does in a binary minheap:
by "bubbling up" the key in its tree, with z
maintained as y's parent. The while loop
stops when y is the root or key[y] >= key[z],
and the tree is minheapordered.
19.2.8
The while loop iterates at most floor(lg n)
times, since that is the maximum depth of x by
property 2 of Lemma 19.1. Thus the procedure
BINOMIALHEAPDECREASEKEY takes O(lg n) time.
Deleting a key
Assume that no key currently in the heap H
has value infinity (Exercise 19.26 indicates
that one can remove this restriction).
BINOMIALHEAPDELETE(H,x)
1 BINOMIALHEAPDECREASEKEY(H,x,infinity)
2 BINOMIALHEAPEXTRACTMIN(H)
BINOMIALHEAPDECREASEKEY bubbles infinity
(and satellite data) up to a root, where it is
removed by BINOMIALHEAPEXTRACTMIN. Since
both procedures run in Theta(lg n) time, so
does BINOMIALHEAPDELETE.