Skip to content
Part IA Lent Term

Binomial Heap Structure and Operations

A binomial heap is a collection of heap-ordered binomial trees with at most one tree of each order. It implements a mergeable priority queue efficiently, with all core operations in O(logN)O(\log N) time.

Structure

A binomial heap HH is:

  • A root list: a linked list of roots of binomial trees, ordered by increasing tree degree (increasing order kk)
  • Each tree is heap-ordered (min-heap): parent key \le children’s keys
  • For each order k0k \ge 0, at most one BkB_k tree is present

Node Attributes

Each node stores:

  • key, payload
  • parent pointer (NIL for roots)
  • child pointer (to the leftmost/highest-degree child)
  • sibling pointer (next sibling in the doubly-linked or singly-linked child list)
  • degree (number of immediate children, not descendants)

The root list reuses sibling pointers of root nodes.

Binary Structure

The sizes of trees present in a binomial heap of NN nodes correspond exactly to the powers of 2 in the binary representation of NN. For N=11=10112=8+2+1N=11 = 1011_2 = 8+2+1, the heap contains B3B_3, B1B_1, and B0B_0. At most log2N+1\lfloor \log_2 N \rfloor + 1 trees exist.

Core Operation: Merge Two Trees

BH-MERGE(bt1, bt2): merge two binomial trees of equal degree kk. Make the root with the larger key the leftmost child of the root with the smaller key. Increment the degree of the surviving root. Time: O(1)O(1).

This is the primitive that all other merge operations build upon.

Operations

Find-Minimum

Scan the root list (at most log2N+1\lfloor \log_2 N \rfloor + 1 roots). Return the root with smallest key. Time: O(logN)O(\log N).

Merge (Destructive Union)

BH-UNION(H1, H2): merge two binomial heaps by:

  1. Merge the two root lists in increasing order of degree, like merging two sorted linked lists
  2. Scan the merged list: whenever two trees of the same degree kk are adjacent, merge them into a Bk+1B_{k+1} tree (using BH-MERGE). This may cascade (like binary addition with carries)

Time: O(logN1+logN2)=O(logN)O(\log N_1 + \log N_2) = O(\log N), where N=N1+N2N = N_1 + N_2.

This is the key advantage over binary heaps, which require Θ(N)\Theta(N) to merge.

Insert

Create a single-node B0B_0 heap from the new element, then BH-UNION with the existing heap.

BH-INSERT(H, key, payload):
    create new node n with degree 0
    return BH-UNION(H, {n})

Time: O(logN)O(\log N) (dominated by the union).

Extract-Minimum

  1. Find the root with minimum key (scan root list)
  2. Remove that root from the root list
  3. Reverse its child list (children are Bk1,Bk2,,B0B_{k-1}, B_{k-2}, \ldots, B_0 in that order; reversing gives increasing degree order)
  4. BH-UNION the reversed child list with the remaining root list
  5. Return the minimum

Time: O(logN)O(\log N) (scan root list O(logN)O(\log N), promote children O(logN)O(\log N), BH-UNION O(logN)O(\log N)).

Decrease-Key

Given a pointer to a node, decrease its key. If the heap property is violated (new key < parent key), swap keys and recurse upward (bubble up), exactly as in a binary heap. Maximum number of swaps: height of the tree, which is O(logN)O(\log N).

Time: O(logN)O(\log N).

Delete

Decrease the key to -\infty, then extract-min. Time: O(logN)O(\log N).

Worked Example

Insert elements [5,3,7,1,9,2][5, 3, 7, 1, 9, 2] into an initially empty binomial heap.

InsertNN (binary)Heap state (trees present)
51 (B0B_0)[B0:5][B_0: 5]
32 (B1B_1)B1:root 3,child 5B_1: \text{root } 3, \text{child } 5 (merged B0+B0B_0 + B_0)
73 (B1,B0B_1, B_0)[B1:3(5),B0:7][B_1: 3(5), B_0: 7]
14 (B2B_2)B2B_2 (merges cascade: 1+B0B11+B_0 \to B_1, plus existing B1B2B_1 \to B_2)
95 (B2,B0B_2, B_0)[B2:1(),B0:9][B_2: 1(\ldots), B_0: 9]
26 (B2,B1B_2, B_1)[B2:1(),B1:2(9)][B_2: 1(\ldots), B_1: 2(9)]

The B2B_2 tree after inserting 1 would have root 1, children B1B_1 (root 3 with child 5) and B0B_0 (7). The structure mirrors binary addition of NN.

Binomial heap insert trace

Summary

OperationTimeNotes
Find-minO(logN)O(\log N)Scan root list
MergeO(logN)O(\log N)Binary addition analogy
InsertO(logN)O(\log N)Create B0B_0, merge
Extract-minO(logN)O(\log N)Remove root, promote children, re-merge
Decrease-keyO(logN)O(\log N)Bubble up; max height logN\log N
DeleteO(logN)O(\log N)Decrease-key to -\infty, then extract-min

Past Tripos: y2024p2q7, y2023p1q9.