IIT Madras Zanzibar · Teaching

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

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)

Algorithm:
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)
Example: Insert key=38 into a leaf [30, 35, __, __] (d=2, max 2d=4 entries, currently 2) Before: [30 | 35 | __ | __] After: [30 | 35 | 38 | __] ← simple insert, no overflow

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:

Leaf Split Algorithm:
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)
Example (d=2): Insert key=40 into full leaf [30 | 35 | 38 | 42] Temp sorted list: [30, 35, 38, 40, 42] Left leaf (keep): [30 | 35 | 38] Right leaf (new): [40 | 42] Push up to parent: key=40 (smallest of right leaf, COPY: key stays in leaf too)

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:

Internal node split (push-up example, d=2): Full internal: [P0 | 20 | P1 | 35 | P2 | 48 | P3 | 60 | P4] New key=40 must be inserted → overflow Temp: [20, 35, 40, 48, 60] with 6 pointers Middle key = 40 ← PUSH UP (removed from internal node) Left internal: [P0 | 20 | P1 | 35 | P2] (keys < 40) Right internal: [P3 | 48 | P4 | 60 | P5] (keys ≥ 48) Push up: 40 → to parent

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

CaseI/O Cost
No split (leaf has space)height reads + 1 write
Leaf split onlyheight reads + 3–4 writes (old leaf, new leaf, parent)
Split propagates k levelsheight 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

Algorithm:
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:

Leaf underflow after delete: [30 | __] (d=2, need ≥ 2 entries) Right sibling has spare: [40 | 45 | 50] Redistribute: Left: [30 | 40] Right: [45 | 50] Update parent separator: change 40 → 45

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:

Leaf underflow after delete: [30 | __] (has 1, needs ≥ 2) Right sibling also sparse: [45 | __] (has 1, exactly d) Merge → [30 | 45] (one leaf, update linked list, delete parent separator) Parent loses a key → may trigger underflow in parent → recurse upward

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

CaseI/O Cost
No underflowheight reads + 1 write
Redistribute (leaf level)height reads + 3 writes (two leaves + parent)
Merge at leaf levelheight reads + 2–3 writes (merged leaf + parent)
Merge propagates k levelsheight 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:

  1. Sort all records by the index key (external merge sort)
  2. Fill leaf pages to a target fill factor (e.g., 80%) to leave room for future inserts
  3. 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