Sub topic 16
B-tree versus B+ tree: fan-out, range scans, storage
Prerequisites and working model
Assume you know balanced search trees, disk blocks, blocking factor, and ceiling and floor arithmetic. A B-tree is a balanced multiway search tree. A B+ tree is a related tree in which internal nodes guide the search and leaf nodes store all data entries.
Both trees keep their leaves at the same depth. Their main difference is where records or record pointers are stored.
Structural differences
In a B-tree, a key and its record pointer may appear in an internal node or a leaf. Therefore, an exact-match search can finish before reaching a leaf.
In a B+ tree, internal keys are separator keys. They identify the correct child but do not represent the final data entry. Every record pointer is stored at the leaf level. A search always reaches a leaf.
| Property | B-tree | B+ tree |
|---|---|---|
| Data entries | Internal nodes and leaves | Leaves only |
| Internal keys | Search keys, often with data pointers | Separator keys |
| Leaf links | Optional | Usually linked left to right |
| Range scans | May require repeated tree searches | Follow linked leaves sequentially |
| Typical database use | Less common | Very common |
Fan-out and tree height
Fan-out is the number of child pointers that an internal node can contain. Higher fan-out means fewer levels for the same number of entries.
For an internal B+ tree node with order \(p\), key size \(K\), pointer size \(P\), and block size \(B\):
\[ pP + (p-1)K \leq B \]
Thus:
\[ p = \left\lfloor \frac{B+K}{P+K} \right\rfloor \]
B+ tree internal nodes store only separator keys and child pointers. They do not need record pointers. This often gives them higher fan-out than a B-tree whose internal nodes also store data pointers.
A B-tree can sometimes store a matching record in an internal node, which may reduce the cost of one exact lookup. However, its internal entries are larger when they include record pointers. The resulting lower fan-out can increase the tree height.
A one-level difference matters because each level usually requires a separate block access. If a B+ tree has height \(h\), an exact lookup normally costs about \(h\) block accesses to reach the leaf, plus any access needed to fetch the actual data record.
Range scans
B+ trees are especially effective for ordered conditions such as:
salary BETWEEN 40000 AND 60000date >= 2026-01-01namevalues from"M"through"P"
The tree finds the first matching leaf. The search then follows the leaf links until the range ends. This avoids returning to the root for every next key.
A B-tree can also answer range queries, but consecutive entries may be spread across internal nodes and different subtrees. An ordered traversal is more complicated and usually causes more random block accesses.
Choosing between them
Use a B+ tree when the workload includes range queries, ordered output, sequential scans, or many records per index. Its duplicated separator keys consume some space, but the larger internal fan-out and linked leaves usually outweigh that cost.
Remember the common GATE distinction: B-tree keys can carry records at several levels, while B+ tree data entries occur only in leaves. Do not count separator keys in internal B+ nodes as separate records.
Exercise
Why does a B+ tree usually have better range-scan performance than a B-tree?
Show answer
After finding the first matching leaf, a B+ tree follows linked leaves sequentially, while a B-tree may require traversal through multiple internal nodes and subtrees.