{"id":115,"date":"2026-11-13T12:31:46","date_gmt":"2026-11-13T04:31:46","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-a-b-tree-stores-keys-in-a-linked-leaf-level\/"},"modified":"2026-11-13T21:50:39","modified_gmt":"2026-11-13T13:50:39","slug":"how-a-b-tree-stores-keys-in-a-linked-leaf-level","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-a-b-tree-stores-keys-in-a-linked-leaf-level\/","title":{"rendered":"How a B+ tree stores keys in a linked leaf level"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> A <strong>B+ tree<\/strong> keeps every record in the leaves and copies separator keys into internal nodes that act only as an index. Leaves are linked in key order, so a range scan walks that chain and never returns to the index. Occupancy rules match a B-tree, and the height is <strong>O(log_t n)<\/strong>. Internal nodes can pack more separators than a B-tree packs records, because they store keys and child pointers, not payloads.<\/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 B-tree range scan has to traverse internal nodes in order, revisiting pages that hold both a routing key and a record. A database wants one leaf page to stream the next hundred records without another index read. Putting every record in the leaves, and storing a sideways pointer from each leaf to the next, makes that scan sequential.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The internal key is then a separator, and it may be a copy of a leaf key. Deleting the leaf record does not have to delete every copy above it immediately. The separator remains a valid bracket as long as the keys on its left stay smaller and the keys on its right stay greater.<\/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\">Point search is O(log_t n) node reads and always ends at a leaf. A range of k records after that leaf costs O(log_t n) to find the start plus O(k\/t) leaf reads to scan, because each leaf holds \u0398(t) records when pages are full enough. Insert and delete follow the B-tree split and merge, applied to leaves that hold records and to internal nodes that hold copies. Extra memory is the leaf links.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The structure is the usual clustered or secondary index inside a relational database. It is not a hash index. Equality and range both work. A hash index answers equality and does not produce the leaf chain.<\/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 from the root, using separator keys only to choose a child. Do not stop at an internal key that equals the target. Continue to a leaf.<\/li>\n<li>Inside the leaf, binary-search the records. The leaf&#8217;s right sibling holds the next greater keys.<\/li>\n<li>Insert into the leaf. If the leaf overflows, split it, copy the smallest key of the right leaf up as a separator, and link the two leaves.<\/li>\n<li>Delete from the leaf. If the leaf underflows, borrow or merge as in a B-tree, and update a separator only when a merge or a borrow changes the smallest key the separator must name.<\/li>\n<li>A range query searches for the lower end and then follows leaf links until the keys pass the upper end.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The range bound depends on the leaf chain. A B-tree without leaf links can answer the same range and pays extra index traffic. Neither structure is a column store or a hash. Internal separators are allowed to be stale copies only within the bracket rule. A separator that is greater than some key in its left subtree sends searches the wrong way.<\/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 an internal hit is treated as the record<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Return the internal node when its separator equals the key<\/td>\n<td>The record lives in a leaf. The internal copy is a routing key and may not carry the payload.<\/td>\n<\/tr>\n<tr>\n<td>Split a leaf and forget the sideways link<\/td>\n<td>The range scan stops at the hole or skips a leaf. The link is part of the split.<\/td>\n<\/tr>\n<tr>\n<td>Delete a separator because the leaf key was deleted, without a replacement bracket<\/td>\n<td>The parent still needs a key that separates the left and right subtrees. Replace the separator, or merge the children.<\/td>\n<\/tr>\n<tr>\n<td>Scan a range by returning to the root for every successor<\/td>\n<td>That search is correct and wastes the leaf chain, which exists so the scan stays on the leaf level.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">A paged tree that stores records in internal nodes is a B-tree. An in-memory ordered dictionary is a red-black tree. The B+ tree is the index whose range scan walks leaves.<\/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 keeps records only in the leaves and follows a separator all the way down. A full leaf splits, and the smallest key of the new right leaf is copied upward. Leaves stay linked, so the range from 3 through 5 walks that chain and prints <code>3 4 5<\/code>.<\/p>\n\n<pre><code class=\"language-python\">MAX = 2\n\nclass Node:\n    def __init__(self, leaf):\n        self.leaf = leaf\n        self.keys = []\n        self.kids = []\n        self.nxt = None\n\ndef find_leaf(node, key):\n    while not node.leaf:\n        i = 0\n        while i &lt; len(node.keys) and key &gt;= node.keys[i]:\n            i += 1\n        node = node.kids[i]\n    return node\n\ndef insert(root, key):\n    if root is None:\n        root = Node(True)\n        root.keys.append(key)\n        return root\n    sep, right = insert_into(root, key)\n    if right is None:\n        return root\n    top = Node(False)\n    top.keys = [sep]\n    top.kids = [root, right]\n    return top\n\ndef insert_into(node, key):\n    if node.leaf:\n        i = 0\n        while i &lt; len(node.keys) and node.keys[i] &lt; key:\n            i += 1\n        node.keys.insert(i, key)\n        if len(node.keys) &lt;= MAX:\n            return None, None\n        right = Node(True)\n        mid = len(node.keys) \/\/ 2\n        right.keys = node.keys[mid:]\n        node.keys = node.keys[:mid]\n        right.nxt = node.nxt\n        node.nxt = right\n        return right.keys[0], right\n    i = 0\n    while i &lt; len(node.keys) and key &gt;= node.keys[i]:\n        i += 1\n    sep, right = insert_into(node.kids[i], key)\n    if right is None:\n        return None, None\n    node.keys.insert(i, sep)\n    node.kids.insert(i + 1, right)\n    if len(node.keys) &lt;= MAX:\n        return None, None\n    sibling = Node(False)\n    mid = len(node.keys) \/\/ 2\n    up = node.keys[mid]\n    sibling.keys = node.keys[mid + 1:]\n    sibling.kids = node.kids[mid + 1:]\n    node.keys = node.keys[:mid]\n    node.kids = node.kids[: mid + 1]\n    return up, sibling\n\nroot = None\nfor key in (1, 2, 3, 4, 5, 6, 7):\n    root = insert(root, key)\nleaf = find_leaf(root, 3)\ngot = []\nwhile leaf is not None and (not got or got[-1] &lt;= 5):\n    for key in leaf.keys:\n        if 3 &lt;= key &lt;= 5:\n            got.append(key)\n    leaf = leaf.nxt\nassert got == [3, 4, 5]\nassert find_leaf(root, 4).leaf\nprint(*got)\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>InnoDB<\/strong> stores a table in a clustered B+ tree and walks the linked leaves for a range scan. The internal nodes hold keys only, which is why a range does not climb back to the root between neighbors. A plain B-tree keeps records in the internal nodes as well.<\/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 pure equality workload that fits in a hash table. Keep it when the data are paged and queries include ranges, prefixes, or ordered scans.<\/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 leaf chain and the rule that separators are copies, not the records. <strong>A B+ tree stores every record in linked leaves and uses internal nodes as separators, so a range scan walks the leaf chain after one root-to-leaf search.<\/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 keeps every record in the leaves and copies separator keys into internal nodes that act only as an index. Leaves are linked in key order, so a range scan walks that chain and never returns to the index. Occupancy rules match a B-tree, and&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-115","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\/115","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=115"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/115\/revisions"}],"predecessor-version":[{"id":514,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/115\/revisions\/514"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}