B-Trees & B+ Trees: Multi-Way Search Trees for Storage Systems

Standard balanced binary search trees like AVL and Red-Black trees optimize for memory (RAM) where pointer traversal has minimal overhead. However, when datasets exceed main memory and must reside on secondary storage (SSDs/HDDs), binary trees suffer because reading each node requires a separate high-latency disk I/O operation.

Invented by Rudolf Bayer and Edward M. McCreight, B-Trees and their modern variant—the B+ Tree—solve this bottleneck by maximizing the branching factor (order M), allowing each node to hold hundreds or thousands of keys aligned directly with disk block (page) sizes (typically 4 KB or 16 KB).

Core Invariants of a B-Tree of Order M

A B-Tree of order M adheres to the following structural rules:

  • Key Capacity per Node: Every internal node contains at most M - 1 keys and at least ceil(M / 2) - 1 keys (except the root node, which can hold a minimum of 1 key).
  • Child Pointer Capacity: A node with k keys must have exactly k + 1 children.
  • Perfect Leaf Balance: All leaf nodes reside at the exact same depth (level), guaranteeing deterministic O(log_M N) height.
  • Sorted Keys: The keys within each node are stored in strictly ascending order, acting as separation bounds for child subtree ranges.

B-Tree vs. B+ Tree: Key Architectural Differences

While classic B-Trees store satellite data/record pointers inside internal nodes alongside routing keys, modern database storage engines almost universally adopt the B+ Tree variant:

  • Data Storage Location: In a B+ Tree, internal nodes strictly store routing keys and child pointers; all actual row data or record pointers reside exclusively in leaf nodes.
  • Higher Branching Factor (Fan-out): Because internal nodes omit data payloads, more keys fit into a single disk page, resulting in shallower trees (often only 3 to 4 levels for hundreds of millions of rows).
  • Sequential Range Scans: Leaf nodes in a B+ Tree are connected via bidirectional linked lists. A range query (e.g., WHERE age BETWEEN 20 AND 30) simply navigates to the first leaf node in O(log_M N) and traverses the linked list linearly, avoiding expensive in-order tree traversals.
  • Uniform Query Latency: Every search in a B+ Tree traverses from root to leaf, providing consistent lookup latency across all keys.

Insertion, Splits, and Deletion Mechanics

1. Insertion and Node Splitting

Keys are always inserted into the appropriate leaf node. If inserting causes the leaf to exceed its maximum capacity (M keys), the node splits into two halves around the median key. In a standard B-Tree, the median key ascends to the parent; in a B+ Tree, the median key copies up to the parent while remaining in the leaf to preserve complete data records.

2. Deletion, Borrowing, and Merging

When a key deletion causes a node's key count to fall below the minimum threshold (ceil(M / 2) - 1), the engine attempts to borrow a key from a neighboring sibling. If both siblings are at minimum capacity, it merges the underflowing node with a sibling and drops the separator key from the parent.

Why Databases Depend on B+ Trees

Database engines design their page architectures around hardware I/O realities:

  1. Disk Block Alignment: Sizing B+ Tree nodes to match the filesystem page size (e.g., 16 KB in MySQL InnoDB) ensures that fetching a node corresponds to a single continuous hardware read.
  2. Buffer Pool Caching: The top 2 levels of a B+ Tree (root and high-level routing pointers) remain cached permanently in RAM buffer pools, meaning most record fetches require at most 1 physical disk read.
  3. Clustered vs. Secondary Indexes: In clustered tables (e.g., InnoDB primary keys), leaf pages contain full table row data; in secondary indexes, leaf pages store the primary key references.

Real-World Systems and Engineering Use Cases

  1. Relational Database Indexing: Powers the default B-Tree/B+ Tree indexes in MySQL (InnoDB), PostgreSQL, SQLite, and Oracle.
  2. File System Directory Structures: Manages large file allocations and directory trees in NTFS, ext4, Btrfs, and Apple's APFS.
  3. Document Stores & Key-Value Engines: Used in MongoDB wiredTiger engine and distributed storage engines requiring bounded lookup bounds.
  4. High-Throughput Analytics & Range Queries: Efficiently serving time-series filtering and sequential bounding box scans.