← The Ledger
Vol. I, No. 14 · Heap Structures

The Binomial Heap

A forest of power-of-two trees whose orders track the bits of the element count.

Written by Khushal Agrawal

loading…

Binomial Trees

A binomial tree of order k, written B_k, is defined recursively: B₀ is a single node, and B_k is two copies of B_{k−1} with one root made the leftmost child of the other. So B₃ is a root with three subtrees of orders 2, 1, and 0.

The shape fixes every measurement. B_k holds exactly 2^k nodes, has height k, gives its root degree k, and holds C(k, d) nodes at depth d — the binomial coefficients the tree is named for. A root of degree k therefore certifies a subtree of exactly 2^k nodes, and no root degree can exceed ⌊log₂ n⌋.

The Node Record

Each node records its degree and three links. The degree doubles as the order of the subtree it roots, which is what merge compares; the parent pointer exists for decrease-key.

typedef struct bnode {
    key_t         key;
    int           degree;    // children = order of this subtree
    struct bnode *parent;
    struct bnode *child;     // leftmost child, the highest order
    struct bnode *sibling;   // next root, or next child of the parent
} bnode;

That is about 32 bytes of overhead per element on a 64-bit machine, plus one allocation, against none for an array-backed binary heap. Root and child lists are scattered across the allocator's output, so every step of a sweep risks a cache miss. This is why a binomial heap loses to a binary heap on workloads that never merge.

Forest As Binary Counter

A binomial heap is a forest of binomial trees with at most one tree of each order, every tree obeying the min-heap property at every edge. Since orders are distinct and a tree of order k holds 2^k nodes, the orders present are exactly the set bits of n in binary. A heap of 13 elements holds B₃, B₂, and B₀.

That correspondence is the whole design. The forest holds at most ⌊log₂ n⌋ + 1 trees, and the roots are kept in a linked list in increasing order of degree. Every operation reduces to arithmetic on a binary counter, and the carry propagation of that counter is what bounds the work. Unlike the binary heap, the minimum is not at a fixed location: it is the smallest of the root-list keys.

Linking Two Trees

The one primitive is link: given two roots of the same order k, the larger becomes the leftmost child of the smaller. The result is a tree of order k + 1 that still satisfies the heap property, because the demoted root's key is no smaller than its new parent's.

// y and z root trees of order k, and z->key <= y->key
void link(bnode *y, bnode *z) {
    y->parent  = z;
    y->sibling = z->child;   // y becomes z's leftmost child
    z->child   = y;
    z->degree++;             // z now roots order k + 1
}

A link is one comparison by the caller and four pointer writes, O(1). It is the carry step of a binary counter: two trees of order k become one of order k + 1, as two units in one bit position carry into the next.

Merge

Merging two heaps splices their root lists into one list sorted by degree, then sweeps it, linking adjacent roots of equal order. Merging 13 (B₃ B₂ B₀) with 6 (B₂ B₁) links the two B₂s into a B₃, which collides with the existing B₃ and becomes a B₄: 19 is B₄ B₁ B₀.

bnode *heap_union(bnode *h1, bnode *h2) {
    bnode *h = merge_root_lists(h1, h2);           // sorted by degree
    bnode *prev = NULL, *x = h, *next = x ? x->sibling : NULL;
    while (next) {
        if (x->degree != next->degree ||
            (next->sibling && next->sibling->degree == x->degree)) {
            prev = x; x = next;                     // no link, or three in a row
        } else if (x->key <= next->key) {
            x->sibling = next->sibling;
            link(next, x);
        } else {
            if (prev) prev->sibling = next; else h = next;
            link(x, next);
            x = next;
        }
        next = x->sibling;
    }
    return h;
}

Each link removes a root from O(log n), so merge is O(log n) worst-case.

Insert And Find-Min

Insertion makes the new element a one-node heap, a single B₀, and merges it. The merge ripples a carry through the consecutive low-order trees: inserting into a heap of 7 (B₂ B₁ B₀) links B₀ with B₀, then the B₁s, then the B₂s, leaving a lone B₃.

bnode *insert(bnode *h, bnode *x) {
    x->parent = x->child = x->sibling = NULL;
    x->degree = 0;
    return heap_union(h, x);     // carry chain of at most log n links
}

The worst case is O(log n), exactly when n is one less than a power of two. find-min scans the O(log n) roots unless the heap caches a pointer to the smallest, which most implementations refresh whenever the root list changes, making it O(1).

Extract-Min

The minimum is a root, so removing it is structural. Its children are binomial trees of orders k − 1 down to 0, each a valid heap already, so reversing the child list produces a binomial heap in ascending order, which is then merged back.

bnode *extract_min(bnode *h, bnode **out) {
    bnode *m = remove_min_root(&h);
    bnode *kids = NULL;
    for (bnode *c = m->child; c; ) {        // orders k-1..0 become 0..k-1
        bnode *nx = c->sibling;
        c->sibling = kids;
        c->parent  = NULL;
        kids = c;
        c = nx;
    }
    *out = m;
    return heap_union(h, kids);
}

The removed root had degree at most ⌊log₂ n⌋, so both forests contribute O(log n) roots and the operation is O(log n) worst-case, with no sift-down and no comparison between siblings.

Decrease-Key And Delete

Decrease-key lowers a key and bubbles it up its ancestor chain, swapping with the parent while the parent is larger. The path is at most the tree's height, ⌊log₂ n⌋, and the forest structure never changes.

void decrease_key(bnode *x, key_t k) {
    x->key = k;
    for (bnode *p = x->parent; p && x->key < p->key; x = p, p = p->parent)
        swap_payload(x, p);     // keys and values move; nodes stay put
}

Lowering 17 to 2 at the bottom of a path 3 9 12 17 swaps three times and leaves 2 at the root. Deleting an arbitrary node is decrease-key to negative infinity followed by extract-min, O(log n). Because payloads move, external handles must point at payload records that are updated on each swap, as an indexed binary heap tracks positions.

Amortized Insertion

An insertion that triggers a full carry chain costs Θ(log n), but such insertions are rare in the same way binary-counter overflows are: bit i flips once every 2^i increments.

n before   n after   links
0111       1000      3
1000       1001      0
1001       1010      1
1010       1011      0
1011       1100      2

Phi = number of trees; an insert with c links changes Phi by 1 - c
amortized = actual + dPhi = (c + 1) + (1 - c) = 2

So n insertions into an empty heap perform O(n) links: insertion is O(1) amortized while staying O(log n) worst-case. This is the standard counter example of amortized analysis. The potential is the tree count because every link removes exactly one tree, so the links an expensive insert performs are paid for by the trees earlier cheap inserts left behind.

Lazy Merging

A lazy binomial heap skips the merge sweep. Insert and merge just splice root lists in O(1), allowing many trees of the same order, and extract-min pays the debt by consolidating the whole root list.

bnode *by_degree[MAX_DEGREE] = {0};
for (bnode *x = roots, *next; x; x = next) {
    next = x->sibling;
    while (by_degree[x->degree]) {             // a tree of this order exists
        bnode *y = by_degree[x->degree];
        by_degree[x->degree] = NULL;
        if (y->key < x->key) { bnode *t = x; x = y; y = t; }
        link(y, x);                            // x's degree grows by one
    }
    by_degree[x->degree] = x;
}

That gives O(1) worst-case insert and merge with O(log n) amortized extract-min. Adding cuts and marks so decrease-key is also O(1) amortized produces the Fibonacci heap, whose consolidation is this loop. The lazy heap gives up the worst-case bound on extract-min: one call can face n roots after n inserts, though it leaves at most ⌊log₂ n⌋ + 1 behind.

Engineering Trade-offs

Binomial heaps offer O(log n) worst-case bounds for insert, merge, extract-min, decrease-key, and delete, with O(1) amortized insertion. Merging is the reason to choose one: it is logarithmic rather than linear, and the structure is simple enough that the bounds hold with no potential-function argument required for correctness. The worst-case guarantee also matters where latency, not throughput, is the constraint.

Against a binary heap, the costs are pointer overhead, allocation, and cache behaviour. Against a Fibonacci heap, the asymptotics for insert, merge, and decrease-key are worse by a logarithmic factor amortized. Against a pairing heap, the code is longer and the measured constants are usually higher. The binomial heap occupies the middle: better bounds than an array heap, plainer analysis than its lazy descendants.

Applications

The sources reviewed for this chapter do not identify a prominent production system that documents a binomial heap in a critical path. Its measurable influence is structural rather than deployed: the lazy binomial heap is the direct ancestor of the Fibonacci heap, and skew binomial heaps are the basis of the Brodal–Okasaki bootstrapped queue, which achieves worst-case constant-time insert and merge in a purely functional setting.

Library containers do exist and are worth naming as implementations. The GNU C++ library's policy-based data structures expose binomial_heap_tag and rc_binomial_heap_tag as instantiations of __gnu_pbds::priority_queue, the latter maintaining a redundant counter to make insertion worst-case constant. Boost.Heap ships boost::heap::binomial_heap as a mergeable queue with mutable handles, and the Haskell pqueue package implements its priority queues as a binomial heap augmented with a global root and a lazily maintained spine.

Summary

A binomial heap is a forest of heap-ordered binomial trees with distinct orders, so the forest mirrors the binary representation of the element count and never holds more than ⌊log₂ n⌋ + 1 trees. Linking two equal-order trees is one comparison, and every operation is a sequence of links driven by carry propagation.

Insert, merge, extract-min, decrease-key, and delete are all O(log n) worst-case, with insertion O(1) amortized over a sequence. The price is four pointers and a degree field per node and an allocation per element, which costs more in practice than the array arithmetic it replaces. Merging in logarithmic rather than linear time is what the extra space buys.

The Binomial Heap: A forest of power-of-two trees whose orders track the bits of the element count
The Binomial Heap — A forest of power-of-two trees whose orders track the bits of the element count.

Sources & further reading

Every claim in this chapter traces to a primary source. Peer-reviewed papers are linked by DOI.

Primary sources

  1. Vuillemin, J. (1978). A data structure for manipulating priority queues. Communications of the ACM, 21(4), 309–315. doi.org/10.1145/359460.359478
  2. Brown, M. R. (1978). Implementation and analysis of binomial queue algorithms. SIAM Journal on Computing, 7(3), 298–319. doi.org/10.1137/0207026
  3. Brodal, G. S. & Okasaki, C. (1996). Optimal purely functional priority queues. Journal of Functional Programming, 6(6), 839–857. doi.org/10.1017/S095679680000201X

Applications and implementations

  1. GNU Project. libstdc++ Policy-Based Data Structures: __gnu_pbds::priority_queue. gcc.gnu.org/onlinedocs/libstdc++/ext/pb_ds/priority_queue.html
  2. Boost C++ Libraries. Boost.Heap: binomial_heap. www.boost.org/doc/libs/release/doc/html/boost/heap/binomial_heap.html
  3. Spitzner, L. et al. pqueue: reliable, persistent, fast priority queues. Hackage. hackage.haskell.org/package/pqueue
  4. Cormen, T. H., Leiserson, C. E., Rivest, R. L. & Stein, C. Introduction to Algorithms, 3rd ed., Chapter 19: Binomial Heaps (online chapter). sites.math.rutgers.edu/~ajl213/CLRS/Ch19.pdf

Enjoyed this chapter? Support The Ledger.