How a binomial heap merges in logarithmic time

Short answer: A binomial heap is a forest of binomial trees, at most one tree of each order, like the binary representation of the heap’s size. Two trees of equal order link in constant time by hanging the root with the larger key under the other. Merging two heaps is the same carry process as adding binary numbers and takes O(log n). Insert is a merge with a one-node heap. Extract-min finds the smallest root, removes that tree, and merges its children back.


What the problem actually asks

A binary heap merges poorly: concatenating two arrays and heapifying is linear in the larger heap. A binomial heap keeps the merge proportional to the number of trees, which is logarithmic. The binomial tree of order k has 2^k nodes, a root, and children that are binomial trees of orders k−1, …, 0. That shape is exactly what the link of two order-k trees produces.

The forest invariant says the orders are unique. It is the binary representation, and the merge is the addition.


What the algorithm guarantees

Find-min is O(log n) if the roots are scanned, or O(1) if a pointer to the minimum root is cached and maintained. Insert and merge are O(log n). Extract-min is O(log n). Decrease-key is O(log n), by sifting up the tree and possibly updating the minimum pointer. Extra memory is a pointer per node. The structure is pointer-based, unlike a binary heap’s array, so it loses locality and wins on merge.

Amortized constant insert is available by postponing the carry, and that version starts to resemble a Fibonacci heap. The strict binomial invariant, with the carry done immediately, keeps every operation logarithmic in the worst case. That worst-case merge is the reason to choose this heap over a binary heap.


The working steps

  1. Represent each binomial tree by a root with a list of child roots, stored in decreasing order of child rank, and a rank integer.
  2. Link two trees of equal rank by making the root with the larger key a child of the other and incrementing the winner’s rank.
  3. Merge two heaps by walking their roots in rank order. When two ranks match, link them and carry the result into the next rank, as in binary addition.
  4. Insert a key by merging a new rank-0 tree into the forest.
  5. Extract the minimum by removing that root from the forest and merging its children, which are themselves a binomial heap, back into the forest.

The logarithmic merge is the feature a binary heap does not have. Decrease-key is still logarithmic, so Dijkstra’s asymptotic bound does not improve. A Fibonacci heap is the structure that makes decrease-key amortized constant and makes the theoretical Dijkstra bound O(E + V log V). Binomial heaps are what you use when merges must be fast and the implementation must stay worst-case logarithmic.


Approaches that look simpler and fail

Approach Why it fails when two trees of equal rank are both kept
Leave two roots of equal rank in the forest The next merge’s carry is ambiguous, and the O(log n) tree count is lost. Equal ranks must link.
Hang the smaller key under the larger key The root would no longer be the minimum of its tree. Find-min and extract-min then miss it.
Quote amortized O(1) decrease-key That bound is the Fibonacci heap’s. A binomial decrease-key sifts up a path of length O(log n).
Store the forest in an array and expect a binary heap’s locality plus this merge The link operation rewires children. An array binary heap does not merge in logarithmic time by this carry.

An array priority queue belongs to the binary heap. An amortized constant decrease-key belongs to the Fibonacci heap. The binomial heap is the mergeable heap with a worst-case logarithmic bound.


A working example

This Python fragment merges two binomial forests the way binary addition merges bits: equal ranks are linked and carried. Extract-min removes the smallest root and merges its children back. Keys 4, 1, 3, 2 come out as 1 2 3 4.

class Tree:
    def __init__(self, key):
        self.key = key
        self.rank = 0
        self.children = []

def link(a, b):
    if a.key > b.key:
        a, b = b, a
    a.children.insert(0, b)
    a.rank += 1
    return a

def merge(left, right):
    forest = left + right
    while True:
        forest.sort(key=lambda t: t.rank)
        out = []
        changed = False
        for tree in forest:
            if out and out[-1].rank == tree.rank:
                tree = link(out.pop(), tree)
                changed = True
            out.append(tree)
        forest = out
        if not changed:
            return forest

def insert(forest, key):
    return merge(forest, [Tree(key)])

def extract_min(forest):
    index = min(range(len(forest)), key=lambda i: forest[i].key)
    node = forest.pop(index)
    return node.key, merge(forest, node.children)

forest = []
for key in (4, 1, 3, 2):
    forest = insert(forest, key)
order = []
while forest:
    key, forest = extract_min(forest)
    order.append(key)
assert order == [1, 2, 3, 4]
print(*order)

Where it is used

A binomial heap is a theoretical mergeable priority queue. Production code almost always uses a binary heap. The logarithmic merge is why the structure appears in the analysis of some network algorithms, not because a library container is a binomial heap.


When you can drop this algorithm

Drop a binomial heap when the queue never merges and never needs decrease-key handles beyond a binary heap’s. The array heap is faster. Keep the binomial heap when two heaps must be merged in O(log n) worst-case time.


Why this stays published

We publish this so a search for a binomial heap returns the binary-carry merge and the logarithmic bound it actually has. A binomial heap merges two priority queues in O(log n) time by linking equal-rank binomial trees the way binary addition handles a carry.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *