B-Trees: Structure, Node Capacity & Searching
π‘ Core Intuitionβ
π³ The Everyday Analogy: The Multi-Tier Filing Cabinetβ
Imagine you have a single binary search tree where each decision point only offers two choices: "Go Left" or "Go Right". If you have documents, a binary tree requires roughly sequential decisions. On disk, that means jumping between 20 physically separate magnetic sectors or SSD flash pagesβan eternity in I/O time.
Instead, consider a B-Tree as an expansive multi-tier filing cabinet:
- When you open a drawer (a disk block), it does not hold just one piece of paper. It holds dozens of categorized divider tabs (keys) and pointers to subsequent drawers.
- In a single mechanical pull of the drawer (one disk block transfer of ), you read divider tabs into RAM simultaneously.
- Your eye scans through all tabs in nanoseconds in memory, picks the exact sub-cabinet, and jumps directly there.
- With a branching factor (fanout) of , you reach any document among files in just 3 drawer pulls ().
A B-Tree is a self-balancing, multi-way search tree designed specifically to minimize disk I/O reads by maximizing the number of keys stored in every single disk block.
π» Bridging to Computer Scienceβ
Binary search trees (BST, AVL, Red-Black) assume all data resides in main memory, where pointer traversal costs nanoseconds. In database management systems, data is stored on secondary storage (disk/SSD). Because disk I/O is orders of magnitude slower than CPU memory operations: To optimize for disk blocks, an index node must match the physical block size (). A B-Tree of order packs hundreds of keys and child pointers into each block, dramatically flattening the tree height.
π Core Deep-Dive & Architectural Conceptsβ
1. Formal Definition & Structural Propertiesβ
Definition: A B-Tree of order is a self-balancing -way search tree that satisfies the following invariants:
- Every node has at most children.
- The Root Node:
- Has at least children (if it is not a leaf node).
- Can have children if the entire tree contains only the root.
- Contains between and search keys.
- Internal Nodes (all nodes except the root and leaves):
- Must have at least children.
- Must have at most children.
- Must contain between and search keys.
- Leaf Nodes:
- Reside at the exact same depth (level), ensuring perfect uniform height balance.
- Contain between and search keys.
- Key Ordering:
- Within every node, keys are maintained in sorted ascending order:
- For any key , all keys in the subtree pointed to by child pointer are strictly less than , and all keys in subtree are strictly greater than .
2. Node Structural Rule Summaryβ
The exact constraints governing root, internal, and leaf nodes for a B-Tree of order are summarized below:
| Node Type | Minimum Children | Maximum Children | Minimum Keys | Maximum Keys |
|---|---|---|---|---|
| Root Node | (or if leaf) | |||
| Internal Nodes | ||||
| Leaf Nodes |
3. Physical Node Structure & Capacity Equationβ
In a standard B-Tree, both internal nodes and leaf nodes store actual data record pointers alongside search keys.
Physical Block Format of a B-Tree Node:
βββββββββ¬ββββββββββ¬ββββββββββ¬ββββββββ¬ββββββββββ¬ββββββββββ¬ββββββββ¬ββββββ¬ββββββββ
β P_1 β RecP_1 β K_1 β P_2 β RecP_2 β K_2 β P_3 β ... β P_m β
βββββββββ΄ββββββββββ΄ββββββββββ΄ββββββββ΄ββββββββββ΄ββββββββββ΄ββββββββ΄ββββββ΄ββββββββ
Tree Record Search Tree Record Search Tree Tree
Pointer Pointer Key Pointer Pointer Key Pointer Pointer
Parameters:
- : Disk Block Size (e.g. or ).
- : Order of the B-Tree (maximum child/block pointers per node).
- : Size of a child tree/block pointer (e.g. ).
- : Size of the search key value (e.g. ).
- : Size of a record pointer (e.g. ).
The Node Capacity Inequality: To guarantee that a node fits completely inside a single physical disk block:
Solving for maximum order :
4. Step-by-Step B-Tree Insertion Algorithmβ
Rule of Thumb:
- Insertion always occurs at a leaf node.
- First let the node fill up to capacity ( keys).
- If a key is inserted into a node that already has keys (Overflow / Conflict), split the node into two halves and promote the median key upward into the parent node.
- If the parent node overflows, recursively split and promote upward.
- If the root overflows, split the root and create a new root with the promoted median. This is the only way a B-Tree increases in height (growing bottom-up).
5. Detailed Step-by-Step Solved Problem: Insertionβ
Problem Statementβ
Construct an empty B-Tree of order by inserting the following sequence of keys:
Given Parametersβ
- Order .
- Maximum child pointers per node: .
- Maximum keys per node: keys.
- Minimum keys per internal/leaf node: key.
Stepwise Execution Traceβ
- Insert 5:
[ 5 ] - Insert 10:
[ 5 | 10 ] (Node is full: contains 2 keys) - Insert 12:
- Overflows temporarily:
[ 5 | 10 | 12 ](3 keys > max 2). - Find median: Median of is .
- Push up as the new root. Left child gets , right child gets .
[ 10 ]
/ \
[ 5 ] [ 12 ] - Overflows temporarily:
- Insert 13:
- , so insert into right leaf
[ 12 ]:
[ 10 ]
/ \
[ 5 ] [ 12 | 13 ] - , so insert into right leaf
- Insert 14:
- , insert into right leaf temporary overflow:
[ 12 | 13 | 14 ]. - Median is . Promote up to parent
[ 10 ]. - Right leaf splits into
[ 12 ]and[ 14 ].
[ 10 | 13 ]
/ | \
[ 5 ] [ 12 ] [ 14 ] - , insert into right leaf temporary overflow:
- Insert 1:
- , insert into left leaf
[ 5 ]:
[ 10 | 13 ]
/ | \
[ 1 | 5 ] [ 12 ] [ 14 ] - , insert into left leaf
- Insert 2:
- , insert into left leaf temporary overflow:
[ 1 | 2 | 5 ]. - Median is . Promote up to parent.
- But parent
[ 10 | 13 ]already has 2 keys! - Parent overflows temporarily:
[ 2 | 10 | 13 ]. - Median of parent is . Promote up to form a new root!
[ 10 ]
/ \
[ 2 ] [ 13 ]
/ \ / \
[ 1 ] [ 5 ] [ 12 ] [ 14 ] - , insert into left leaf temporary overflow:
- Insert 3:
- falls between and , so insert into leaf
[ 5 ]:
[ 10 ]
/ \
[ 2 ] [ 13 ]
/ \ / \
[ 1 ] [ 3 | 5 ] [ 12 ] [ 14 ] - falls between and , so insert into leaf
- Insert 4:
- falls between and , insert into leaf
[ 3 | 5 ]temporary overflow:[ 3 | 4 | 5 ]. - Median is . Promote to parent
[ 2 ]. - Parent becomes
[ 2 | 4 ]. Leaf splits into[ 3 ]and[ 5 ].
[ 10 ]
/ \
[ 2 | 4 ] [ 13 ]
/ | \ / \
[ 1 ] [ 3 ] [ 5 ] [ 12 ] [ 14 ] - falls between and , insert into leaf
Final Result: The tree is perfectly balanced, all leaves are at depth 2, and every node satisfies the invariant of having between and keys!
6. B-Tree Deletion Protocolβ
When deleting a key from a B-Tree, we must guarantee that no node drops below the minimum key requirement ( keys).
Case 1: Deletion from a Leaf Node:
- If the leaf has more than the minimum required keys, simply remove .
- If the leaf drops below minimum keys (Underflow):
- Borrow from Sibling (Rotation): If an adjacent sibling has an extra key, borrow the sibling's key via the parent node.
- Merge with Sibling: If siblings have only minimum keys, merge the node with a sibling and pull down the separating key from the parent.
Case 2: Deletion from an Internal Node:
- Replace the deleted key with either its In-order Predecessor (largest key in its left subtree) or In-order Successor (smallest key in its right subtree).
- Then recursively delete the predecessor/successor from the leaf node using Case 1.
7. Mathematical Solved Derivation: B-Tree Order & Fanoutβ
Problemβ
Consider a disk with block size . Search key size is , block pointer size is , and record pointer size is . Calculate the maximum order of the B-Tree.
Derivationβ
Apply the physical capacity inequality: Substitute the given values:
Taking the floor:
Conclusion: The maximum order of this B-Tree is . Every node can hold at most child pointers and search keys.
π Architecture / Visual Blueprintβ
π In The Real World: Production Case Studyβ
SQLite & Embedded Database Storage Enginesβ
While enterprise database engines (like MySQL InnoDB and Postgres) utilize B+ Trees for tabular indices, SQLiteβthe most widely deployed database in the worldβuses traditional B-Trees for table storage:
- SQLite's
.sqlite3file is divided into pages of . - Table B-Tree pages store the row data (payload) directly inside the interior and leaf pages alongside the integer
ROWID. - For point queries (
SELECT * FROM users WHERE rowid = 42), if the record is found in an internal node at depth 1, SQLite can terminate search immediately without traversing down to the leaf level! - However, this design incurs a severe trade-off for range scans (
SELECT * FROM users WHERE rowid BETWEEN 10 AND 100), which requires complex in-order tree traversal instead of scanning contiguous leaf blocks.
π― Exam & Interview Pitfall Checkβ
Question 1: Why does a B-Tree grow upward from the bottom, unlike standard binary search trees?
Answer: In standard binary search trees, new elements are inserted by attaching new leaf nodes downward below existing leaves, which can cause the tree to become unbalanced or skewed. In a B-Tree, insertion always occurs into existing leaf nodes. When a leaf exceeds capacity ( keys), it splits into two leaves and promotes its median key upward into its parent. Height increases only when the root itself overflows and splits, creating a new root above the previous one. This bottom-up growth guarantees that all leaf nodes remain at the exact same depth.
Question 2: In a B-Tree of order , what is the worst-case number of nodes created when inserting a single new key?
Answer: If the tree currently has height (with levels numbered from to ), and every ancestor node along the insertion path is already completely full ( keys), inserting one key triggers cascading splits at every level:
- The leaf node splits (1 new node created).
- All intermediate internal nodes split ( new nodes created).
- The root splits (1 new node created, plus 1 new root node created). Total new nodes created in worst case:
Trap 1: Confusing Order with Key Capacity. The order represents the maximum number of child pointers, not the maximum number of keys. A node of order contains at most keys.
Trap 2: Forgetting Record Pointers in B-Tree Capacity Calculations. Unlike B+ Trees, internal nodes in a B-Tree store record pointers alongside each key. When calculating node capacity, you must include for every key slot, which substantially reduces the branching factor.