Binomial Heap Operations in Detail
This note walks through binomial heap operations step by step, showing the tree transformations and the binary-addition analogy that makes them efficient.
Merge: The Binary Addition Analogy
Merging two binomial heaps corresponds to binary addition. Each heap has trees at certain orders (the 1-bits in its size). Merging processes trees like adding binary numbers: trees of equal order combine and “carry” to the next order.
Algorithm:
BH-UNION(H1, H2):
H = new empty heap
H.root_list = merge H1.root_list and H2.root_list in increasing degree order
if H.root_list is empty: return H
prev = NIL
x = H.root_list
next = x.sibling
while next != NIL:
if (x.degree != next.degree) or
(next.sibling != NIL and next.sibling.degree == x.degree):
prev = x // skip, move forward
x = next
else if x.key <= next.key:
x.sibling = next.sibling
BH-LINK(next, x) // make next a child of x
else:
if prev == NIL: H.root_list = next
else: prev.sibling = next
BH-LINK(x, next) // make x a child of next
x = next
next = x.sibling
return H
BH-LINK(y, z) makes root the leftmost child of root (assuming ). Sets , , , .
Time: .
Insert: Step by Step
Insert elements one at a time.
1. Insert 7: , heap has (root 7).
2. Insert 3: . New (3) merges with existing (7). 3 < 7, so 7 becomes child of 3. Result: tree, root 3, one child 7.
3. Insert 5: . New (5). Heap has (3, child 7). No equal-order trees to merge. Result: root list .
4. Insert 1: . New (1) merges with (5) → (root 1, child 5). This merges with existing (root 3, child 7). 1 < 3, so (3) becomes child of (1). New tree: , root 1, children: (root 3, child 7) and (5 — actually 5 is root of the merged , but after merging into , the children of root 1 are two trees: one with root 3 (child 7), one with root 5 (leaf)).
Wait — let us redo carefully. After merging with , we get with root 1 (child 5). Then merging with existing (root 3, child 7): 1 < 3, so becomes a child of root 1. The new tree has root 1, children from left: (root 3, child 7), then (5). Total 4 nodes ✓.
5. Insert 9: . New (9). Heap has (1). No merge needed. Root list: .
6. Insert 2: . New (2) merges with (9) → (root 2, child 9). No further merge (no other ). Root list: .
7. Insert 4: . New (4) cannot merge (no other ). Root list: .
Extract-Min: Step by Step
Consider heap after inserting : root list (showing root keys only).
Step 1: Find minimum root. Scan: . Min = 1 (root of ).
Step 2: Remove root 1 from root list. Remaining root list: .
Step 3: Promote children of root 1. The tree had children (root 3, child 7) and (5). After promotion, these become separate trees in a new heap. Reverse the child list (was ; after reversal, for increasing order): new heap has .
Step 4: BH-UNION the promoted children with the remaining root list:
- Remaining:
- Promoted:
- Merge: (root min(4,5) = 4, child 5). Then new (root 2, children). Then no matching → stays .
- Final root list:
The minimum of the new heap is the smaller of roots 3 and 2, which is 2.
Step 5: Return the extracted key 1.
Complexity Summary
| Operation | Detailed steps | Cost |
|---|---|---|
| Insert | Create , merge with root list | |
| Extract-min | Find min (), promote children (), re-merge () | |
| Decrease-key | Bubble up (at most levels) | |
| Merge | Merge root lists, cascade merges (binary addition) |
The key insight: all operations are logarithmic because the number of trees and the height of any tree are both bounded by .
Summary
| Insight | Why it matters |
|---|---|
| Binary addition | Tree orders combine like binary carries |
| Root list size | |
| Tree height | |
| children | — ordered by degree |
| Child list reversal | Ensures increasing degree order after extract-min |
Past Tripos: y2024p2q7, y2023p1q9.