Skip to content
Part IA Lent Term

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 yy the leftmost child of root zz (assuming z.key≤y.keyz.\text{key} \le y.\text{key}). Sets y.parent=zy.\text{parent} = z, y.sibling=z.childy.\text{sibling} = z.\text{child}, z.child=yz.\text{child} = y, z.degree=z.degree+1z.\text{degree} = z.\text{degree} + 1.

Time: O(log⁡N1+log⁡N2)=O(log⁡N)O(\log N_1 + \log N_2) = O(\log N).

Insert: Step by Step

Insert elements [7,3,5,1,9,2,4][7, 3, 5, 1, 9, 2, 4] one at a time.

1. Insert 7: N=1=12N=1 = 1_2, heap has B0B_0 (root 7).

2. Insert 3: N=2=102N=2 = 10_2. New B0B_0 (3) merges with existing B0B_0 (7). 3 < 7, so 7 becomes child of 3. Result: B1B_1 tree, root 3, one child 7.

3. Insert 5: N=3=112N=3 = 11_2. New B0B_0 (5). Heap has B1B_1 (3, child 7). No equal-order trees to merge. Result: root list [B0(5),B1(3(7))][B_0(5), B_1(3(7))].

4. Insert 1: N=4=1002N=4 = 100_2. New B0B_0 (1) merges with B0B_0 (5) → B1B_1 (root 1, child 5). This B1B_1 merges with existing B1B_1 (root 3, child 7). 1 < 3, so B1B_1 (3) becomes child of B1B_1 (1). New tree: B2B_2, root 1, children: B1B_1 (root 3, child 7) and B0B_0 (5 — actually 5 is root of the merged B1B_1, but after merging into B2B_2, the children of root 1 are two B1B_1 trees: one with root 3 (child 7), one with root 5 (leaf)).

Wait — let us redo carefully. After merging B0(1)B_0(1) with B0(5)B_0(5), we get B1B_1 with root 1 (child 5). Then merging with existing B1B_1 (root 3, child 7): 1 < 3, so B1(3)B_1(3) becomes a child of root 1. The new B2B_2 tree has root 1, children from left: B1B_1 (root 3, child 7), then B0B_0 (5). Total 4 nodes ✓.

5. Insert 9: N=5=1012N=5 = 101_2. New B0B_0(9). Heap has B2B_2(1). No merge needed. Root list: [B0(9),B2(1(…))][B_0(9), B_2(1(\ldots))].

6. Insert 2: N=6=1102N=6 = 110_2. New B0B_0(2) merges with B0B_0(9) → B1B_1 (root 2, child 9). No further merge (no other B1B_1). Root list: [B1(2(9)),B2(1(…))][B_1(2(9)), B_2(1(\ldots))].

7. Insert 4: N=7=1112N=7 = 111_2. New B0B_0(4) cannot merge (no other B0B_0). Root list: [B0(4),B1(2(9)),B2(1(…))][B_0(4), B_1(2(9)), B_2(1(\ldots))].

Insert trace all steps

Extract-Min: Step by Step

Consider heap after inserting [7,3,5,1,9,2,4][7, 3, 5, 1, 9, 2, 4]: root list [B0(4),B1(2),B2(1)][B_0(4), B_1(2), B_2(1)] (showing root keys only).

Step 1: Find minimum root. Scan: 4,2,14, 2, 1. Min = 1 (root of B2B_2).

Step 2: Remove root 1 from root list. Remaining root list: [B0(4),B1(2)][B_0(4), B_1(2)].

Step 3: Promote children of root 1. The B2B_2 tree had children B1B_1 (root 3, child 7) and B0B_0 (5). After promotion, these become separate trees in a new heap. Reverse the child list (was B1,B0B_1, B_0; after reversal, B0,B1B_0, B_1 for increasing order): new heap has [B0(5),B1(3(7))][B_0(5), B_1(3(7))].

Step 4: BH-UNION the promoted children with the remaining root list:

  • Remaining: [B0(4),B1(2(9))][B_0(4), B_1(2(9))]
  • Promoted: [B0(5),B1(3(7))][B_0(5), B_1(3(7))]
  • Merge: B0(4)+B0(5)→B1B_0(4) + B_0(5) \to B_1 (root min(4,5) = 4, child 5). Then B1(2)+B_1(2) + new B1(4)→B2B_1(4) \to B_2 (root 2, children). Then B1(3)+B_1(3) + no matching → stays B1B_1.
  • Final root list: [B1(3(7)),B2(2(4(5),9))][B_1(3(7)), B_2(2(4(5), 9))]

The minimum of the new heap is the smaller of roots 3 and 2, which is 2.

Step 5: Return the extracted key 1.

Extract-min trace

Complexity Summary

OperationDetailed stepsCost
InsertCreate B0B_0, merge with root listO(log⁡N)O(\log N)
Extract-minFind min (O(log⁡N)O(\log N)), promote children (O(log⁡N)O(\log N)), re-merge (O(log⁡N)O(\log N))O(log⁡N)O(\log N)
Decrease-keyBubble up (at most log⁡N\log N levels)O(log⁡N)O(\log N)
MergeMerge root lists, cascade merges (binary addition)O(log⁡N)O(\log N)

The key insight: all operations are logarithmic because the number of trees and the height of any tree are both bounded by O(log⁡N)O(\log N).

Summary

InsightWhy it matters
Binary additionTree orders combine like binary carries
Root list size≤⌊log⁡N⌋+1\le \lfloor \log N \rfloor + 1
Tree height≤log⁡N\le \log N
BkB_k childrenBk−1,Bk−2,…,B0B_{k-1}, B_{k-2}, \ldots, B_0 — ordered by degree
Child list reversalEnsures increasing degree order after extract-min

Past Tripos: y2024p2q7, y2023p1q9.