Proofs: B-tree
These are the human proofs of B-tree, at full length.
Search, insert and delete keep the B-tree properties and give the right answers full
This proof shows: correctness, termination.
We use the pseudocode of the page and the minimum degree . The keys are distinct. For a node , is the set of keys in the subtree of , and is the number of edges from down to a leaf of its subtree. A subtree is valid if it satisfies these properties at each of its nodes :
(B1) and . If is internal, it has children. (B2) If is internal, each key of is larger than (when ) and smaller than (when ). (B3) All leaves of the subtree of have the same depth. (B4) , except at the root of the subtree, where if is internal.
A tree is a B-tree when the subtree of its root is valid. By (B2), an in-order walk lists in increasing order, and for each child, by (B3).
Lemma 1 (search). If the subtree of is valid, B-TREE-SEARCH returns with when , and NIL otherwise. It makes at most disk reads.
Proof. By induction on . Lines L1 and L2 stop at the least with or . So when , and when . If line L3 finds , the answer is right. Otherwise is not a key of , because the keys of before position are smaller than and those from position on are larger. If is a leaf, is the keys of , and line L4 returns NIL correctly. If is internal, by (B2) every key of that lies strictly between and is in , and lies there, so if and only if . The child has height , and its subtree is valid (with the weaker root rule of (B4), which the search does not use). The induction hypothesis gives the answer, with at most reads, plus the one read of line L5. In the base case , is a leaf and no read happens.
Lemma 2 (split). Let be an internal node that is not full, and let be full, with keys. After SPLIT-CHILD, the subtree of has the same key set and the same leaf depths, and the new node have keys each, has one key more, and (B1) to (B3) still hold at each node.
Proof. Write for the keys of and for its child subtrees, if any. Lines L14 to L17 leave and in , and put and in . Lines L18 and L19 put into at position , with to its left and to its right. No key is lost: moved up, and the others stay below. (B1) holds at and , which have keys and children each if internal. At , grows by one, to at most because was not full, and the keys stay sorted: by (B2) at before the split, lies between and the old . (B2) holds at and because their children kept their old neighbouring keys, and at because the keys of are below and those of are above it. The subtrees keep their depth below , so (B3) holds.
Lemma 3 (insert into a node that is not full). Let the subtree of be valid, let be not full, and let . Then INSERT-NONFULL makes the subtree of hold , keeps it valid, keeps the depth of its leaves, and does not touch a node outside it.
Proof. By induction on . If is a leaf, lines L23 and L24 shift the keys larger than one place right and put in the gap. The keys stay sorted, and because was not full. If is internal, line L25 finds the child whose key range, by (B2), contains . If that child is full, line L28 splits it, which is allowed because is not full, and Lemma 2 keeps the subtree valid. After the split, is in the range of or of , and line L29 picks the right one by comparing with the new key , which is not because . In both cases the chosen child is not full: it was not full, or it is one of the two halves with keys. Its subtree is valid and has height , so the induction hypothesis applies to line L30. The child keeps at least keys and gains at most one, so (B4) holds there, and (B2) holds at because went into the right range.
Theorem 1 (insert). If is a B-tree and is not in , then after B-TREE-INSERT the tree is a B-tree with the keys of and .
Proof. If the root is not full, line L13 applies Lemma 3 to . If is full, lines L9 and L10 make a new root with no key and the single child , and line L11 splits . Lemma 2 applies to , which is not full, and leaves with one key and two children with keys each, so (B4) holds. All leaves are now one level deeper, all by the same amount, so (B3) holds. Then line L13 applies Lemma 3 to .
Lemma 4 (delete). Let the subtree of be valid, and let be the root of the tree or have at least keys. Then B-TREE-DELETE makes the subtree of hold , keeps (B1) to (B3) at each of its nodes and (B4) at each of its nodes other than , removes at most one key from , keeps the depth of its leaves, and does not touch a node outside it.
Proof. By induction on . Line L31 finds the least with , or .
Case 1, line L33. is a key of the leaf . Removing it keeps the keys sorted and removes one key.
Case 2a, line L35. , is internal and has at least keys. Let be the largest key of . By (B2), is the largest key of below . The deletion of from touches only the subtree of , so it gives the same tree whether is done before it or after it. Do it before it in the analysis order "delete, then replace". The node has at least keys, so the induction hypothesis applies: the subtree of becomes valid on , with at least keys at . All these keys are below , and is above every key of . So writing in place of keeps (B1) and (B2) at . The number of keys of does not change.
Case 2b, line L36. The same with the smallest key of , , which has at least keys.
Case 2c, line L37. Both and have keys. The new is , then , then , with keys and, if internal, children. Its keys are sorted by (B2) at , so (B1) holds. loses the key and the child . The children of and have the same depth by (B3), so (B3) holds. Now has keys and , and the induction hypothesis deletes from the subtree of , leaving at least keys at .
Line L38. is not a key of and is a leaf, so , and nothing changes.
Case 3, lines L39 to L41. is not a key of , is internal, and by (B2) only if . If has at least keys, the induction hypothesis applies to it directly. Else has keys. In case 3a a sibling, say the left one , has at least keys. The key moves down to the front of , the last key of moves up in its place, and the last child of becomes the first child of . The in-order sequence of the subtree of is the same as before, so (B1) and (B2) hold, no depth changes, keeps at least keys, and now has . The right sibling is the mirror image. In case 3b both siblings that exist have keys, and is merged with one of them and the key of between them, as in case 2c. The merged node has keys, loses one key, and (B1) to (B3) hold as in case 2c. In both cases, line L42 continues at a child with at least keys, whose key range contains , and the induction hypothesis applies.
In each case loses at most one key, so it keeps at least keys if it had at least . Each child that the procedure leaves behind kept or gained keys, or is the node of the recursive call, which keeps (B4) by the hypothesis. A node outside the subtree of is never touched, because each step changes only and its children. In the base case , is a leaf and only case 1 or line L38 applies.
Theorem 2 (delete). If is a B-tree, then after B-TREE-DELETE and the final check, is a B-tree on its old keys without .
Proof. Lemma 4 applies to the root, whatever its number of keys. Afterwards (B1) to (B4) hold at each node except that the root may have 0 keys. If the root is a leaf with 0 keys, the tree is empty. If it is internal with 0 keys, it has exactly one child by (B1), and the final check makes that child the new root. All leaves become one level less deep, all by the same amount, so (B3) holds, and the new root has at least keys.
Termination. Each recursive call, or each round of the loop in the code, moves from a node to one of its children, or to a node in the subtree of a child, so of the current node drops by at least 1. The search for the largest key of in case 2a also moves down one level at each step. So each operation stops after at most rounds of each kind, where is the height of the tree.