6.10 B-Trees and B+ Trees Foundations
π‘ Core Intuition: The 3-Second Mental Modelβ
π³ The Everyday Analogy:β
Imagine searching for a single legal statute inside a multi-building archive. If each filing cabinet holds only one sheet of paper and two hallway signs (Left or Right), finding one record among a billion files forces you to unlock thirty separate cabinets across thirty corridors. Instead, put an entire 500-entry directory index on the front desk of each floor. One glance directs you immediately to the exact wing, shrinking thirty slow trips down to three page reads.
π» Bridging to Computer Science:β
Binary Search Trees and AVL Trees assume every pointer hop hits fast RAM in nanoseconds. On SSDs and spinning disks, fetching a single 4 KB or 16 KB page costs orders of magnitude more latency than scanning hundreds of sorted keys already loaded into cache. B-Trees and B+ Trees flatten tree height dramatically by packing hundreds of keys and child pointers into a single disk-page node, guaranteeing shallow, perfectly balanced I/O lookups.
πTable of Contents
- π§± Why High Branching Factor Conquers Disk Latency
- π Formal Properties of an Order-m B-Tree
- βοΈ Splitting Bottom-Up Without Increasing Skew
- π B-Trees vs. B+ Trees: Why Databases Choose B+ Trees
- π Real-World Production Context: Database Indexes & Filesystems
- π Numerical Applications & Worked Examples
- π― Exam & Interview Pitfall Check
- π Navigation & Next Steps
π§± Why High Branching Factor Conquers Disk Latencyβ
An AVL tree storing keys requires pointer dereferences per search. Even if only 20 levels miss RAM cache, twenty random disk seeks stall CPU execution for milliseconds.
By choosing a high order matched to the hardware page size (typically to ), a single disk fetch pulls in an entire sorted block of delimiter keys and child block IDs. With , storing one billion records requires a tree height of at most:
π Formal Properties of an Order-m B-Treeβ
In classical database and algorithms literature (Knuth / CLRS), a B-Tree of order (maximum children per node) enforces five structural invariants so nodes never become too sparse or overflow:
- Maximum Children & Keys: Every node has at most children and therefore at most keys.
- Minimum Children (Non-Root Internal Nodes): Every internal node except the root must have at least children (i.e., at least keys).
- Root Exception: If the tree is non-empty, the root has at least children unless it is itself a leaf node (containing to keys).
- Uniform Leaf Depth: All leaf nodes sit at the exact same depth from the root β a B-Tree never grows unbalanced along one branch.
- Sorted Interleaving: A non-leaf node with keys stores child pointers , partitioning subtrees cleanly into strictly disjoint key ranges.
βοΈ Splitting Bottom-Up Without Increasing Skewβ
Unlike a binary search tree that allocates new leaves blindly downward (risking chains), a B-Tree grows upward from the root when nodes overflow:
- Descend from the root to the target leaf where the new key belongs.
- If the leaf has fewer than keys, insert in sorted order and stop.
- Overflow Split: If the node already holds keys, adding the new key temporarily creates keys. Promote the median key up into the parent node, and split the remaining keys evenly into a left half and right half node.
- If promoting the median fills the parent, the split cascades upward; splitting the root creates a brand-new root with 1 key and 2 children, increasing tree height uniformly by across all leaves simultaneously.
π B-Trees vs. B+ Trees: Why Databases Choose B+ Treesβ
Production storage engines (PostgreSQL, MySQL InnoDB, SQLite, NTFS) almost universally implement B+ Trees rather than textbook B-Trees.
Textbook B-Tree vs. Production B+ Tree
How stripping data payloads from internal nodes accelerates both point lookups and range scans
Keys + Payloads Everywhere
- β’Data pointers / row records live in BOTH internal nodes and leaves.
- β’Point search can terminate early at an internal node.
- β’Payloads eat page space -> smaller fan-out -> taller trees.
- β’Range queries require slow back-and-forth tree traversal.
Routing Internals + Linked Leaves
- β’Internal nodes store ONLY separator keys and child block pointers.
- β’Maximum fan-out packs far more branches per 16 KB page -> shorter trees.
- β’All keys & payloads reside at leaf level in sorted order.
- β’Leaves connect via sibling pointers for blazing sequential range scans.
π Real-World Production Context: Database Indexes & Filesystemsβ
Where Multi-Way Balanced Trees Power Storage Systems
Every modern persistent database and filesystem relies on high fan-out trees to defeat I/O bottlenecks
Clustered Primary Indexes
MySQL InnoDB / PostgreSQL- 16 KB pages yield branching factors around m = 500 to 1200.
- Top 2 to 3 levels stay permanently pinned in the RAM buffer pool.
Filesystem Directory Extents
ext4 / XFS / Btrfs / APFS- Maps millions of file blocks inside directories cleanly via B+ Tree extents.
- Copy-on-Write B-Trees in Btrfs/ZFS enable instant atomic snapshots.
Range Scan Iterators
OLAP & Time-Series- Once the first leaf key is located, sibling pointers stream consecutive records sequentially.
- Avoids touching internal nodes again for the entire range scan.
Page-Level Concurrency
Latch Crabbing- High branching factor plus leaf-only records shrink lock contention windows.
- B-Link Trees add right-sibling pointers inside internal levels for lock-free reads.
π Numerical Applications & Worked Examplesβ
Suppose a disk block size is , each search key occupies , each record pointer takes , and each block (child) pointer takes . Calculate:
- The maximum order (maximum children) of an internal B-Tree node vs. an internal B+ Tree node.
- What is the minimum number of keys a non-root internal node must store in a B-Tree of order ?
Step-by-Step Solution:β
- B-Tree Internal Node Order (): An internal B-Tree node holds at most child pointers, keys, and record pointers:
Substituting values:
- B+ Tree Internal Node Order (): A B+ Tree internal node omits record pointers () entirely, packing only child pointers and keys:
(Notice how removing record pointers raises branching factor from to β almost wider per disk page!)
- Minimum Keys for Order : By invariant #2, every non-root internal node has at least children, so minimum keys = keys.
π― Exam & Interview Pitfall Checkβ
- Can two leaves sit at different depths in a valid B-Tree?
- Never. Because B-Trees only increase height when the root node splits upward, every single path from root to leaf has identical length.
- In a B+ Tree, if a key is duplicated into an internal node during a leaf split, what happens on deletion?
- Routing keys in internal B+ Tree nodes only act as traffic signs; sometimes implementations leave deleted separator keys intact inside internal nodes as long as the leaf is removed, whereas B-Tree deletions must immediately rebalance or swap with an inorder predecessor/successor.
- Why choose order in storage engines instead of AVL Trees?
- Cache-line & page locality: Disk I/O reads an entire block () regardless of whether 1 key or 500 keys are inside it; B-Trees amortize each seek over hundreds of comparisons in CPU L1/L2 cache.
- Mixing Up "Order " vs. "Minimum Degree ": Textbook authors vary! Knuth defines order as the maximum children ( min children), whereas CLRS defines minimum degree ( children , children , max keys ). Always clarify your definition before writing inequalities.
- Assuming B+ Trees Are Faster in Pure RAM: On small in-memory datasets fitting inside L1/L2 cache, scanning an array of 200 keys per node adds branch-prediction and cache-scan overhead where tight Red-Black / AVL trees or Hash Tables often winβthough cache-conscious small B-Trees (like Rust's
BTreeMap, ) still outperform binary trees due to cache-line utilization! - Forgetting Copy-Up vs. Push-Up: On a B-Tree node split, the median key is pushed up and removed from the children; on a B+ Tree leaf split, the median key is copied up into the parent while remaining inside the right leaf so no data record disappears!
π Navigation & Next Stepsβ
Next up, we step away from key-comparison trees into digital radix indexing: Tries (Prefix Trees) for autocomplete and IP routing.
Never Study in Isolation. Join Binary Dose.
Connect with fellow engineers, clear conceptual doubts, and get 2 visual breakdowns & engineering mental models daily across our community channels.
Discord Community
Start at #welcome-and-rules, say hello in #general-chat, clear doubts, and collaborate with engineers worldwide.