Module 8: Storage & Indexing · Lecture 37 · Week 13
B+ Tree: Insertions & Deletions
Splits, redistributions, and merges: how the B+ tree maintains its invariants as data changes
Learning Objectives
- Execute a B+ tree insertion step-by-step, handling the no-overflow and overflow (split) cases
- Execute a B+ tree deletion, handling no-underflow, redistribution, and merge cases
- Explain how splits propagate upward and how a tree grows in height
- Analyze the I/O cost of insert and delete operations on a B+ tree
1. Insertion into a B+ Tree
Inserting a key-RID pair into a B+ tree always begins by finding the correct leaf node (exactly as in a search). If the leaf has space, the entry is inserted in sorted order. If the leaf is full (2d entries), it must be split.
1.1 Case 1: No Overflow (Leaf Has Space)
1. Traverse from root to the correct leaf (log_d n I/Os)
2. Insert (key, RID) into the leaf in sorted order
3. Write the modified leaf back to disk
I/O cost: log_d(n) reads + 1 write to leaf + possible write to parent (if parent pointer updated)
1.2 Case 2: Leaf Overflow: Split
If the target leaf is full (2d entries), inserting would violate the maximum-fill constraint. We must split the leaf into two nodes:
1. Temporarily place all 2d+1 entries (existing + new) in sorted order
2. Create a new leaf node
3. Move the first ⌊(2d+1)/2⌋ entries to the left node
4. Move the remaining ⌈(2d+1)/2⌉ entries to the right node
5. Copy the smallest key of the right node up to the parent (as a separator key)
6. Update the linked list pointers (left.next = new_right, new_right.next = old_right.next)
Copy-Up vs. Push-Up
Leaf splits copy-up: the separator key is copied to the parent AND kept in the right leaf. This is because leaf nodes hold actual data entries; we cannot remove them.
Internal node splits push-up: the middle key is pushed to the parent and removed from the internal node. Internal nodes are routers, not data holders.
1.3 Case 3: Internal Node Overflow: Split Propagates Up
When a leaf split pushes a key up to the parent internal node, that parent may also overflow (if it had 2d entries). The process repeats recursively:
1.4 Tree Height Growth
If a split propagates all the way to the root and the root overflows, a new root is created containing just one key (the pushed-up separator) and two child pointers. This is the only way a B+ tree grows taller: it grows from the root upward, not from the leaves downward.
1.5 Insertion I/O Cost
| Case | I/O Cost |
|---|---|
| No split (leaf has space) | height reads + 1 write |
| Leaf split only | height reads + 3–4 writes (old leaf, new leaf, parent) |
| Split propagates k levels | height reads + O(k) writes |
| Root splits (height increases) | height reads + O(height) writes (rare) |
2. Deletion from a B+ Tree
Deletion begins by locating the entry in the leaf node. Once found, it is removed. If the leaf still has at least d entries, we are done. If it falls below d entries (underflow), we must either redistribute entries from a sibling or merge with a sibling.
2.1 Case 1: No Underflow
1. Find and remove the entry from the leaf
2. The leaf still has ≥ d entries → no structural change needed
3. Write the modified leaf back to disk
4. Note: if the deleted key was a separator key copied into a parent, the separator can remain (it still routes correctly, though it no longer exists as a leaf entry)
2.2 Case 2: Underflow: Redistribute from Sibling
If deletion causes a leaf to drop below d entries, check an adjacent sibling. If the sibling has more than d entries, we can redistribute:
2.3 Case 3: Underflow: Merge with Sibling
If both the underflowing node and its sibling have exactly d entries, redistribution is impossible (each would then have fewer than d entries). We must merge the two nodes into one:
Merges Propagate Downward in Height
A leaf merge removes a separator key from the parent internal node. This may cause the parent to underflow, triggering another redistribution or merge one level up. If the root ends up with zero keys (only one child), it is deleted and the tree shrinks by one level. This is the only way a B+ tree decreases in height.
2.4 Deletion I/O Cost
| Case | I/O Cost |
|---|---|
| No underflow | height reads + 1 write |
| Redistribute (leaf level) | height reads + 3 writes (two leaves + parent) |
| Merge at leaf level | height reads + 2–3 writes (merged leaf + parent) |
| Merge propagates k levels | height reads + O(k) writes |
3. Bulk Loading a B+ Tree
Building a B+ tree by inserting n records one at a time costs O(n log n) I/Os. For large tables, bulk loading is far more efficient:
- Sort all records by the index key (external merge sort)
- Fill leaf pages to a target fill factor (e.g., 80%) to leave room for future inserts
- For each leaf page, extract the first key and build internal nodes bottom-up
Cost: O(n) I/Os (dominated by the external sort). Bulk loading is the standard method for building indexes on existing data.
4. Summary
- B+ tree insertion: find leaf, insert, split if overflow (copy-up for leaves, push-up for internal nodes)
- Splits propagate upward; the tree grows by creating a new root when the root splits
- B+ tree deletion: find leaf, delete, redistribute if possible, else merge (pull-down separator from parent)
- Merges propagate upward; the tree shrinks by deleting the root when it becomes empty
- All operations cost O(log_d n) I/Os; bulk loading costs O(n) I/Os and should be preferred for initial index construction