Slides - Binomial Heaps - KCS503
Slides - Binomial Heaps - KCS503
Binomial Tree
Binomial Tree
Binomial Tree: Properties
A Binomial Tree of order k has following properties.
Nodes: 12
Degree, k= 2 & 3
Nodes: 13
Degree, k= 0, 2 & 3
Binomial Heap: Examples
Parent
Key
Degree
Child Sibling
Finding Minimum Key
BINOMIAL-HEAP-MINIMUM(H)
y ← NIL
x ← head[H]
min ← ∞
while x ≠ NIL
do
if key[x] < min
then min ← key[x]
y←x
x ← sibling[x]
return y
Linking Two Binomial Trees: Equal Degree
BINOMIAL-LINK(y, z)
p[y] ← z
Parent
sibling[y] ← child[z]
Key
child[z] ← y Degree
degree[z] ← degree[z] + 1 Child Sibling
Uniting Two Binomial Heaps
H ← MAKE-BINOMIAL-HEAP()
head[H] ← BINOMIAL-HEAP-MERGE(H1, H2)
free the objects H1 and H2 but not the lists they point to
prev-x ← NILif head[H] = NIL
then return H
x ← head[H]
next-x ← sibling[x]
Case 3: Link Same Degree Binomial Heaps
if key[x] ≤ key[next-x]
then sibling[x] ← sibling[next-x]
BINOMIAL-LINK(next-x, x)
Case 2: Updating Pointers
if (degree[x] ≠ degree[next-x]) or
(sibling[next-x] ≠ NIL and degree[sibling[next-x]] = degree[x])
then prev-x ← x
x ← next-x
Case 4: Linking & Reconnect with Previous
if prev-x = NIL
then head[H] ← next-x
else
sibling[prev-x] ← next-x
BINOMIAL-LINK(x, next-x)
x ← next-x
Case 3: Link Same Degree Binomial Heaps
BINOMIAL-HEAP-INSERT(H, x)
H’ ← MAKE-BINOMIAL-HEAP()
p[x] ← NIL
child[x] ← NIL
sibling[x] ← NIL
degree[x] ← 0
head[H’] ← x
H ← BINOMIAL-HEAP-UNION(H, H’)
Finding Minimum Key in a Binomial Heap
Separate the Node with Minimum key
H’ ← MAKE-BINOMIAL-HEAP()
reverse the order of the linked list of x’s children, and
set head[H’] to point to the head of the resulting list
Unite two Binomial Heaps
H ← BINOMIAL-HEAP-UNION(H, H’)
Finding Minimum Key in a Binomial Heap
BINOMIAL-HEAP-EXTRACT-MIN(H)
find the root x with the minimum key in the root list of H, and
remove x from the root list of H
H’ ← MAKE-BINOMIAL-HEAP()
reverse the order of the linked list of x’s children, and set
head[H’] to point to the head of the resulting list
H ← BINOMIAL-HEAP-UNION(H, H’)
return x
Decreasing a Key Value in a Binomial Heap
DECREASE-KEY(H, x, k) assigns to node x within heap H the new key
value k, which is assumed to be no greater than its current key value.
BINOMIAL-HEAP-DECREASE-KEY(H, x, k)
if k > key[x]
then error “new key is greater than current key”
Decreasing a Key Value in a Binomial Heap
key[x] ← k
y←x
z ← p[y]
Decreasing a Key Value in a Binomial Heap
BINOMIAL-HEAP-DECREASE-KEY(H, x, k)
if k > key[x]
then error “new key is greater than current key”
key[x] ← k
y←x
z ← p[y]
while z ≠ NIL and key[y] < key[z]
do exchange key[y] ↔ key[z]
y←z
z ← p[y]
Deleting a Key in a Binomial Heap
BINOMIAL-HEAP-DELETE(H, x)
BINOMIAL-HEAP-DECREASE-KEY(H, x, −∞)
BINOMIAL-HEAP-EXTRACT-MIN(H)