Sub topic 6
Deleting keys: borrowing and merging
This chapter assumes you know how a B-tree is laid out and searched, and how an insert splits a full node. Deletion is the mirror image of that. Inserts fix overfull nodes by splitting. Deletes fix underfull nodes by borrowing or merging.
The invariant deletion must protect
A B-tree of minimum degree t keeps every non-root node between t - 1 and 2t - 1 keys. The lower bound keeps pages reasonably full and keeps the tree shallow. The root is exempt and may hold as few as one key.
Removing a key from a leaf can drop that leaf below the minimum. The tree then has to repair itself before the delete is finished.
Three cases
Take t = 2, so each non-root node holds 1 to 3 keys.
- The leaf has spare keys. Remove the key. Nothing else changes.
- The leaf is at the minimum, but a sibling has a spare key. Borrow.
- The leaf and its siblings are all at the minimum. Merge.
Borrowing (rotation)
Borrowing moves one key through the parent. The parent's separator key moves down into the underfull node. The sibling's nearest key moves up to replace that separator. If the nodes are internal, the child pointer next to that key moves too.
Suppose the parent holds [10], the left leaf holds [3, 5, 8], and the right leaf holds [12]. Delete 12. The right leaf is now empty, so we rotate:
- Separator 10 moves down into the right leaf.
- Key 8 moves up to become the separator.
The result is parent [8], left leaf [3, 5], and right leaf [10]. Only three pages were touched, and the tree height did not change.
Merging
If no sibling can spare a key, borrowing would only push the problem onto the sibling. Instead, combine the underfull node, a sibling, and the separator from the parent into one node.
Suppose the parent holds [10, 20] and the leaves are [5], [12], and [25]. Delete 12. The middle leaf is empty, and its neighbors are at the minimum. Merge it with the left leaf and pull down the separator 10. The leaf becomes [5, 10], and the parent becomes [20].
The merge removed a key from the parent, so the parent may now be underfull. The repair then repeats one level up. In the worst case it reaches the root. If the root ends up with no keys, its only child becomes the new root, and the tree gets one level shorter. This is the only way a B-tree loses height.
Deleting from an internal node
In a classic B-tree, internal nodes hold real keys. To delete one, replace it with its in-order predecessor (the largest key in the left subtree) or successor, then delete that key from the leaf. The leaf case above then applies.
In a B+tree, the leaves hold all the data. Deleting a key removes it from its leaf. A copy of that key may stay in an internal node as a separator, which is harmless, because separators only guide the search.
Common errors
- Forgetting the cascade. A merge shrinks the parent, so the underfull check must continue upward.
- Borrowing from a sibling at the minimum. That only moves the underflow to the sibling.
- Treating the root like other nodes. The root has no minimum, and it is the only node that can disappear.
- Assuming databases always rebalance. Many engines are lazy. PostgreSQL removes dead entries during vacuum and frees only pages that become fully empty. It does not eagerly merge half-empty pages, so heavy deletes leave sparse pages. The chapter on fill factor and bloat returns to this.
Exercise
A B-tree with t = 2 has a parent [10, 20] and leaves [5], [12, 15], [25]. You delete 5. Which repair applies, and what are the parent and leaves afterward?
Show answer
The leaf [5] becomes empty, and its right sibling [12, 15] has a spare key, so borrow. Separator 10 moves down into the left leaf, and 12 moves up to replace it. The result is parent [12, 20] with leaves [10], [15], and [25].