Académique Documents
Professionnel Documents
Culture Documents
Lecture
BINOMIAL HEAPS
CS 473
Lecture X
Binomial Heaps
DATA STRUCTURES: MERGEABLE HEAPS
MAKE-HEAP ( )
Creates & returns a new heap with no elements.
INSERT (H,x)
Inserts a node x whose key field has already been
filled into heap H.
MINIMUM (H)
Returns a pointer to the node in heap H whose key
is minimum.
CS 473
Lecture X
Mergeable Heaps
EXTRACT-MIN (H)
Deletes the node from heap H whose key is
minimum. Returns a pointer to the node.
DECREASE-KEY (H, x, k)
Assigns to node x within heap H the new value k
where k is smaller than its current key value.
CS 473
Lecture X
Mergeable Heaps
DELETE (H, x)
Deletes node x from heap H.
CS 473
Lecture X
Binomial Trees
A binomial heap is a collection of binomial
trees.
The binomial tree Bk is an ordered tree defined
recursively
Bo Consists of a single node
.
.
.
Bk Consists of two binominal trees Bk-1
linked together. Root of one is the
leftmost child of the root of the other.
CS 473
Lecture X
Binomial Trees
B k-1
B k-1
Bk
CS 473
Lecture X
Binomial Trees B
B1
B1
B0
B1
B0
B1
B2
B3
B3
B2
B0
B1
CS 473
B4
Lecture X
Binomial Trees
B2
Bk-2
B1
Bo
Bk-1
CS 473
Bk
Lecture X
Lecture X
Lecture X
10
D(k-1, i)
D(k-1,i -1)
Bk-1
D(k,i)=D(k-1,i -1) + D(k-1,i) =
Bk-1
true by induction
k-1
i -1
CS 473
d=i
d=i
Lecture X
k-1
i
k
=
i
11
Lecture X
12
Bk-3
B1 B0
B2
Bk-2
B1 B0
Bk-3
B2
Bk-2
CS 473
Bk-1
Lecture X
13
CS 473
14
Binomial Heaps
A BINOMIAL HEAP H is a set of BINOMIAL
TREES that satisfies the following Binomial
Heap Properties
1. Each binomial tree in H is HEAP-ORDERED
CS 473
15
Binomial Heaps
2. There is at most one binomial tree in H whose
root has a given degree,
n-node binomial heap H consists of at most
[lgn] + 1 binomial trees.
Binary represantation of n has lg(n) + 1 bits,
n b
lgn
,b
lgn -1
, ....b1, b0> =
lgn
i=0
bi 2i
Lecture X
16
Binomial Heaps
Example: A binomial heap with n = 13 nodes
3 2 1 0
13 =< 1, 1, 0, 1>2
Consists of B0, B2, B3
head[H]
10
B0
12
B2
25
18
11
27
CS 473
Lecture X
14
17
38
29
B3
17
Lecture X
18
parent
key
degree
child
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
sibling
10
10
10
CS 473
Lecture X
19
CS 473
Lecture X
20
RUNNING-TIME= (1)
end
CS 473
Lecture X
21
end
CS 473
Lecture X
22
Lecture X
23
Lecture X
24
CS 473
Lecture X
25
child[z]
NIL
+1
p[y]
sibling [y]
CS 473
Lecture X
26
Lecture X
27
Lecture X
28
next-x
Bk
prev-x
Bk
CS 473
Bl
l >k
sibling { next-x}
next-x
Bl
Lecture X
29
next-x
BK
a
BK
BK
prev-x
next-x
BK
CS 473
sibling [next-x]
BK
Lecture X
BK
30
CS 473
Lecture X
31
prev-x
next-x
Bk
sibling [next-x]
Bk
x
Bl
next-x
CASE 3
c
prev-x
next-x
CASE 4
b
CS 473
l>k
Lecture X
32
Lecture X
33
Lecture X
34
Binomial-Heap-Union Procedure
BINOMIAL-HEAP-MERGE PROCEDURE
- Merges the root lists of H1 & H2 into a single linkedlist
- Sorted by degree into monotonically increasing order
CS 473
Lecture X
35
Binomial-Heap-Union Procedure
BINOMIAL-HEAP-UNION (H1,H2)
H MAKE-BINOMIAL-HEAP ( )
head [ H ] BINOMIAL-HEAP-MERGE (H1,H2)
free the objects H1 & H2 but not the lists they point to
prev-x NIL
x HEAD [H]
next-x sibling [x]
while next-x NIL do
if ( degree [x] degree [next-x] OR
(sibling [next-x] NIL and degree[sibling [next-x]] = degree [x]) then
prev-x x
CASE 1 and 2
x next-x
CASE 1 and 2
elseif key [x] key [next-x] then
sibling [x] sibling [next -x]
CASE 3
CS 473
Lecture X
36
CASE 3
else
if prev-x = NIL then
head [H] next-x
else
sibling [prev-x] next-x
endif
BINOMIAL-LINK(x, next-x)
x next-x
CASE 4
CASE 4
CASE 4
CASE 4
CASE 4
endif
next-x sibling [x]
endwhile
return H
end
CS 473
Lecture X
37
NODES : H2 =
5 4 3 2 1 0
ex:
CS 473
Lecture X
38
MERGE
CASE1 MARCH
Cin=0
1+0=1
next-x
B0
B1
x
B0
B1
B1
B2
B2
B4
B5
B5
next-x
B1
B2
B2
B4
B2
B4
B5
B5
B5
CASE3 or 4
LINK Cin=0
1+1=10
x
B0
CASE2 MARCH
then CASE3 and
CASE4 LINK
Cin=1
1+1=11
CS 473
B1
next-x
B2
B5
B1
B2
x
B0
B2
B2
Lecture X
next-x
B2
B4
B5
B5
39
B0
B2
CASE1
MARCH
Cin=1
0+0=1
next-x
B2
B4
B5
B3
B2
x
B0
CASE1
MARCH
Cin=0
0+1=1
CASE3 OR 4
LINK Cin=0
1+0=10
CS 473
B0
B2
B2
B5
B3
B3
next-x
B4
B5
B4
B5
next-x
B5
B5
x
B0
B2
B3
Lecture X
B4
B5
B5
B6
40
Inserting a Node
BINOMIAL-HEAP-INSERT (H,x)
H'
MAKE-BINOMIAL-HEAP (H, x)
P [x] NIL
child [x] NIL
RUNNING-TIME= O(lg n)
sibling [x] NIL
degree [x] O
head [H] x
H BINOMIAL-HEAP-UNION (H, H)
end
CS 473
Lecture X
41
H = < 110011>
H
MERGE
( H,H)
B0
B1
= { B0, B1 ,B4, B5 }
B4
B5
next-x
B0
B0
B1
B4
B5
5 4 3 2 1 0
LINK
next-x
B0
B1
B2
B0
B1
LINK
B4
B4
B4
B5
1 1 0 0 1 1
1
B5
B1
CS 473
B5
Lecture X
1 1 0 1 0 0
42
CS 473
Lecture X
43
Lecture X
44
B1
B4
B1
B2
CS 473
Lecture X
B0
45
x
head [H]
B0
B1
B4
B1
B2
B0
head [H]
CS 473
Lecture X
46
Lecture X
47
Decreasing a Key
BINOMIAL-HEAP-DECREASE-KEY (H, x, k)
key [x] k
y x
z p[y]
while z NIL and key [y] < key [z] do
exchange key [y] key [z]
exchange satellite fields of y and z
yz
z p [y]
endwhile
end
CS 473
Lecture X
48
Decreasing a Key
Similar to DECREASE-KEY in BINARY HEAP
BUBBLE-UP the key in the binomial tree it
resides in
RUNNING TIME: O(lgn)
CS 473
Lecture X
49
Deleting a Key
BINOMIAL- HEAP- DELETE (H,x)
yx
z p [y]
RUNNING-TIME=
while z NIL do
key [y] key [z]
satellite field of y satellite field of z
y z ; z p [y]
endwhile
H MAKE-BINOMIAL-HEAP
remove root z from the root list of H
reverse the order of the linked list of zs children
and set head [H] head of the resulting list
H BINOMIAL-HEAP-UNION (H, H)
CS 473
Lecture X
O(lg n)
50
CS 473
Lecture X
51