3.4.9 · D3Trees

Worked examples — B-tree and B+ tree — motivation (disk storage), properties

4,488 words20 min readBack to topic

The scenario matrix

Every situation a B/B+ tree problem can throw at you falls into one of these cells. Each worked example below is tagged with the cell(s) it covers.

# Case class The tricky thing Covered by
A Degenerate: empty tree first insert creates the root Ex 1
B Insert, no split node has room, just slot it in Ex 1
C Insert forces a leaf split median rises one level Ex 2
D Insert forces the ROOT to split tree grows taller (only way!) Ex 2
E Cascading split a split makes the parent full too Ex 3
F Delete, no underflow plenty of keys, easy Ex 4
G Delete causes underflow → borrow steal a key from a sibling Ex 4
H Delete causes underflow → merge no sibling to lend → fuse + shrink Ex 5
M Delete from an INTERNAL node replace by predecessor/successor Ex 6
N Cascading merge (deep underflow) merge makes the parent underflow too Ex 7
I Limiting case: height bound how tall can it get / disk-read count Ex 8
J B+ range query (word problem) walk the linked leaves Ex 9
K Exam twist: choose from block size fit one node into one disk block Ex 10
L B vs B+ insertion + contrast B+ splits & where the data lives Ex 11

Prerequisite links if any cell feels shaky: Binary Search Tree, Big-O Notation, Disk and Memory Hierarchy.


Worked examples

Example 1 — Cells A & B: empty tree, then painless inserts

Figure s01 shows the single root node filling up one key at a time — trace the blue node growing left to right, and note the last frame is drawn in pale yellow to flag "full but still legal".

Figure — B-tree and B+ tree — motivation (disk storage), properties

Example 2 — Cells C & D: the insert that splits the root (tree grows taller)

In figure s02 the pink node on the left is the overfull node; follow the yellow arrow across to the split result — the promoted median 10 sits in the yellow root, its two blue children hang below at one common depth.

Figure — B-tree and B+ tree — motivation (disk storage), properties

Example 3 — Cell E: a cascading split


Example 4 — Cells F & G: delete with no underflow, then delete with a borrow


Example 5 — Cell H: delete forcing a merge (tree shrinks)

Figure s03: on the left, the pink node is the leaf that emptied and its min-sized sibling; the yellow arrow points to the fused result where separator 10 has dropped in to form the new single root.

Figure — B-tree and B+ tree — motivation (disk storage), properties

Example 6 — Cell M: deleting a key that lives in an INTERNAL node

Figure s04 shows the swap: the pink separator 15 in the root is crossed out, the blue predecessor 10 is lifted from the left leaf along the yellow arrow into the root, and the leaf shrinks to .

Figure — B-tree and B+ tree — motivation (disk storage), properties

Example 7 — Cell N: a cascading merge (underflow that propagates up)

Figure s05: the pink path traces the delete of 35 causing to empty; the yellow arrows show separator 30 dropping into , key 20 rising to the root, and the orphan leaf sliding across to .

Figure — B-tree and B+ tree — motivation (disk storage), properties

Example 8 — Cell I: limiting case — how tall can it get, how many disk reads?

Figure — B-tree and B+ tree — motivation (disk storage), properties

Example 9 — Cell J: B+ tree range query (word problem)


Example 10 — Cell K: exam twist — pick from the disk block size


Example 11 — Cell L: B+ tree insertion (with a leaf split) vs the B-tree on the same keys


Recall Quick self-test (cover the answers)

Why is forbidden? ::: Then min keys and max ; nodes could be empty and there is no median to promote — the algorithms break. Empty tree, first insert 42 with — what is the tree? ::: A single root node ; the first key always becomes the root. , splitting the four values — which rises, by what rule? ::: The lower median, the -th (2nd) smallest . A B-tree's height only increases when… ::: the root splits (growth happens at the top → all leaves stay level). Deleting a key that sits in an internal node — what replaces it? ::: Its predecessor (largest of left subtree) or successor (smallest of right subtree), then delete that copy from the leaf. A leaf merge empties its parent internal node — then what? ::: The underflow cascades up: borrow from an internal sibling if possible, else merge again (possibly shrinking the tree). , — worst-case disk reads per lookup? ::: , since . On a B+ leaf split, is the median moved or copied up? ::: Copied — the record must stay in a leaf.