Multiway Trees, B-Trees & B+ Trees
Storage page disk access latency, B-Tree (M-way) balancing invariants, proactive node splitting, and B+ Tree leaf linked-list sequential scans.
When data scales beyond random-access memory, binary trees fail due to disk seek latency. Multiway search trees solve the memory wall by matching node capacity to physical storage pages, collapsing tree height into shallow, high-fanout hierarchies where B-Trees and B+ Trees power virtually all production relational databases and filesystem storage engines.
1. Executive Summary & Learning Objectives
#Invented by Rudolf Bayer and Edward M. McCreight in 1970 at Boeing, the B-Tree is a self-balancing search tree optimized for systems reading and writing large blocks of memory. Unlike binary trees whose branching factor is fixed at 2, B-Trees and B+ Trees expand their branching factor to hundreds or thousands of keys per node, keeping the tree exceptionally shallow and minimizing disk I/O operations.
By the end of this chapter, you will be able to:
- Analyze the storage hierarchy latencies (L1 cache vs. RAM vs. NVMe SSD vs. HDD) and formulate why binary search trees fail on external storage.
- State the formal B-Tree invariants parameterized by minimum degree , calculating key and child boundaries across all levels.
- Execute proactive split insertions and borrow/merge deletions in time without cascading backward passes.
- Contrast B-Trees and B+ Trees, explaining how separating router keys from doubly-linked data leaves optimizes sequential range queries.
- Prove the structural isomorphism between 2-3-4 trees and Red-Black trees.
2. External Memory & The Need for Multiway Trees
#The Storage Hierarchy Latency Cliff
#Modern computing hardware exhibits latency disparities spanning seven orders of magnitude across the memory hierarchy:
| Memory Tier | Typical Access Latency | Latency Scaled to Human Terms () | Hardware Transfer Granularity |
|---|---|---|---|
| CPU L1 / L2 Cache | (Cache Line) | ||
| Main Memory (DRAM) | |||
| NVMe Flash SSD | () | (Page Block) | |
| Mechanical Hard Disk (HDD) | () | (Sector Block) |
Because operating systems and storage controllers read and write secondary storage strictly in fixed-size blocks (typically , , or ), fetching a single byte costs the same I/O latency as reading the entire page block.
Why Binary Search Trees Fail on External Storage
#Suppose an enterprise database indexes () records using a balanced binary search tree (AVL or Red-Black Tree):
- Tree Height: levels.
- In the worst case, each node resides on a different physical disk page.
- A single key lookup requires 30 random page reads.
- On an NVMe SSD at per read, this takes ; on an HDD at per seek, this takes per query!
The Multiway Solution: High Fanout, Shallow Trees
#Instead of 2 children per node, let each node hold hundreds of keys, precisely matching one storage page block ():
- Suppose page size , key size , and pointer size .
- Each node stores keys and child pointers.
- Resulting Tree Height:
- Searching now requires only 3 to 4 page reads—an reduction in physical I/O!
3. B-Tree Invariants & Structural Mechanics
#A B-Tree is a self-balancing search tree parameterized by a minimum degree :
| Invariant # | Invariant Rule | Formal Boundary Condition | Architectural Purpose |
|---|---|---|---|
| 1 | Root Capacity | The root contains between and keys. If not a leaf, it has at least children. | Allows the tree to grow upward through root splits. |
| 2 | Internal Node Keys | Every internal node except root has at least keys and at most keys. | Guarantees at least storage page utilization. |
| 3 | Internal Node Children | An internal node with keys has exactly children (between and children). | Matches multiway search partitioning boundaries. |
| 4 | Key Ordering | Keys in each node are strictly sorted: . | Enables fast in-node binary search. |
| 5 | Subtree Range Invariant | For child subtrees of node : . | Preserves the global multiway search invariant. |
| 6 | Uniform Leaf Depth | All leaves appear at the exact same depth . | Ensures uniform, predictable worst-case performance. |
Proactive Split Insertion ()
#In classical algorithms, inserting into a full leaf requires splitting that node and propagating the median key upward, which can trigger cascading splits back to the root.
CLRS formalizes Proactive Splitting: As the search algorithm descends from the root toward the target insertion leaf, whenever it encounters any node that is already full (contains exactly keys), it splits that node immediately before descending further. Consequently, the parent is guaranteed to have space to accept the promoted median key!
| Phase | Topological State | Keys in Involved Nodes | Structural Changes |
|---|---|---|---|
| Before Split | Full Child Node ( keys). | . | is the -th child of non-full Parent . |
| Split Execution | Median key is extracted. | - Median: . - Left slice: . - Right slice: . | is inserted into at position . retains the left keys. A new sibling node is allocated for the right keys. |
| After Split | Parent has one additional key and child. | - : contains . - : keys, children. - : keys, children. | Both and now have room for future insertions. Invariant 2 and 6 preserved! |
4. B+ Trees: The Universal Database Storage Engine Standard
#While a standard B-Tree stores satellite data records in both internal router nodes and leaf nodes, the B+ Tree enforces a strict separation between routing indices and payload data:
| Dimension / Layer | Internal Nodes (Router Layer) | Leaf Nodes (Data Layer) |
|---|---|---|
| Stored Contents | Search keys and child pointers only. Zero data payloads! | All actual data records or tuple row IDs (RID). |
| Key Duplication | Keys may be duplicated in leaves as exact data anchors. | Contains every single key present in the entire database. |
| Inter-Node Links | Downward child pointers only. | Linked sequentially via a Doubly-Linked List. |
| Query Traversal | Pure routing: Guides searches down to appropriate leaf. | Terminal points for lookups and start points for range scans. |
Why B+ Trees Dominate Relational Databases (PostgreSQL, MySQL InnoDB)
#- Higher Branching Factor ():
- Because internal nodes store zero tuple data, many more index keys fit into each disk page. The resulting tree is shorter, reducing disk I/O.
- High-Performance Range Scans (
BETWEEN A AND B):- In a standard B-Tree, a range scan requires an in-order tree walk that jumps repeatedly between disk pages across different levels of the tree.
- In a B+ Tree, the engine performs a single binary search to find the start key leaf, then traverses the horizontal linked list of leaves linearly!
- Predictable Query Latency:
- Every lookup traverses the exact same depth to reach a leaf, eliminating latency jitter.
5. Step-by-Step Dry Run State Trace: B-Tree Insertion & Node Split
#Consider a B-Tree with minimum degree (each node holds at most keys and at least key).
Inserting keys sequentially: into a single root node:
| Step | Operation | Current Node Keys | Node Condition | Mutation / Split Action | Resulting Hierarchy |
|---|---|---|---|---|---|
| 1 | Insert | [10] | Count = 1 () | Insert into sorted position. | Root: [10] |
| 2 | Insert | [10, 20] | Count = 2 () | Insert into sorted position. | Root: [10, 20] |
| 3 | Insert | [10, 20, 30] | Count = 3 (Node Full!) | Node is full (). Root must split upon next insertion. | Root: [10, 20, 30] |
| 4 | Insert | Needs insertion into [10, 20, 30] | Full Node Encountered! | Root Split Triggered: Median key promoted to become new Root! | New Root: [20]Left Child: [10]Right Child: [30] |
| 5 | Complete Insert | Target leaf is Right Child [30] | Count = 1 () | Insert into right leaf: [25, 30]. | Root: [20]Left: [10]Right: [25, 30] |
6. The Isomorphism Between 2-3-4 Trees and Red-Black Trees
#A 2-3-4 Tree is a B-Tree of minimum degree :
- 2-Node: 1 key, 2 children.
- 3-Node: 2 keys, 3 children.
- 4-Node: 3 keys, 4 children.
Every Red-Black Tree is structurally isomorphic to a 2-3-4 Tree:
| 2-3-4 Tree Node | Equivalent Red-Black Tree Subtree | Node Color Encoding |
|---|---|---|
2-Node [ B ] | Single node [ B ] | B is BLACK. |
3-Node [ A │ B ] | Parent [ B ] with left child [ A ] (or right child [ B ] with left child [ A ]) | B is BLACK, A is RED. |
4-Node [ A │ B │ C ] | Node [ B ] with two children: left [ A ] and right [ C ] | Root B is BLACK, both children A and C are RED. |
Isomorphism Consequence: When a 2-3-4 tree splits a 4-node , median key moves up. In the isomorphic Red-Black Tree, this corresponds precisely to Case 1 (Uncle Color Flip): recoloring children and to BLACK and parent to RED!
7. Asymptotic Complexity Matrix
#| Operation | B-Tree (Memory Keys) | B-Tree (Disk Block I/O) | B+ Tree Range Query ( items) | Binary Search Tree (Disk I/O) |
|---|---|---|---|---|
| Search | ||||
| Insertion | ||||
| Deletion | ||||
| Range Scan | random I/Os | sequential I/Os | random I/Os |
8. Common Traps, Edge Cases & Implementation Pitfalls
#- Underflow on Deletion (Borrow vs. Merge):
- When deleting from an internal node with keys, check whether an immediate sibling has keys. If so, borrow a key via parent rotation; if both siblings have keys, merge the node with a sibling and drop a parent key.
- Page Fragmentation in In-Memory B-Trees:
- In languages with garbage collection (JavaScript/Python/Java), allocating array objects inside nodes can induce heap fragmentation. Systems implementations in C/C++ or Rust allocate contiguous page-aligned byte buffers.
- Failure to Maintain Doubly-Linked Leaf Chain:
- In B+ Trees, when a leaf splits, the
nextandprevpointers of the leaf chain must be updated atomically. Omitting this breaks concurrent range scans.
- In B+ Trees, when a leaf splits, the
9. Real-World Applications & Practice Problems
#Production Systems
#- PostgreSQL & MySQL InnoDB Storage Engines: Use B+ Trees as the default indexing structure (
BTREE) for primary keys and secondary indices. - Modern Filesystems (Btrfs, XFS, APFS, NTFS): Manage filesystem directory namespaces and extent allocation maps using B-Trees.
- SQLite Database: Uses B-Trees for database table organization and B+ Trees for indices.
Practice Problems
#- Implement B-Tree Search and Split Insertion — Implement multiway key search and proactive child split.
- B+ Tree Leaf Chain Scan — Implement lower-bound binary search to leaf followed by doubly-linked list iteration.
10. References & Academic Attribution
#- Bayer, R., & McCreight, E. M. (1970). Organization and maintenance of large ordered indices. Boeing Scientific Research Laboratories, Report No. 20.
- Comer, D. (1979). The ubiquitous B-tree. ACM Computing Surveys (CSUR), 11(2), 121–137.
- Cormen, T. H., Leiserson, C. E., Rivest, R. L., & Stein, C. (2022). Introduction to Algorithms (4th ed.), Chapter 18 (B-Trees). MIT Press.