3.4.9 · D4Trees

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

2,396 words11 min readBack to topic

L1 — Recognition

Recall Solution Q1

With : min keys (non-root) ; max keys ; a node with keys has children.

  • (a) 2 keys → equals the minimum . Legal.
  • (b) 5 keys → equals the maximum . Legal.
  • (c) 6 keys → exceeds max . Illegal (this node must split).
  • (d) 4 keys should have children, not 4. Illegal (broken child-count rule).
Recall Solution Q2

Data only in leaves + linked leaves for sequential scans = the B+ tree. In a plain B-tree, records can sit in internal nodes and there is no leaf chain.

Recall Solution Q3

(b) number of block reads. The CPU compares keys in nanoseconds; a disk block read is ~ times slower. See Disk and Memory Hierarchy. Everything about B-trees is designed to cut block reads, not comparisons.


L2 — Application

Recall Solution Q4

Use the parent's bound . Height is an integer, so . At most 3 edges → at most 4 disk reads.

Recall Solution Q5

Entries per block . Treat that as the max number of children : So pick → up to keys per node, one node = one block. This is exactly the parent's rule: block size dictates fanout.

Recall Solution Q6

So , giving at most 5 block reads for any of a billion keys — compared with ~30 for a binary Binary Search Tree.


L3 — Analysis

Recall Solution Q7

With min 1 key per node, each node has as few as children — the tree degenerates toward binary. Worst-case height , i.e. up to ~20 levels. Compare with Q4's guaranteed when nodes are half-full (). Conclusion: the " keys" rule forces every node at least half full, which keeps the effective branching factor high and the height at instead of . The rule is what makes the tree short.

Recall Solution Q8
  • B-tree internal fanout children.
  • B+ tree internal fanout children. Because B+ internal nodes carry no data payload, they pack ~4× more routers per block. Height scales as , so higher fanout → shorter tree → fewer reads. This is the structural reason Database Indexing engines prefer B+ trees.
Recall Solution Q9

No. In a B+ tree the internal 40 is only a router — the real record for 40 lives in a leaf. You must descend to the leaf to read the data. (Contrast: in a plain B-tree, a key match can end at any node because the record sits there.)


L4 — Synthesis

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

max keys , split on the 4th. "Split-on-the-way-down": when you meet a full node, split it first (median goes up), then descend.

  1. Insert 10, 20, 5 → root [5 10 20] (full).
  2. Insert 6: root is full → split, median 10 up. Tree: [10] over [5 6] and [20]. Insert 6 lands in left leaf [5 6].
  3. Insert 12 → into [20][12 20].
  4. Insert 30 → [12 20 30] (full leaf).
  5. Insert 7 → into [5 6][5 6 7] (full leaf).
  6. Insert 17: descend right; leaf [12 20 30] is full → split, median 20 up to root → root [10 20]; 17 lands in [12 17]. Final tree: root [10 20]; leaves [5 6 7], [12 17], [30]. All leaves at depth 1. ✓ (matches the figure)
Figure — B-tree and B+ tree — motivation (disk storage), properties
Recall Solution Q11

All real keys live in chained leaves; internal nodes are pure routers. Leaves (sorted, chained): [5 6 7] ⇄ [10 12 17] ⇄ [20 30]. Routers above: [10 20]. Range 6..17: descend once to the leaf containing 6 → [5 6 7] (read 1), grab 6,7; follow the leaf link to [10 12 17] (read 2), grab 10,12,17; next key 20 > 17, stop. Answer: 2 leaf blocks read — sequentially, no climbing back to the root. That leaf chain is why range scans are cheap.


L5 — Mastery

Recall Solution Q12

For , . If the root also needed keys, then a tree storing a single key would be illegal — yet a one-key tree obviously must exist (you just inserted your first key!). The smallest legal non-empty tree is a single root node with 1 key, e.g. [42], height . Hence the root is exempt: it may hold as few as 1 key. Every other node still obeys . This exemption is exactly what lets a fresh tree of size 1 be valid.

Recall Solution Q13

(a) Max height = sparsest tree. Bound: . (b) Min height = densest tree. Every node full ( keys, children). Keys held by a tree of height where every node is full: total nodes , each with 5 keys → keys .

  • : keys (too few for 40).
  • : keys (still ).
  • : keys (). So min height . Here max and min height both equal 2 — with and 40 keys the tree is forced to exactly height 2. Neat coincidence that shows how tight the bounds squeeze.
Recall Solution Q14

(a) An empty tree stores nothing; by convention it has no nodes (or a single empty root), so there is no root-to-leaf path — height is undefined/; the first insert creates a height-0 single-node tree. (b) As , one node can hold all keys → the tree collapses to a single node, . (Real life stops this because a node can't exceed one disk block.) (c) Smallest legal is (1–3 keys, 2–4 children) — the 2-3-4 tree, which is structurally equivalent to a Red-Black Tree. Below nodes could be too empty to guarantee balance.


Recall One-line self-test

A B-tree with storing keys — worst-case reads? ::: , so at most 7 block reads. See File Systems and Database Indexing for real deployments.