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 the height is O(log_t n). Internal nodes can pack more separators than a B-tree packs records, because they store keys and child pointers, not payloads.
What the problem actually asks
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.
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.
What the algorithm guarantees
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 Θ(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.
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.
The working steps
- 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.
- Inside the leaf, binary-search the records. The leaf’s right sibling holds the next greater keys.
- 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.
- 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.
- A range query searches for the lower end and then follows leaf links until the keys pass the upper end.
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.
Approaches that look simpler and fail
| Approach | Why it fails when an internal hit is treated as the record |
|---|---|
| Return the internal node when its separator equals the key | The record lives in a leaf. The internal copy is a routing key and may not carry the payload. |
| Split a leaf and forget the sideways link | The range scan stops at the hole or skips a leaf. The link is part of the split. |
| Delete a separator because the leaf key was deleted, without a replacement bracket | The parent still needs a key that separates the left and right subtrees. Replace the separator, or merge the children. |
| Scan a range by returning to the root for every successor | That search is correct and wastes the leaf chain, which exists so the scan stays on the leaf level. |
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.
A working example
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 3 4 5.
MAX = 2
class Node:
def __init__(self, leaf):
self.leaf = leaf
self.keys = []
self.kids = []
self.nxt = None
def find_leaf(node, key):
while not node.leaf:
i = 0
while i < len(node.keys) and key >= node.keys[i]:
i += 1
node = node.kids[i]
return node
def insert(root, key):
if root is None:
root = Node(True)
root.keys.append(key)
return root
sep, right = insert_into(root, key)
if right is None:
return root
top = Node(False)
top.keys = [sep]
top.kids = [root, right]
return top
def insert_into(node, key):
if node.leaf:
i = 0
while i < len(node.keys) and node.keys[i] < key:
i += 1
node.keys.insert(i, key)
if len(node.keys) <= MAX:
return None, None
right = Node(True)
mid = len(node.keys) // 2
right.keys = node.keys[mid:]
node.keys = node.keys[:mid]
right.nxt = node.nxt
node.nxt = right
return right.keys[0], right
i = 0
while i < len(node.keys) and key >= node.keys[i]:
i += 1
sep, right = insert_into(node.kids[i], key)
if right is None:
return None, None
node.keys.insert(i, sep)
node.kids.insert(i + 1, right)
if len(node.keys) <= MAX:
return None, None
sibling = Node(False)
mid = len(node.keys) // 2
up = node.keys[mid]
sibling.keys = node.keys[mid + 1:]
sibling.kids = node.kids[mid + 1:]
node.keys = node.keys[:mid]
node.kids = node.kids[: mid + 1]
return up, sibling
root = None
for key in (1, 2, 3, 4, 5, 6, 7):
root = insert(root, key)
leaf = find_leaf(root, 3)
got = []
while leaf is not None and (not got or got[-1] <= 5):
for key in leaf.keys:
if 3 <= key <= 5:
got.append(key)
leaf = leaf.nxt
assert got == [3, 4, 5]
assert find_leaf(root, 4).leaf
print(*got)
Where it is used
InnoDB 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.
When you can drop this algorithm
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.
Why this stays published
We publish this so a search for a B+ tree returns the leaf chain and the rule that separators are copies, not the records. 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.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
