Proofs: 2-3-4 tree
These are the human proofs of 2-3-4 tree, at full length.
Top-down insertion and deletion keep the 2-3-4 tree valid full
This proof shows: correctness.
A tree is valid when (B1) each node has 1, 2 or 3 sorted keys, (B2) an internal node with keys has children, (B3) the keys are in search-tree order, so all keys of child lie strictly between the keys and of the node (with and the last bound ), and (B4) all leaves are at the same depth. During a deletion we allow two exceptions for a moment: the root may hold no key and have one child (after a merge at the root), and a key may have two copies, as explained in Lemma 5. We write for the set of keys of the subtree .
Lemma 1 (SPLIT-CHILD). Let be a node with at most 2 keys whose child has the 3 keys . Lines L10 and L11 give a valid tree with the same keys, the same depth of every leaf, and with one key more.
Proof. The new left node gets and the two left children of , the new right node gets and the two right children; each is a 2-node (or a leaf with one key), so (B1) and (B2) hold for them. The key goes into at position , and the two new nodes become children and . Then has at most 3 keys and one more child than before, so (B1) and (B2) hold. All keys of the left node are less than and greater than , and all keys of the right node are greater than and less than , so (B3) holds. Both new nodes are at the depth of , and the children of keep their depths, so (B4) holds.
Lemma 2 (MERGE). Let have at least 2 keys, or be the root, and let and be 2-nodes. Line L26 gives a valid tree with the same keys and leaf depths, in which is a 4-node and has one key less. If was the root with one key, it now has no key and the single child .
Proof. The node gets the keys , , with by (B3), and the four children of the two nodes in order. (B1) to (B3) hold for it, and the leaf depths do not change. The node loses one key and one child.
Lemma 3 (FILL). Let have at least 2 keys, or be the root, and let be a 2-node. Lines L23 to L25 give a valid tree with the same keys and leaf depths, in which the child that holds the old key of and covers the same key range has at least 2 keys, and the returned index points to it.
Proof. At line L23 the left sibling has at least 2 keys. Its last key goes up into position of , the old goes down to the front of , and the last child of becomes the first child of . The keys of that child lie between and , which are now the bounds of the first child slot of , so (B3) holds; the child keeps its depth. keeps at least 1 key and now has 2. Line L24 is the mirror image. At line L25 both siblings that exist are 2-nodes, and Lemma 2 merges with one of them into a 4-node, whose index is returned. In each case the keys that covered are covered by the returned child.
Lemma 4 (INSERT). If the tree is valid and , INSERT gives a valid tree with keys . If , it gives a valid tree with keys .
Proof. If the root is nil, line L1 makes a leaf with one key. If the root is a 4-node, line L2 puts an empty root above it and splits it by Lemma 1; all leaves move down by one level together, so (B4) holds and the new root has one key.
Invariant. At each test of line L5, the tree is valid, its keys are , is not a 4-node, and if is in the tree or is to be placed, its place is in the subtree of : all keys of 's subtree range contain by (B3).
It holds at the first test, since the root is not a 4-node after line L2. If is a key of , line L6 returns. If is a leaf, it has at most 2 keys, and line L7 inserts at the position that keeps it sorted; gets at most 3 keys, and (B3) holds because lies in the range of . Else let be the child whose range contains . If it is a 4-node, line L8 splits it by Lemma 1, which is allowed because is not a 4-node; the middle key goes into at position . If it equals , the line returns; else lies in the range of one of the two new children, and points to it. That child is a 2-node. If was not a 4-node, it is not one now. Line L9 steps down, and the invariant holds at the next test. Each step goes one level down, so the loop stops at a leaf.
Lemma 5 (DELETE). If the tree is valid, DELETE gives a valid tree with keys for the key given.
Proof. Write for the key given. During the loop, is either or a replacement key, as explained below.
Invariant. At each test of line L14: (D1) the tree satisfies (B1) to (B4), except that the root may have no key and one child, and that in (B3) the two copies of of (D3) count as equal bounds; (D2) is the root or has at least 2 keys; (D3) either and the keys of the tree are , or , an ancestor of holds in the place of , and a second copy of is the largest (or the smallest) key of the subtree of , in a leaf of it, so the keys are with counted twice; (D4) the range of contains .
It holds at the first test with the root. If is a leaf, line L15 removes if it is there. By (D2), keeps at least 1 key unless it is the root, which may become empty only when it held the last key of the tree. In the first case of (D3) the keys are now , or when was absent. In the second case the duplicate copy of is gone, and the keys are .
If is internal and holds at position (line L16), there are three cases. At line L17, has at least 2 keys. The largest key of its subtree is the predecessor of ; writing over keeps (B3) in the sense of (D1), because is greater than all other keys of 's subtree and less than the keys to the right. Now and , which has at least 2 keys and whose range contains , so (D2) to (D4) hold. From here on, is the largest key of the subtree of , so it is not a key of an internal node on the path, and the path follows the last child at each level. There, line L20 can only borrow from the left sibling, which moves a key of that sibling up, or merge with the left sibling, which moves a key of down; neither moves . So stays in the last leaf, line L16 does not find it again, and (D3) holds at each step. Line L18 is the mirror image, with the successor, the smallest key, and the first child at each level. At line L19 both children are 2-nodes, and has at least 2 keys or is the root, so Lemma 2 applies: moves down into the merged 4-node, and becomes that node.
If is internal and does not hold , the child whose range contains is reached. If it is a 2-node, line L20 makes the child that covers hold at least 2 keys, by Lemma 3, which applies by (D2). Line L21 steps down, and (D2) and (D4) hold.
Each step goes one level down, so the loop reaches a leaf. Line L22 removes an empty root and makes its only child the root; all leaves move up by one level together, so (B4) holds and (D1) gives a valid tree. If the tree held only , the root was a leaf and becomes nil.
Theorem. Starting from the empty tree, after any sequence of insertions and deletions the tree is a valid 2-3-4 tree that holds exactly the keys inserted and not deleted since. CONTAINS returns true exactly for these keys.
Proof. The empty tree is valid. Lemmas 4 and 5 keep it valid and give the stated key sets. CONTAINS follows (B3): at each node, the key can lie only in the child whose range contains it, and it stops at a leaf after at most steps.