Sub topic 12
B-tree Search, Insertion with Splits, and Deletion
Prerequisites: you should know balanced search trees, binary search, logarithms, and why database systems read disk blocks rather than individual records. This chapter uses order \(m\) to mean that a node has at most \(m\) children and \(m-1\) keys. Some books use “order” differently, so always check the convention in a question.
Searching a B-tree
A B-tree keeps keys sorted inside every node. A node with keys \(k1, k2, \ldots, kq\) has up to \(q+1\) child pointers. The child before \(k1\) contains smaller keys. The child between \(ki\) and \(k{i+1}\) contains keys in that interval. The final child contains larger keys.
Search proceeds from the root:
- Compare the target with the sorted keys in the current node.
- Return the record if the target is found.
- Otherwise follow the child pointer for the correct interval.
- Repeat until the key is found or a leaf proves that it is absent.
All leaves have the same depth. Therefore, a search visits at most one node per tree level. If the height is \(h\), the search usually costs \(h+1\) block accesses when the root is not cached.
Insertion and node splits
Insert a new key into the appropriate leaf. If the leaf has room, place the key in sorted order. If the node is full, split it into two nodes:
- Combine the old keys and the new key in sorted order.
- Promote the median key to the parent.
- Keep smaller keys in the left node and larger keys in the right node.
- Insert the new child pointer into the parent.
- Split the parent too if it is now full.
A split can move upward repeatedly. If the root splits, create a new root. This increases the tree height by one while preserving balance.
For minimum degree \(t\), a non-root node contains between \(t-1\) and \(2t-1\) keys. A full node has \(2t-1\) keys. Splitting promotes the median and leaves \(t-1\) keys in each resulting child.
> callout note > Do not copy the promoted median into both B-tree children. In a B-tree, the median moves to the parent. B+ trees use a different split rule because their actual records remain in leaves.
Deletion and rebalancing
Deletion is more difficult because removing a key can leave too few keys in a node.
- If the key is in a leaf with more than the minimum number of keys, remove it directly.
- If the key is in an internal node, replace it with its predecessor or successor, then delete that replacement from a leaf or lower subtree.
- Before descending into a child with the minimum number of keys, try to borrow a key from an adjacent sibling through the parent.
- If neither sibling can lend a key, merge the child with a sibling and pull the separating parent key down.
- If the root becomes empty, replace it with its only child. The height then decreases by one.
Borrowing preserves the number of levels. Merging may propagate a shortage toward the root.
| Operation | Main structural action | Possible height change |
|---|---|---|
| Search | Follow one interval per level | None |
| Insertion | Split full nodes and promote medians | Increases only when the root splits |
| Deletion | Borrow or merge underfull nodes | Decreases only when the root becomes empty |
Worked strategy
For a numerical question, first identify the order convention. Then write the minimum and maximum key counts, draw the affected path, and apply splits or merges only along that path. Finally count levels, not individual key comparisons, when the question asks for block accesses.
Exercise
A B-tree has minimum degree t = 2. A full leaf contains keys 10, 20, and 30. What happens when 25 is inserted?
Show answer
The temporary sorted keys are 10, 20, 25, and 30. Promote a median according to the implementation's split rule, leaving two nodes whose keys are separated by the promoted key. The parent receives the promoted separator and may split if it was already full.