B-TREE
Fat, shallow nodes that grow from the root, one page read per level
A B-tree with minimum degree t = 2 allows at most 2t − 1 = 3 sorted keys per node. Right now it is a single full leaf root [10 20 30]. You insert 40. What happens?
A B-tree of minimum degree t keeps between t−1 and 2t−1 sorted keys in each node, and each node fills one disk page. Search reads one node per level, so a shallow tree means few reads.
The insert taught here is CLRS’s one-pass insert, and it never lets a node overflow. Walking down from the root, any full child (2t−1 keys) is split before you enter it: its median moves up into the parent, and its two halves become siblings. If the root itself is full, it splits and a new root holds the median. That root split is the only way the tree grows taller, so every leaf stays at the same depth. Production B+trees such as PostgreSQL’s nbtree and InnoDB split bottom-up instead: they insert into the leaf and split it only when it overflows, then push the separator up. The tree still grows only at the root.
B-tree of minimum degree t
every node: at most 2t-1 keys (it must fit one page)
every non-root node: at least t-1 keys
internal node with k keys has k+1 children
all leaves at the same depth
INSERT(T, k): // CLRS one pass, top-down
if root has 2t-1 keys:
s = new node; s.children = [root]
SPLIT-CHILD(s, 0); root = s // only way height grows
x = root
while x is not a leaf:
i = child index for k
if x.children[i] has 2t-1 keys:
SPLIT-CHILD(x, i) // split BEFORE descending
if k > x.keys[i]: i++
x = x.children[i]
insert k into leaf x // guaranteed room
SPLIT-CHILD(x, i): // y = x.children[i], full
z.keys = y.keys[t .. 2t-2] // right t-1 keys
z.children = y.children[t .. 2t-1] // right t children (if internal)
median = y.keys[t-1] // moves up
y.keys = y.keys[0 .. t-2] // left t-1 keys
y.children = y.children[0 .. t-1]
insert median at x.keys[i], z at x.children[i+1]
Height <= log_t((n+1)/2) + 1; one page read per level. Watch the invariant strip and the depth rows. CLRS INSERT splits the root at 40 and again at 90, and every node stays at 3 keys or fewer. NAIVE appends every key to one leaf: the 4th key breaks every node has ≤ 2t−1 keys and it never recovers.
Each node is one disk page, so each level is one read. Change the minimum degree t until the nine inserts leave a tree where finding 90 costs at most 2 page reads.
GOAL: search 90 reads ≤ 2 pages, every invariant holds
This split works on leaves. The grader inserts sorted, reversed and seeded-random keys for t = 2, 3 and 4 and checks node sizes, child counts, leaf depth, key order and that no key is lost. Find what goes wrong the first time an internal node splits.
The on-disk indexes of PostgreSQL and MySQL InnoDB (B+tree variants that keep values only in leaves) and SQLite (B+trees for rowid tables, plain B-trees for indexes), and filesystems such as NTFS, HFS+, XFS and Btrfs. Page splits show up as write amplification and index bloat after random-key inserts.
“Why do databases use B-trees and not binary trees?” Talk about page reads per level and fan-out, walk through a split and a root split, and say what the B+tree variant changes (values only in leaves, leaves linked for range scans).