B+ TREE
The index behind almost every relational database
A B+ tree has 3 levels. An insert overflows a leaf, so the leaf splits in two and pushes a key up to its parent. The parent overflows and splits the same way, and so on up, until the root splits. What happens to the depth of the leaves?
A B+ tree keeps row data only in its leaves. Internal nodes hold keys and child pointers, so a page fits hundreds of them. A lookup reads one node per level, root to leaf. The leaves are chained, so a range scan descends once and then walks sideways.
Inserts go into a leaf. When a leaf overflows, it splits into two halves and copies the right half’s first key up to its parent. If the parent overflows, it splits and moves its middle key up. When the root splits, a new root appears above it. The tree only ever grows at the top, so every leaf stays at the same depth.
B+ tree (fanout F: at most F children, F-1 keys per node)
===================================================
Structure
internal nodes: keys + child pointers only (no row data)
leaves: keys + row data (or row ids), linked by next pointers
every leaf sits at the same depth
Lookup(K)
start at root; pick the child whose range holds K; repeat to a leaf
cost: one page read per level = height
Range(lo, hi)
lookup(lo), then follow leaf next pointers until a key > hi
Insert(K)
1. find the leaf, insert K in sorted position
2. leaf overfull? split into left | right,
COPY right[0] up to the parent as the separator
3. parent overfull? split it, MOVE its middle key up
4. root split? make a new root: the tree grows by one level,
at the top, so all leaves stay at the same depth
Why it stays shallow
fanout 500, 3 levels -> ~125 million keys, 3 page reads
balanced binary tree -> ~27 levels for the same keys
unbalanced binary tree, sorted inserts -> one level per key Watch the leaf-depth invariant. In B+ TREE: SORTED INSERTS, keys arrive almost in ascending order (only 10 comes out of place, after 30) and the tree splits its way to every leaf at depth 2. The same keys into NO SPLITS: BINARY TREE build a chain, and the invariant turns red at the fourth insert.
The same seven sorted inserts run at every fanout. At FANOUT 4 the root splits once and the tree stops at 2 levels. Change FANOUT until the root has to split a second time and the tree grows to 3 levels.
GOAL: the seven inserts grow the tree to 3 levels
Implement leaf insertion. leaf is a sorted array of keys. Insert key in order. If the leaf now has more than maxKeys keys, split it into two non-empty sorted halves and return { left, right, separator }, where separator is the key copied up to the parent: every key in left is below it and every key in right is at or above it. Otherwise return the leaf array.
Every InnoDB table (the clustered primary key is a B+ tree), PostgreSQL btree indexes, SQLite tables (SQLite indexes are plain B-trees, with keys in interior pages too), and directory indexes in XFS and NTFS. Symptoms you will see: page splits under random-key inserts (UUIDv4 primary keys) and index bloat after mass deletes.
“How does a database index work?” and “Why a B+ tree and not a hash table or a binary tree?” Walk a lookup, explain a leaf split and a root split, say why fanout keeps height at 3-4, and mention that linked leaves make range queries and ORDER BY cheap where a hash index cannot.