Short answer: A B-tree of minimum degree t stores between t−1 and 2t−1 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 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.
What the problem actually asks
A binary tree of n keys has height log₂ 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.
The half-full rule is what forces splits and merges. A page with 2t−1 keys cannot accept another. A page with t−1 keys cannot give one away without a repair.
What the algorithm guarantees
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.
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.
The working steps
- 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.
- 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−1 keys.
- 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.
- On delete, if the key is internal, replace it with its predecessor or successor from a leaf and delete that copy.
- If a child would fall below t−1 keys, borrow from a sibling or merge with a sibling and pull the separating parent key down.
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.
Approaches that look simpler and fail
| Approach | Why it fails when a node is allowed to grow without a split |
|---|---|
| Append keys to one leaf forever | 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. |
| Split a node and leave the median in both children | The separator belongs in the parent exactly once. Duplicating it breaks the bracket invariant. |
| Borrow a key without moving the parent separator | The parent key is the fence between two siblings. A rotation has to slide that fence, or a search descends into the wrong child. |
| Use a binary search tree and call each node a page | The height is log₂ n page reads. The large fanout is the reason the B-tree exists. |
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.
A working example
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 1 2 3 4 5 6 7.
T = 2
class Node:
def __init__(self, leaf=True):
self.keys, self.child, self.leaf = [], [], leaf
def split(parent, i):
node = parent.child[i]
newer = Node(node.leaf)
median = node.keys[T - 1]
newer.keys = node.keys[T:]
node.keys = node.keys[: T - 1]
if not node.leaf:
newer.child = node.child[T:]
node.child = node.child[:T]
parent.keys.insert(i, median)
parent.child.insert(i + 1, newer)
def insert_nonfull(node, key):
i = len(node.keys) - 1
if node.leaf:
node.keys.append(None)
while i >= 0 and key < node.keys[i]:
node.keys[i + 1] = node.keys[i]
i -= 1
node.keys[i + 1] = key
return
while i >= 0 and key < node.keys[i]:
i -= 1
i += 1
if len(node.child[i].keys) == 2 * T - 1:
split(node, i)
if key > node.keys[i]:
i += 1
insert_nonfull(node.child[i], key)
def insert(root, key):
if len(root.keys) == 2 * T - 1:
fresh = Node(False)
fresh.child = [root]
split(fresh, 0)
root = fresh
insert_nonfull(root, key)
return root
def inorder(node, out):
if node.leaf:
out.extend(node.keys)
return
for i, key in enumerate(node.keys):
inorder(node.child[i], out)
out.append(key)
inorder(node.child[-1], out)
def depths(node, d, acc):
if node.leaf:
acc.append(d)
else:
for child in node.child:
depths(child, d + 1, acc)
root = Node()
for key in range(1, 8):
root = insert(root, key)
keys = []
inorder(root, keys)
leaves = []
depths(root, 0, leaves)
assert keys == list(range(1, 8)) and len(set(leaves)) == 1
print(*keys)
Where it is used
PostgreSQL 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.
When you can drop this algorithm
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.
Why this stays published
We publish this so a search for a B-tree returns the occupancy window, the upward split, and the equal leaf depth. 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.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
