B-TREE

Fat, shallow nodes that grow from the root, one page read per level

L215 MINALGORITHMS
PREDICT

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.

▶ SIMULATION
LAST STEP
empty tree
t = 2
keys per node: 1..3
largest node: 0 keys
height 1 · 0 keys
SPLITS
node splits 0
root splits 0
search 90: —
root[ ]
✓ every node has ≤ 2t−1 keys✓ every non-root node has ≥ t−1 keys✓ all leaves at the same depth✓ in-order walk is sorted
SCHEDULE
01[INSERT]insert 10
02[INSERT]insert 20
03[INSERT]insert 30
04[INSERT]insert 40
05[INSERT]insert 50
06[INSERT]insert 60
07[INSERT]insert 70
08[INSERT]insert 80
09[INSERT]insert 90
10[SEARCH]search 90
11[CHECK]check every invariant
Start of clrs insert: split on the way down scenario. 11 steps.
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.

TUNE IT NOT YET

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

FIX IT NOT YET

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.

Mental model:

a B-tree is a page-sized node repeated a few levels deep. Insert keeps every node within its page by splitting full nodes on the way down, pushing the median up. The tree grows only at the root, which is why all leaves are always at the same depth and why a lookup costs one read per level.

IN PRODUCTION

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.

IN INTERVIEWS

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

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