Sub topic 16
B-tree versus B+ tree: fan-out, range scans, storage
This chapter assumes you know how both trees are built: node order, the balance rules, and how insertion splits a node. You should also be comfortable with block-access cost and ceiling and floor arithmetic. Here we compare the two trees on the three axes GATE questions and database designers care about: fan-out, range scans, and storage.
The structural difference
A B-tree stores every key once, in either an internal node or a leaf. Each key carries its record pointer, so a search can stop at an internal node.
A B+ tree keeps record pointers only in the leaves. Internal nodes hold separator keys that only guide the search. Every key therefore appears in a leaf, and some keys are also copied into internal nodes. The leaves are chained by next-leaf pointers.
| Property | B-tree | B+ tree |
|---|---|---|
| Record pointers | In every node | Leaves only |
| Internal node content | Keys, record pointers, child pointers | Separator keys and child pointers |
| Key repeated in tree | Never | Some keys appear in internal nodes and leaves |
| Leaves linked | No | Yes, left to right |
| Search ends | At any level | Always at a leaf |
| Cost of a point search | 1 to h accesses | Exactly h accesses |
Fan-out
Fan-out is the number of children a node can have. A larger fan-out gives a shorter tree, and a shorter tree needs fewer block accesses.
Use block size B, key size K, block pointer size P and record pointer size Pr. The two internal-node inequalities differ:
- B-tree internal node of order p: p·P + (p − 1)·(K + Pr) ≤ B
- B+ tree internal node of order p: p·P + (p − 1)·K ≤ B
Each B-tree key drags a record pointer into the internal node, so fewer entries fit. The B+ internal node spends its whole block on keys and child pointers.
The leaf of a B+ tree holds q keys, each with a record pointer, plus one next-leaf pointer: q·(K + Pr) + P ≤ B. Leaves are a separate calculation from the internal order.
The script below uses the same numbers as the order-calculation chapter (B = 512, K = 8, P = 6, Pr = 7). It then finds the smallest height that could hold one million keys.
def btree_order(B, K, P, Pr):
return (B + K + Pr) // (K + Pr + P)
def bplus_internal_order(B, K, P):
return (B + K) // (K + P)
def bplus_leaf_order(B, K, P, Pr):
return (B - P) // (K + Pr)
def btree_levels(n, p):
# a full B-tree of L levels holds p**L - 1 keys
levels = 1
while p ** levels - 1 < n:
levels += 1
return levels
def bplus_levels(n, p, q):
# a full B+ tree of L levels holds q * p**(L-1) keys in its leaves
levels, capacity = 1, q
while capacity < n:
levels += 1
capacity *= p
return levels
B, K, P, Pr = 512, 8, 6, 7
pb = btree_order(B, K, P, Pr)
pp = bplus_internal_order(B, K, P)
q = bplus_leaf_order(B, K, P, Pr)
n = 1_000_000
print("B-tree order", pb, "levels", btree_levels(n, pb))
print("B+ internal order", pp, "leaf keys", q, "levels", bplus_levels(n, pp, q))This prints a B-tree order of 25 with 5 levels, and a B+ internal order of 37 with 33 keys per leaf and 4 levels. The B+ tree is one level shorter, so every point search saves one block access.
Range scans
Suppose a query asks for all keys from 40 to 90.
In a B+ tree you descend once to the leaf containing 40. That costs h accesses. You then follow next-leaf pointers until a key exceeds 90. If the range spans m leaves, the cost is about h + (m − 1) accesses, and every leaf is read sequentially.
In a B-tree there are no leaf links. An in-order traversal climbs back to internal nodes to pick up keys that live there, then descends again. Qualifying keys are scattered across levels, and the same internal blocks may be revisited. The cost is higher and less predictable.
This is the main reason database systems choose B+ trees. Range predicates (BETWEEN, <, >), ORDER BY on the indexed column, and full ordered scans all use the leaf chain.
Storage
Two effects pull in opposite directions.
- Duplication. B+ trees repeat some keys in internal nodes. The internal levels are small, though. With 33 keys per leaf and 37 children per internal node, each internal level is about 1/37 the size of the level below. The extra space is a few percent of the whole tree.
- Density. B+ internal nodes pack more keys per block, since no record pointers are stored there. That gives a smaller internal part and a shorter tree.
A B-tree stores each key exactly once. If the keys are large and the record pointers are small, this can matter. In practice the fan-out advantage of the B+ tree outweighs the duplication.
Internal nodes of a B+ tree are also small and hot, so they stay in the buffer pool. Searches then cost about one disk read, for the leaf.
Updates
Insertion. Both trees split an overflowing node. A B-tree splits by moving the median key up to the parent. A B+ tree leaf split copies the smallest key of the new right leaf into the parent, so the key stays in the leaf. An internal B+ node split moves the median up, as in a B-tree.
Deletion. A B+ tree always deletes from a leaf. After deletion, a separator in an internal node may no longer match any leaf key. That is still a valid separator, so the tree needs no repair unless the leaf underflows. A B-tree deletion from an internal node needs a replacement, usually the in-order predecessor or successor taken from a leaf. That replacement can cascade into more underflow handling.
Decision criteria
| Workload | Better choice | Reason |
|---|---|---|
| Range queries, ordered scans | B+ tree | Linked leaves |
| Point lookups, large tree | B+ tree | Higher fan-out, shorter height |
| Predictable lookup cost | B+ tree | Every search takes h accesses |
| Frequent hits on keys near the root | B-tree | Search may stop early |
| Simple deletion logic | B+ tree | Deletes only from leaves |
Common errors
- Using the B+ internal formula for a B-tree. A B-tree node always includes record pointers.
- Forgetting the next-leaf pointer P in the leaf inequality.
- Counting a B+ search as stopping early. It always reaches a leaf, so the cost is h, plus one if you must read the data block.
- Counting the separator copy as a new record. Keys in internal nodes of a B+ tree are not extra records.
- Computing height from the internal order alone. The leaf order q differs from the internal order p, so the bottom level multiplies by q.
Exercise
A block is 1024 bytes. Keys are 10 bytes, block pointers are 8 bytes, and record pointers are 6 bytes. Find the order of a B-tree node, the internal order of a B+ tree node, and the number of keys in a B+ leaf. Which tree is shorter for a very large file?
Show answer
B-tree: p·8 + (p − 1)·16 ≤ 1024 gives 24p ≤ 1040, so p = 43. B+ internal: p·8 + (p − 1)·10 ≤ 1024 gives 18p ≤ 1034, so p = 57. B+ leaf: q·16 + 8 ≤ 1024 gives q = 63. The B+ tree has the larger fan-out (57 against 43), so it is shorter or equal in height for large files.
Exercise
A range query returns keys spread over 5 consecutive leaves of a B+ tree of height 3 (the root is level 1, the leaves are level 3). How many block accesses does the index part of the query need?
Show answer
Descending to the first leaf costs 3 accesses. The other 4 leaves are reached through next-leaf pointers, costing 4 more. The total is 7 accesses, which matches h + (m − 1) = 3 + 4.