Loading lessons...
Multi-Way Trees: B-Trees, B* Trees & B+ Trees
Multi-Way Trees: B-Trees, B* Trees & B+ Trees
When datasets exceed RAM and live on disk or SSD, each node access costs a slow disk I/O read. Multi-way Balanced Search Trees pack hundreds of keys into a single disk-block-sized node, keeping tree height tiny (3-4 levels for millions of records!).
B-Tree vs B+ Tree
1. M-Way Search Tree & B-Tree (Order m)
An M-Way Search Tree allows each node to have up to m children and m - 1 sorted keys separating the child key ranges. A B-Tree of order m adds strict balance rules:
- Root has at least 2 children (unless it is a leaf).
- Every internal node has between ⌈ m/2 ⌉ and m children (so ⌈ m/2 ⌉ - 1 to m - 1 keys).
- All leaves are at the exact same level.
B-Tree Order-5 Insertion & Deletion Trace (CIC-209 Deck)
In a B-Tree of order m = 5: max keys = 4, min keys = ⌈ 5/2 ⌉ - 1 = 2.
- Insert (
8, 9, 39, 4, ...): Always insert into a leaf in sorted order. Once a leaf reaches 5 keys (overflow), split it around the median key and promote the median key to the parent! - Delete (
93, 201, 180, 72): If deleting from an internal node, replace with Inorder Predecessor/Successor from a leaf. If a leaf underflows (< 2 keys):
- Borrow (Redistribute) from an adjacent sibling with > 2 keys via the parent separator, OR
- Merge with an adjacent sibling + parent separator key.
2. 2-3 Tree, 2-3-4 Tree & B* Tree
- 2-3 Tree: B-Tree of order m = 3 (nodes have 2 or 3 children, 1 or 2 keys).
- 2-3-4 Tree: B-Tree of order m = 4 (nodes have 2, 3, or 4 children, 1 to 3 keys).
- *B Tree: Enforces a higher minimum fill factor of 2/3 (instead of 1/2). On overflow, it redistributes keys to a sibling first; only when both siblings are full does it perform a 2-to-3 split** - improving disk space utilization!
3. B+ Tree (Database & Filesystem Index Standard)
| Feature | B-Tree | B+ Tree |
|---|---|---|
| Data Pointers | Stored in internal + leaf nodes | Stored only in leaf nodes |
| Internal Nodes | Keys + Data pointers | Routing keys only (higher fan-out!) |
| Leaf Linkage | Not linked | Linked sequentially (fast range scans) |
| Search Cost | Can stop early at internal node | Always O(logm n) to leaf (uniform) |
- B+ Tree Insertion Note: When a leaf splits, the median key is copied (not moved!) up to the parent so every key still exists in the linked leaf level for sequential range scans (
WHERE id BETWEEN 100 AND 500).
Why Is the Order m So Large in Practice?
- Reading a whole disk block takes as long as reading part of one, and a block holds many keys/pointers (typically 128-1024).
- So a node is sized to fill one block: order m ≈ 128-1024, i.e., 127-1023 keys and 128-1024 child pointers per node.
- A large m makes the tree very shallow: with m = 1024, three levels index over a billion keys, so any search needs only about 3 disk reads.
2-3 and 2-3-4 Trees (examples from the notes)
2-3 tree (B-tree of order 3): every non-root node has 2 or 3 subtrees; all leaves are on the same level.
[33 | 72]
/ | \
[14 | 21] [44 | 65] [94]
/ | \ / | \ / \
[11,13][17][24,28][35,42][46,58][68][85][96,98]2-3-4 tree (B-tree of order 4): every node has 2, 3 or 4 subtrees (1 to 3 keys).
[42]
/ \
[16 | 21] [57 | 78 | 91]
/ | \ / | | \
[11] [17,19] [21,23,24] [45] [63,65] [85,90] [95]B* Tree Example: Insert 30 (order 5)
Before: [21 | 78]
/ | \
[8,10] [25,32,53,76] [93,99]30 belongs in the middle leaf, which is already full (4 keys). A plain B-tree would split it. A *B tree first redistributes**: the left sibling has room, so the separator 21 moves down into the left leaf and the smallest key of the full leaf (25) moves up to replace it:
After: [25 | 78]
/ | \
[8,10,21] [30,32,53,76] [93,99]No new node and no extra level-that is why B* trees stay at least two-thirds full. A split happens only when the sibling is full too, and then two full nodes become three nodes, each two-thirds full.
Step-by-step B-tree and B+ tree insertion and deletion traces (order 4 and 5) are in the next lesson, B-Tree & B+ Tree: Worked Insertion & Deletion.
📝 IPU End-Term Exam Corner (5-10 Marks)
- Q1. Define a B-Tree of order m and list its properties. Insert
8, 9, 39, 4, ...into an empty B-Tree of order 5. (7.5 Marks) - Write: Min/max children & keys per node, equal leaf depth invariant, and step-by-step median-split insertion trace.
- *Q2. Differentiate between B-Tree, B Tree, and B+ Tree. (7.5 Marks)**
- Write: Data pointer placement (all nodes vs leaves only), fill factor (1/2 vs 2/3), 2-to-3 split in B* Tree, and sequential linked leaves in B+ Tree.