0% found this document useful (0 votes)
61 views9 pages

Binomial Heap

Binomial heaps are a data structure that supports five heap operations in logarithmic time or better. They are made up of binomial trees, which are recursively defined as either a single node or two binomial trees of one less order linked together. Binomial trees have properties like a height proportional to their order and a root node with higher degree than other nodes. Binomial heaps use these binomial trees to represent the heap and support fast union operations.

Uploaded by

Qwer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views9 pages

Binomial Heap

Binomial heaps are a data structure that supports five heap operations in logarithmic time or better. They are made up of binomial trees, which are recursively defined as either a single node or two binomial trees of one less order linked together. Binomial trees have properties like a height proportional to their order and a root node with higher degree than other nodes. Binomial heaps use these binomial trees to represent the heap and support fast union operations.

Uploaded by

Qwer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

19 Binomial Heaps

This chapter and Chapter 20 present data structures known as mergeable heaps,
which support the following five operations.
MAKE-HEAP) creates and returns a new heap containing no elements.
INSERT(H, x) inserts node x, whose key field has already been filled in., into
heap H
MINIMUM(H) returns a pointer to the node in heap H whose key is minimum.
EXTRACT-MIN(H) deletes the node from heap H whose key is minimum, return-
ing a pointer to the node.
UNION(H1, H2) creates and returns a new heap that contains all the nodes of heaps
Hi and H2. Heaps H1 and H2 are "destroyed" by this operation.
In addition, the data structures in these chapters also support the following two
operations.
DECREASE-KEY (H, x, k) assigns to node x within heap H the new key valuek,
which is assumed to be no greater than its current key value.
DELETE(H, x) deletes node x from heap H.
As the table in Figure 19.1 shows, if we don't need the UNION Operation, ordi-
nary binary heaps, as used in heapsort (Chapter 6), work well. Operations other
than UNION run in worst-case time O(lgn) (or better) on a binary heap. If the
UNION operation must be supported, however, binary heaps perfom poorly. By
concatenating the two arrays that hold the binary heaps to be merged and then run-
ning MIN-HEAPIFY (see Exercise 6.2-2), the UNION operation takes O(n) timein
the worst case.

'As mentioned in the introduction to Part V, our default mergeable heaps are mergeable min-

heaps, and so the operations MINIMUM, ExTRacT-MIN, and DECREASE-KEY apply. Alterna
tively, we could define a mergeable mar-heap with the operations MAXIMUM, EXTRACT-MAX,
and INCREASE-KEY.
19.1 Binomial trees and binomial
heaps 457
Chapter 19 Binomial Heaps

Binary heap Binomial heap Fibonacci heap 19.1 Binomial trees and binomial
heaps
Procedure (Worst-case)(worst-case) (amortized)_
(1) e(1) A binomial
MAKE-HEAP e(1) heap is a collection of binomial trees, so this section starts by defining
INSERT edgn) Olgn) e(1) binomial trees and proving some key
O(1) and show how
properties. We then define binomial heaps
MINIMUM O(1) Odgn) they can be
Olgn)
represented.
EXTRACT-MIN lgn) edgn)
UNION en) 0dgn) O1) 19.1.1 Binomial trees
DECREASE-KEY edgn) elgn) (1)
dgn) edgn) Odgn) The binomial tree B is an ordered tree (see Section
DELETE B.5.2) defined recursively.
As shown in Figure 19.2(a), the binomial tree Bo consists of a
single node. The
Running times for uperations three implementations of mergeable heaps. The binomial tree B; consists of two binomial trees B-1 that are linked
Figure 19.1 on
together: the
number of items in the heap(s) at the time of an operation is denoted by n.
root of one is the leftmost child of the root of the other.
Figure 19.2(b) shows the
binomial trees Bo through B4.
In this chapter, we examine "binomial heaps," whose worst-case time bounds are Some properties of binomial trees are given by the following lemma.
also shown in Figure 19.1. In particular, the UNION operation takes only O(lgn)
time to merge two binomial heaps with a total of n elements. Lemma 19.1 (Properties of binomial trees)
In Chapter 20, we shall explore Fibonacci heaps, which have even better time For the binomial tree B,
bounds for some operations. Note, however, that the running times for Fibonacci
1. there are 2 nodes,
heaps in Figure 19.1 are amortized time bounds, not worst-case per-operation time
bounds. 2. the height of the tree is k,
This chapter ignores issues of allocating nodes prior to insertion and freeing 3. there are exactly () nodes at depth i for i = 0, 1 , , k, and
nodes following deletion. We assume that the code that calls the heap procedures
deals with these details. 4. the root has degree k. which is greater than that of any other node; moreoverif
the children ofthe root are numbered from leftto right byk - 1,k - 2 . 0 ,
Binary heaps, binomial heaps, and Fibonacci heaps are all inefficient in their
support of the operation SEARCH; it can take a while to find a node with a given child i is the root of a subtree B;.
key. For this reason, operations such as DECREASE-KEY and DELETE that refer
to a given node require a pointer to that node as part of their input. As in our the basis is the binomial
discussion of priority queues in Section 6.5, when we use a mergeable heap in Proof The proof is by induction on k. For each property,
tree Bo. Verifying that each property holds for Bo is trivial.
an application, we often store a handle to the corresponding application object
For the inductive step, we assume that the lemma holds for B-1.
in each mergeable-heap element, as well as a handle to corresponding mergeable-
heap element in each applic tion object. The exact nature of these handles depends 1. Binomial tree B; consists oftwo copies of B-1, and so B;
has 2-l+2-l =
2
on the application and its implementation. nodes.
Section 19.1 defines binomial heaps after first defining their constituent binomial
of B-i are linked to form B, the
trees. It also introduces a particular representation of binomial heaps. Section 19.2 2. Because of the way in which the two copies
the maximum depth in B-1.
shows how we car. implenient operations on binomial heaps in the time bounds maximum depth of a node in B; is one greater than
maximum depth is (k 1) +l=k.
-

given in Figure 19.1. By the inductive hypothesis, this


i of binomial tree Bg. Since B:
3. Let Dk, i) be the number of nodes at depth i in B;-1
linked together, a node at depth
is composed of two copies of B-1 the
i and once at depth i +1. In other words,
appears in B; once at depth
number of nodes at depth i in B-1 plus
number of nodes at depth i in Be is the
19.1 Binomial trees and binomial heaps 459
Chapter 19 Binomial Heaps

Corollary 19.2
The maximum degree of any node in an n-node binomial tree is lgn.
()
Bp-1 Proof Immediate from properties 1 and 4 of Lemma 19.1.
Bx-
Bk depth The term "binomial tree" comes from property 3 of Lemma 19.1, since the
0 terms )are the binomial coefficients. Exercise 19.1-3 gives furtherjustification
for the term.
2
(b)
3 19.1.2 Binomial heaps

Bo B: B2 B3 Ba
A binomial heap H is a set of binomial trees that satisies the following
heap properties.
of node
binomial-
1. Each binomial tree in H obeys the min-heap property.
cf We that
the key
each
a
such tree is
is
greater than or equal to the key its parent. say
min-heap-ordered.
(C) 2. For any nonnegative integer k, there is at most one binomial tree in H whose

A
Bo root has degree k.
tree contains the
The first property tells us that the root of a min-heap-ordered
Br-2 smallest key in the tree.
Bk-1 H consists of at most
The second property implies that an n-node binomial heap
B
Llgn]+1 binomial trees. To see why, observe that the binary representation of n
has lgn] +1 bits, say (bignj. blgn)-1. bo),
-..,
so that n =
b2. By
therefore, binomial tree B; appears in H if and only if
Figure 19.2 (a) The recursive definition of the binomial tree Bg. Triangles represent rooted sub- property 1l of Lemma 19.1,
binomial trees.
trees. (b) The binomial trees Bo through B4. Node depths in B4 are shown. (c) Another way of bit b; =1. Thus, binomial heap H contains at most Llg n] +1
looking at the binomial tree Bg. H with 13 nodes. The binary represen-
Figure 19.3(a) shows a binomial heap
binomial trees B, B2,
tation of 13 is (110I), and H consists of min-heap-ordered
the number of nodes at depth i - 1 in B-1. Thus,
and Bo, having8, 4,and1 nodes respectively,
for a total of 13nodes.
Dk, i) = Dk- 1,i) + Dk - 1,i - 1) (by the inductive hypothesis)
Representing binomial heaps
-(7) =) (by Exercise C.1-7)
As shown in Figure 19.3(b), each binomiaB tree
of
within a binomial heap is stored
Section 10.4. Each node has a key
in the left-child, right-sibling representation
-() field and any other satellite
information required by the application.

each node r contains pointers plx]


to its parent, child[x] to
its leftmost
In addition,
child, and
noder is root, then
to its right. If
a
4. The only node with greater degree in B than in Bs-1 is the root, which
sibling[x] to the sibling of x immediately NIL, and if r is the
has one more child than in B;-1. Since the root of B-1 has NIL. If node x has no children,
then child[x] =

degree k.- 1, Plx] =

NIL. Each node x also contains the


the root of B; has degree k. Now, by the inductive
hypothesis, and as Fig- rightmost child of its parent, then sibling[x]
=

ure 19.2(c) shows, from left to right, the children of the root of number of children of x.
B-1 are roots field degree[x), which is the trees within a
binomial
of B-2, B1-3,.., Bo. When B-1 is linked to B-1, therefore, the children
of As Figure 19.3 also shows, the
roots of the binomial
The degrees
theresulting root are roots of B;-1, Bs-2,..., Bo. as the root list.
linked list, which we refer to
heap are organized in a
19.2 Operations on binomial heaps 461
Chapter 19 Binomial Heaps
460

(a) head[H]>(10- (1
12
J01) 101 110
OO1)O10) O110 001 1019 1100
O001) 0010 0100 1000
O000

Figure 19.4 The binomial tree B4 with nodes labeled in binary by a postorder walk.

(b) head[H]- Exercises


P
19.1-1
key Suppose that x is a node in a binomial tree within a binomial heap. and assume
degree that siblinglx] # NIL. Ifx is not a root, how does degreejsiblinglx]] compare to
child degreelx]? How about if x is a root?
sibling
19.1-2
Ifx is a nonroot node ina binomial tree within a binomial heap, how does degreelx)
compare to degreelplx]1?

19.1
Suppose we 'abel the nodes of binomial tree B, in binary by a postorder walk, as
in Figure 19.4. Consider a noder labeled I at depth i, and let j =k-i. Show
that x hasj l's in its binary representation. How many binary k-strings are there
Figure 19.3 A binomial heap H with n = 13 nodes. (a) The heap consists of binomial trees Bo, B2 that contain exactlyj l's? Show that the degree of x is equal to the number of l's
and B3, which have 1, 4, and 8 nodes respectively, totaling n = 13 nodes. Since each binomial tree to the right of the rightmost 0 in the binary representation
of l.
is min-heap-ordered, the key of any node is no less than the key of its parent. Also shown is the root
list, which is a linked list of roots in order of increasing degree. (b) A more detailed representation
of binomial heap H. Each binomial tree is stored in the left-child, right-sibling representation, and
each node stores its degree.
19.2 Operations on binomial heaps

of the roots strictly increase as we traverse the root list, By the second binomial- binomial heaps in the time
In this section, we show how to perform operations on
heap property, in an n-node binomial heap the degrees of the roots are a subset bounds; the lower
bounds shown in Figure 19.1. We shall only show the upper
of (0, 1,... lgnj}. The sibling field has a different meaning for roots than for bounds are lett as Exercise 19.2-10.
nonroots. Ifx is a root, then siblinglx] points to the next root in the root list. (As
usual, siblinglx] = NIL ifx is the last root in the root list.)
Creating a new binomial heap
A given binomial heap H is accessed by the field head[H), which is
simply a MAKE-BINOMIAL-HEAP procedure sim-
pointer to the first root in the root list of H. If binomial heap H has no elements, To make an empty binomial heap, the
where head[H] NIL. The running time
then head[H] ply allocates and returms an object H,
=
= NIL.
is (1).
19.2 Operatlons on binomial heaps 463
Binomial Heaps
Chapter 19
The following procedure unites binomial heaps H1 and H2, returning the
the minimum key sulting heap. It destroys the re-
Finding representations of Hi and H2 in the process. Be-
BINOMIAL-HEAP-MINIMUM
returns a pointer to the node with t sides BINOMIAL-LINK, the procedure uses an auxiliary procedure BINOMIAL-
The procedure assumes that HEAP-MERGE that merges the root lists of Hi and Hh into a
H. This implementation single linked list that
minimum key in an n-node binomial heap is sorted by degree into
with value oo. (See
Exercise 19.2-5.) monotonically increasing order. The BINOMIAL-HEAP
there are no keys MERGE procedure, whose
pseudocode we leave as Exercise 19.2-1, is similar to
BINOMIAL-HEAP-MINIMUM (H) the MERGE procedure in Section 2.3.1.

IyNIL BINOMIAL-HEAP-UNION(H1, Ha)


2 r-head[H] 1 HMAKE-BINOMIAL-HEAP0
3 min 0 0
2 head[H]
while x NIL
3
BINOMIAL-HEAP-MERGE(H1. Hz)
free the objects H and H2 but not the lists they point to
do if keylx] < min
4 if head[H = NIL
thenmin keylx]
X
5 then return H
7
6 prev-x NIL
8 xSiblinglx]
x head[H]
9 return y
reside in a
8 next-x sibling[x]
the minimum key must
Since a binomial heap is min-heap-ordered,
B I N O M I A L - H E A P - M I N I M U M procedure
checks all roots, which 9whilenext-x # NIL
root node. The 10 do if (degreelx] # degreelnext-x)) or
and a pointer to
the curent minimum in min
number at most Llgnj +1, saving of Figure 19.3, (siblinglnext-x] # NIL and degree[sibling|next-x]= degreelx)
When called on the binomial heap Cases 1 and 2
the current minimum in y. 11 thenprev-xx
B I N O M I A L - H E A P - M I N I M U M returns a pointer
to the node with key 1.
Cases 1and 2
time of 12 X next-x
1 roots to check, the unning
Because there are at most lgnj + 13 else if keylxl keylnext-x]
BINOMIAL-HEAP-MINIMUM is Olgn). thensiblinglr]- sibling(next-x] D Case 3
14
15 BINOMIAL-LINK (next-x, x) Case 3
Uniting two binomial heaps 16 else if prev-x = NIL DCase 4
17 thenhead[H] next-x DCase 4
is used as a subroutine by most of the
The operation of uniting two binomial heaps links 18 else sibling[prev-x] nex-x DCase 4
BINOMIAL-HEAP-UNION procedure repeatedly
remaining operations. The links 19 BINOMIAL-LINK (x, next-x) DCase 4
binomial trees whose roots have the same degree. The following procedure DCase 4
x next-x
the B-1 tree rooted at node z; that is, it makes z 20
the B- tree rooted at node y to next-x siblinglr]
thus becomes the root of a B tree. 21
the parent of y. Node z
22 return H
BINOMIAL-LINK (), 2) BINOMIAL-HEAP-UNION in which all four
cases

1 ply] Z
Figure 19.5 shows an example of
given in the pseudocode occur. has two phases. The first phase, per-
2 siblingly]child[z] The BINOMIAL-HEAP-UNION Procedure
3 childlz]y the root lists of binomial
formed by the call of BINOMIAL-HEAP-MERGE, merges into monotoni*
4 degreelz]degreelz] +1 heaps H and H2 into a single linked
list H that is sorted by degree
no more) of each
be as many as two roots (but
The BINOMIAL-LINK procedure makes node y the new head of the linked list cally increasing order. There might until at most one
links roots of equal degree
of node z's children in O(1) time. It works because the left-child, right-sibling degree, however, so the second phase linked list H is sorted by degree,
we can
in root remains of each degree. Because the
representation of each binomial tree matches the ordering propety of the tree:
a B tree, the leftmost child of the root is the root of a Br-I tree. perform all the link operations quickly.
19.2 Operations on binomial heaps
465
Chapter 19 Binomial Heaps
464
prev-x next-x
(d) headH 12
(a) headH, 12 0- 15 headH,18
2833 3 25

BINOMIAL-HEAP-MERGE
$0 Case 4

S5 55
prev-x next-x
next-x
(e) headH 12 15
b) head[H 1 2 1 8
8 18

Case 3 50) Case 3


50
.

nex-x prev-x next-x

(c) headlH 12 1 ( headH) 12

18 25 18
vialbe
Case 2 Case 41 50

BINOMIAL-HEAP-UNION. (a) Binomial heaps H and H2. (b) Bi- The BINOMIAL-HEAP-UNION procedure next initializes some pointers into the
Figure 19.5 The execution of
x is the first root on
nomial heap H is the output of BiINOMIAL-HEAP-MERGE(H1, H2). Initially, root list of H. First, it simply returns in lines 4-5 if it happens to be uniting two
< keylnext-x], case 3 applies.
the root list of H. Because both x and next-x have degree 0 and key[x] o1 beini. empty binomial heaps. From line 6 on, therefore, we know that H has at least one
with the same degree, so case 2 applies. (d) After
(c) After the link occurs, is the first of three roots root. Throughout the procedure, we maintain three pointers into the root list:
x is the first of two
all the pointers move down one position in the root list, case 4 applies, since
roots of equal degree. (e) After the link occurs, case 3 applies. () After another link, case 1 applies,
because x has degree 3 and next-x has degree 4. Th iteration of the while loop is the last, because points tothe root currently being examined,
I7O111t
after the pointers move down one position in the root list, nexr-r = NIL. prev-x points to the root preceding r on the root list: siblinglprev-x] = x (since
initially x has no predecessor, we start with prev-x set to NIL), and
In detail, the procedure works as follows. Lines 1-3 start by merging the root next-x points to the root following r on the root list: siblinglx] = next-x.
lists of binomial heaps H, and Hz into a single root list H. The root lists of Hi
and Hy are sorted by strictly increasing degree, and BINOMIAL HEAP- MERGE re- Initially, there are at most two roots on the root list H of a given degree: because
tuns a root list H that is sorted by monotonically increasing degree. If the root lists Hi and H2 were binomial heaps, they each had at most one root of a given degree.
of H and Hz have m roots altogether, BINOMIAL-HEAP-MERGE runs in O(m) Moreover, BINOMIAL-HEAP-MERGE guarantees us that if two roots in H have
time by repeatedly xamining the roots at the heads of the two root lists and ap- the same degree, they are adjacent in the root list.
In fact, during the execution of BINOMIAL-HEAP-UNION, there may be three
pending the root with the lower degree to the output root list, removing it from its roots of a given degree appearing on the root list H at some time. We shall see
input root list in the precess.
19.2 Operations on binomial heaps 467
Chapter 19 Binomial Heaps

ev-X next-x sibling|next-x]


of the while loop of prev X next-X
how this situation
could occur. At each iteration (a) , ..
ina moment based on their degrees
whether to link x and next-x Case
lines 9-21, therefore, we decide of the is that each time **************|
An invariant loop
and possibly the degree of siblinglnext-x]. non-NIL. (See Exercise 19.2-4 Bk B B
both x and next-x are B
we start the body of the loop,
for a precise loop invariant.) that
when degreelx] # degreelnext-x], prev-x next-x sibling{next-x]
Case 1, shown in Figure 19.6(a), occurs prev-x next-.

is the root of a Bi-tree for somel > k. (b)


and next-x
is, when x is the root of a B-tree Case
and next-x, so we simply march the
Lines 11-12 handle this case. We don't linkr to the node
the list. Updating next-x to point
pointers one position farther down Bk Bk BR B Bk
which is common to every case.
line 21,
following the new node x is handled in
is the first of three roots of equal
Case 2, shown in Figure 19.6(b), occurs when x
prev- next-x sibling|next-x] prev-x next-x
degree, that is, when (c)
Case 3
degreelx] = degree[next-x] = degree[sibling|next-x]
the pointers one
We handle this case in the same manner as case 1: we just march B B
iteration will execute either case 3 or case 4 keylx] s keylnext-x)
position farther down the list. The next
10 tests for
and third of the three equal-degree roòts. Line
to combine the second
both cases 1 and 2, and lines 11-12 handle both cases.
Br
when
Cases 3 and 4 occur when x is the first of two roots of equal degree, that is, prev-x next-x sibling(next-x]
.
prev-x next-x
(d)
degreelx] = degreefnext-x] # degreelsiblinglnext-x]]. Case 4
*************
These cases may occur in any iteration, but one of them always occurs immediately
Bk B B BR B
following case 2. In cases 3 and 4, we link x and next-x. The two cases are keyle]>keylnext-x]
distinguishedby whether x or next-x has the smaller key, which determines the
node that will be the root after the two are linked. B
is
In case 3, 19.6(c), keylx]< keylnext-x],
shown in Figure so next-x
linkedtox.
Line 14 removes next-x from the root list, and line 15 makes next-x the leftmost Figure 19.6 The four cases that occur in BINOMIAL-HEAP-UNION. Labels a, b, c, and d serve
child of x. only to identity the roots involved: they do not indicate the degrees or keys of these roots. In
In case 4, shown in Figure 19.6(d), next-x.has the smaller key, so x is linked to each case, x is the root of a Bg-tree and > k. (a) Case 1: degree[x] # degreelnext-x]. The
next-x. Lines 16-18 remove x from the root list; there are two cases depending pointers move one position farther down the root list. (b) Case 2: degree[x] degree[next-x] = =

and the
on whether x is the first root on the list (line 17) or is not (line 18). Line 19 then degree[sibling[next-x]]. Again, the pointers move one position farther down the list,
next iteration executes either case 3 or case 4. (c) Case 3: degreelx] = degreelnext-x] #
makes x the leftmost child of next-x, and line 20 updatesx for the next iteration. and link it
degree[sibling[next-x]] and < keylnext-x]. We remove next-x from the root list
keylx]
Following either case 3 or case 4, the setup for the next iteration of the while degree[sibling[next-x]] and
to x, creating a Bx+1-tree. (d) Case 4: degreelx] degree|next-x] #
=

loop is the same. We have just linked two B-trees to form a B+1-tree, which x key\next-x] < key[x). We remove x from the root list and link it to next-X, again creating Bk+1-tree.
a

now points to. There were already zero, one, or two other B+1-trees on the root
list resulting from BINOMIAL-HEAP-MERGE, SO x is now the first of either one,
H2 contain n2 nodes, so that n =n1 +n2. Then Hj contains
at
two, or three Br+1-trees on the root list. If x is the only one, then we enter case 1 contain n nodes and
contains at
in the next iteration: degree[x] # degree[next-x]. Ifx is the first of two, then we most llg n1l+l H2 contains at most lg n2l+l roots, and so H
roots and

[lgnil+ lgn2l+2 <2 lg n]+2 Olgn) roots immediately after the


call
enter either case 3 or case 4 in the next iteration. It is when x is the first of three most =

BINOMIAL-HEAP-MERGE is
that we enter case 2 in the next iteration. of BINOMIAL-HEAP-MERGE. The time to perform
The running time of BINOMIAL-HEAP-UNION is O(lgn), where n is the total thus O(lgn). Each iteration of the while loop takes O(1) time, and there are at
because each iteration either advances the
number of nodes in binomial heaps Hi and H2. We can see this as follows. Let most + llgn2] +2 iterations
g nil
H
19.2 Operations on binomial heaps 469
Binomial Heaps
Chapter 19
(a) head[H]
list ofH or removes a root from the root list.
pointers oneposition down the root

The total time is thus Olgn).


7)
Inserting a node
into binomial heap H, assuming that x has
The following procedure inserts node x
been filled in.
already been allocated and key[x] has already
(b) headlH]
BINOMIAL-HEAP-INSERT(H. x)
41) 28)13
1 H'-MAKE-BINOMIAL-HEAPO
2 plx] NIL
3 child x]NIL
siblinglx] NIL
degree[x]-0
6 head[H'] +x
7 H BINOMIAL-HEAP-UNION(H, H') (c) head[H]- headH]-

The procedure simply makes a one-node binomial heap H' in O(1) time and unites
it with the n-node binomial heap H in Odgn) time. The call to BINOMIAL-HEAP-
UNION takes care of freeing the temporary binomial heap H'. (A direct implemen-
tation that does not call BINOMIAL-HEAP-UNION is given as Exercise 19.2-8.)
(d) headH 2
Extracting the node with minimum key

The following procedure extracts the node with the minimum key from binomial
heap H and returns a pointer to the extracted node.

BINOMIAL-HEAP-EXTRACT-MIN(H)
I find the root x with the minimum key in the root list of H,
and remove x from the root list of H
Figure 19.7 The action of BINOMIAL-HEAP-EXTRACT-MIN. (a) A binomial heap H. (b) The
2 H' MAKE-BINOMIAL-HEAP() root x with minimum key is removed from the root list of H. (c) The linked list of x's children is
3 reverse the order of the linked list of x's children, reversed, giving another binomial heap H'. (d) The result of uniting H and H'.
and set head[H'] to point to the head of
the-resulting list
4
H-BINOMIAL-HEAP-UNION (H, H') in x's tree except for x itself. Because x's tree was removed from H in line 1, the
5 return x
binomial heap that results from uniting H and H' in line 4, shown in Figure 19.7(d),
This procedure works as shown in Figure 19.7. The contains all the nodes originally in H except for r. Finally, line 5 returns x.
input binomial heap H is Since each of lines 1-4 takes O (lg n) time if H has n nodes, BINOMIAL-HEAP
shown in Figure 19.7(a). Figure 19.7(b) shows the situation after
line 1: the root x EXTRACT-MIN runs in O(lgn) time.
with the minimum key has been removed from the root list
of H. If x is the root
of a B-tree, then by property 4 of Lemma 19.1, x's
children, from left to
are roots of
B-1", B:-2",. Bo-trees. Figure 19.7(c) shows that by reversingright,
,
the
list of x's children in line 3, we have a binomial
heap H' that contains every nod
19.2 Operations on hinomial heaps
Binomial Heaps 471
Chapter 19

(a) headH]-
Decreasing a key
heap H to a
node x in a binomial
The following procedure
decreases the key of a
than x's curent key.
error if k is greater
n e w valuek. t signals an
BINOMIAL-HEAP-DECREASE-KEY (H, x, k)
1 ifk>keylx] current key"
2 then error "new key is greater than
3 keylx]+k headH
4 y
5 z ply]
6 while z # NIL and key[y] < keylz]
do exchange keyly] +> key[z]
and z have satellite fields, exchange
them, too.
If y
9 y Z

10 zplyl
decreases a key in the same
manner (c)headi
As shown in Figure 19.8, this procedure After ensuring
the key in the heap.
as in a binary min-heap: by "bubbling up"
than the current key and then assigning the
that the new key is in fact no greater
node x.
new key to x, the procedure goes up
the tree, with y initially pointing to
is checked against the
In each iteration of the while loop of lines 6-10, key[y]
the binomial tree is now
key of y's parent z. Ify is the root key[y] 2 key[z],
or
and so its key is
min-heap-ordered. Otherwise, node y violates min-heap ordering,
other satellite information.
exchanged with the key of its parent z, along with any
and continues with
The procedure then sets y to z, going up one level in the tree, vitoemoodiow Figure 19.8iteration
Theactionofwhile
BINOMIAL-HEAP-DECREASE-KEY. (a) Thesituation just before line 6
the next iteration. ni viiogro) i1oot he hrst ofthe loop. Node y has had its key decreased to 7,
which is less than the
The BINOMIAL-HEAP-DECREASE-KEY procedure takes O(lgn) time. By key of y's parent z. (b) The keys of the two nodes are exchanged, and the situation just before line 6
of the second iteration is shown. Pointers y and z have moved up one level in the tree, but min-heap
so the while loop of
property 2 of Lemma 19.1, the maximum depth of x [lgn],
is
order is still violated. (c) After another exchange and moving pointers y andz up one more level, we
lines 6-10 iterates at most [lgnj times. find that min-heap order is satisfied, so the while loop terminates.

Deleting a key deals with the situation in which -oo cannot appear as a key, even temporarily.) It
and satellite information from binomial heap H then bubbles this key and the associated satellite information up to a root by calling
It is easy to delete a node x's key
in O(lgn) time. The following implementation assumes that no node currently in BINOMIAL-HEAP-DECREASE-KEY. This root is then removed from H by a call
the binomial heap has a key of-0o. 2ihs o f BINOMIAL-HEAP-EXTRACT-MIN.
The BINOMIAL-HEAP-DELETE procedure takes O(lgn) time.
BINOMIAL-HEAP-DELETE (H, x)
1 BINOMIAL-HEAP-DECREASE-KEY (H, x, -0o) Exercises
2 BINOMIAL-HEAP-EXTRACT-MIN(H)
19.2-1
The BINOMIAL-HEAP-DELETE procedure makes node x have the unique mini- Write pseudocode for BINOMIAL-HEAP-MERGE.
mum key in the entire binomial heap by giving it a key of -oo. (Exercise 19.2-6

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy