B+ TREE

The index behind almost every relational database

L215 MINDATABASES
PREDICT

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.

▶ SIMULATION
B+ TREE · FANOUT 4
keys 0 · max keys/node 3
leaf splits 0 · internal 0 · root 0
—
NODES BY LEVEL
leaves (depth 1): []
QUERY
leaf depth 1
lookup: —
leaf chain— (1 leaves)
✓ all leaves at the same depth✓ keys in order across leaves, none lost
SCHEDULE
01[LOAD]insert 16 keys, 5 … 60
02[QUERY]lookup 42: descend from the root to a leaf
03[QUERY]range scan 42..50: follow leaf next pointers
04[QUERY]report
Start of lookup + range scan scenario. 4 steps.
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.

BREAK IT NOT YET

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

FIX IT NOT YET

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.

Mental model:

a B+ tree is a wide, shallow tree that only grows at the root. A full node splits in half and pushes one key up, so every leaf is always at the same depth and every lookup costs the same number of page reads. Linked leaves make range scans one descent plus a sequential walk.

IN PRODUCTION

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.

IN INTERVIEWS

“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.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.
NEXT: LSM TREE →