{"id":117,"date":"2026-11-13T12:31:49","date_gmt":"2026-11-13T04:31:49","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-a-binomial-heap-merges-in-logarithmic-time\/"},"modified":"2026-11-13T21:50:42","modified_gmt":"2026-11-13T13:50:42","slug":"how-a-binomial-heap-merges-in-logarithmic-time","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-a-binomial-heap-merges-in-logarithmic-time\/","title":{"rendered":"How a binomial heap merges in logarithmic time"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> A <strong>binomial heap<\/strong> is a forest of <strong>binomial trees<\/strong>, at most one tree of each order, like the binary representation of the heap&#8217;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 <strong>O(log n)<\/strong>. Insert is a merge with a one-node heap. Extract-min finds the smallest root, removes that tree, and merges its children back.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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\u22121, \u2026, 0. That shape is exactly what the link of two order-k trees produces.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The forest invariant says the orders are unique. It is the binary representation, and the merge is the addition.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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&#8217;s array, so it loses locality and wins on merge.<\/p>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Represent each binomial tree by a root with a list of child roots, stored in decreasing order of child rank, and a rank integer.<\/li>\n<li>Link two trees of equal rank by making the root with the larger key a child of the other and incrementing the winner&#8217;s rank.<\/li>\n<li>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.<\/li>\n<li>Insert a key by merging a new rank-0 tree into the forest.<\/li>\n<li>Extract the minimum by removing that root from the forest and merging its children, which are themselves a binomial heap, back into the forest.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The logarithmic merge is the feature a binary heap does not have. Decrease-key is still logarithmic, so Dijkstra&#8217;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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when two trees of equal rank are both kept<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Leave two roots of equal rank in the forest<\/td>\n<td>The next merge&#8217;s carry is ambiguous, and the O(log n) tree count is lost. Equal ranks must link.<\/td>\n<\/tr>\n<tr>\n<td>Hang the smaller key under the larger key<\/td>\n<td>The root would no longer be the minimum of its tree. Find-min and extract-min then miss it.<\/td>\n<\/tr>\n<tr>\n<td>Quote amortized O(1) decrease-key<\/td>\n<td>That bound is the Fibonacci heap&#8217;s. A binomial decrease-key sifts up a path of length O(log n).<\/td>\n<\/tr>\n<tr>\n<td>Store the forest in an array and expect a binary heap&#8217;s locality plus this merge<\/td>\n<td>The link operation rewires children. An array binary heap does not merge in logarithmic time by this carry.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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 <code>1 2 3 4<\/code>.<\/p>\n\n<pre><code class=\"language-python\">class Tree:\n    def __init__(self, key):\n        self.key = key\n        self.rank = 0\n        self.children = []\n\ndef link(a, b):\n    if a.key &gt; b.key:\n        a, b = b, a\n    a.children.insert(0, b)\n    a.rank += 1\n    return a\n\ndef merge(left, right):\n    forest = left + right\n    while True:\n        forest.sort(key=lambda t: t.rank)\n        out = []\n        changed = False\n        for tree in forest:\n            if out and out[-1].rank == tree.rank:\n                tree = link(out.pop(), tree)\n                changed = True\n            out.append(tree)\n        forest = out\n        if not changed:\n            return forest\n\ndef insert(forest, key):\n    return merge(forest, [Tree(key)])\n\ndef extract_min(forest):\n    index = min(range(len(forest)), key=lambda i: forest[i].key)\n    node = forest.pop(index)\n    return node.key, merge(forest, node.children)\n\nforest = []\nfor key in (4, 1, 3, 2):\n    forest = insert(forest, key)\norder = []\nwhile forest:\n    key, forest = extract_min(forest)\n    order.append(key)\nassert order == [1, 2, 3, 4]\nprint(*order)\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop a binomial heap when the queue never merges and never needs decrease-key handles beyond a binary heap&#8217;s. The array heap is faster. Keep the binomial heap when two heaps must be merged in O(log n) worst-case time.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for a binomial heap returns the binary-carry merge and the logarithmic bound it actually has. <strong>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.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;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&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,35],"tags":[],"class_list":["post-117","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-heaps"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/117","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=117"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/117\/revisions"}],"predecessor-version":[{"id":516,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/117\/revisions\/516"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=117"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}