{"id":114,"date":"2026-11-13T12:31:43","date_gmt":"2026-11-13T04:31:43","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-a-b-tree-keeps-every-leaf-at-one-depth\/"},"modified":"2026-11-13T21:50:38","modified_gmt":"2026-11-13T13:50:38","slug":"how-a-b-tree-keeps-every-leaf-at-one-depth","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-a-b-tree-keeps-every-leaf-at-one-depth\/","title":{"rendered":"How a B-tree keeps every leaf at one depth"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> A <strong>B-tree<\/strong> of minimum degree t stores between t\u22121 and 2t\u22121 keys in every node except the root, and every leaf sits at the same depth. Search follows one child pointer per node, so a tree of n keys has height <strong>O(log_t n)<\/strong>. Insert splits a full node on the way down, or on the way up, and delete merges or rotates with a sibling. The structure is the paged dictionary behind many filesystems and databases, where one node is one disk page.<\/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 tree of n keys has height log\u2082 n and one comparison per level, which is the right shape in RAM and the wrong shape on disk. A disk wants a node the size of a page, holding hundreds of keys, so that a search pays a handful of page reads. The B-tree invariant keeps every page at least half full, so the height stays logarithmic in n with a large base, and it keeps every leaf at the same depth, so no search walks a long chain while another finishes at the root.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The half-full rule is what forces splits and merges. A page with 2t\u22121 keys cannot accept another. A page with t\u22121 keys cannot give one away without a repair.<\/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\">Search, insert, and delete touch O(log_t n) nodes. With t in the hundreds, that is a few I\/Os for any table that fits on a machine. Extra memory is the pages themselves. All leaves at equal depth plus the occupancy bounds imply the height bound. Keys inside a node are sorted, and the child between two keys holds the keys that lie strictly between them.<\/p>\n\n\n<p class=\"wp-block-paragraph\">A split of a full root grows the tree upward, which is how the depth stays uniform. Deletion at a leaf may borrow a key from a sibling through the parent, or merge with a sibling when both are at the minimum. Merging the last two children of the root shrinks the height. B+ trees move the data records into the leaves and chain those leaves. A B-tree may store records in internal nodes.<\/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>Search by binary search inside a node, then follow the child whose separating keys bracket the target. Stop at a leaf or at an internal hit, according to the variant.<\/li>\n<li>On insert, if the implementation splits full nodes on the way down, split any full node before descending into it, and insert into a leaf that is guaranteed not to be full. Otherwise insert into the leaf and split on the way up when a node exceeds 2t\u22121 keys.<\/li>\n<li>A split median moves up into the parent. The left half and the right half become two children. A full root splits into a new root with one key.<\/li>\n<li>On delete, if the key is internal, replace it with its predecessor or successor from a leaf and delete that copy.<\/li>\n<li>If a child would fall below t\u22121 keys, borrow from a sibling or merge with a sibling and pull the separating parent key down.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The I\/O bound assumes a node is a page and t is large. A B-tree with t = 2 is a small balanced tree, correct and pointless on disk. Binary search trees are the in-memory alternative. A B+ tree is the variant that keeps internal nodes as a pure index and links the leaves for range scans.<\/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 a node is allowed to grow without a split<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Append keys to one leaf forever<\/td>\n<td>The leaf is no longer a page, and the equal-depth rule does not by itself bound the work inside a node. The occupancy cap is the split.<\/td>\n<\/tr>\n<tr>\n<td>Split a node and leave the median in both children<\/td>\n<td>The separator belongs in the parent exactly once. Duplicating it breaks the bracket invariant.<\/td>\n<\/tr>\n<tr>\n<td>Borrow a key without moving the parent separator<\/td>\n<td>The parent key is the fence between two siblings. A rotation has to slide that fence, or a search descends into the wrong child.<\/td>\n<\/tr>\n<tr>\n<td>Use a binary search tree and call each node a page<\/td>\n<td>The height is log\u2082 n page reads. The large fanout is the reason the B-tree exists.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">A leaf-linked index for range scans is a B+ tree. An in-memory ordered map is a red-black tree. The B-tree is the paged balanced search tree with both internal and leaf occupancy rules.<\/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 is a B-tree of minimum degree 2. A full node of three keys splits around its median before the walk descends. After inserting 1 through 7 every key is found and every leaf sits at one depth. It prints the inorder keys <code>1 2 3 4 5 6 7<\/code>.<\/p>\n\n<pre><code class=\"language-python\">T = 2\n\nclass Node:\n    def __init__(self, leaf=True):\n        self.keys, self.child, self.leaf = [], [], leaf\n\ndef split(parent, i):\n    node = parent.child[i]\n    newer = Node(node.leaf)\n    median = node.keys[T - 1]\n    newer.keys = node.keys[T:]\n    node.keys = node.keys[: T - 1]\n    if not node.leaf:\n        newer.child = node.child[T:]\n        node.child = node.child[:T]\n    parent.keys.insert(i, median)\n    parent.child.insert(i + 1, newer)\n\ndef insert_nonfull(node, key):\n    i = len(node.keys) - 1\n    if node.leaf:\n        node.keys.append(None)\n        while i &gt;= 0 and key &lt; node.keys[i]:\n            node.keys[i + 1] = node.keys[i]\n            i -= 1\n        node.keys[i + 1] = key\n        return\n    while i &gt;= 0 and key &lt; node.keys[i]:\n        i -= 1\n    i += 1\n    if len(node.child[i].keys) == 2 * T - 1:\n        split(node, i)\n        if key &gt; node.keys[i]:\n            i += 1\n    insert_nonfull(node.child[i], key)\n\ndef insert(root, key):\n    if len(root.keys) == 2 * T - 1:\n        fresh = Node(False)\n        fresh.child = [root]\n        split(fresh, 0)\n        root = fresh\n    insert_nonfull(root, key)\n    return root\n\ndef inorder(node, out):\n    if node.leaf:\n        out.extend(node.keys)\n        return\n    for i, key in enumerate(node.keys):\n        inorder(node.child[i], out)\n        out.append(key)\n    inorder(node.child[-1], out)\n\ndef depths(node, d, acc):\n    if node.leaf:\n        acc.append(d)\n    else:\n        for child in node.child:\n            depths(child, d + 1, acc)\n\nroot = Node()\nfor key in range(1, 8):\n    root = insert(root, key)\nkeys = []\ninorder(root, keys)\nleaves = []\ndepths(root, 0, leaves)\nassert keys == list(range(1, 8)) and len(set(leaves)) == 1\nprint(*keys)\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\"><strong>PostgreSQL<\/strong> B-tree indexes and many filesystem catalogs are B-trees: every leaf sits at one depth so a disk lookup is a short root-to-leaf walk. The variant whose leaves are linked, used by InnoDB, is the B+ tree. An in-memory red-black tree is the wrong shape for a page-sized node.<\/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 B-tree for a small in-memory dictionary. A red-black tree or a hash table does less work per key. Keep a B-tree when the dictionary lives on pages and every lookup must cost a logarithmic number of page reads with a large base.<\/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 B-tree returns the occupancy window, the upward split, and the equal leaf depth. <strong>A B-tree keeps every node except the root between half full and full, and every leaf at one depth, so search, insert, and delete cost O(log_t n) page reads.<\/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 B-tree of minimum degree t stores between t\u22121 and 2t\u22121 keys in every node except the root, and every leaf sits at the same depth. Search follows one child pointer per node, so a tree of n keys has height O(log_t n). Insert splits a full&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,34],"tags":[],"class_list":["post-114","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-trees"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/114","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=114"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/114\/revisions"}],"predecessor-version":[{"id":513,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/114\/revisions\/513"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=114"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}