Proofs: 2-3 tree
These are the human proofs of 2-3 tree, at full length.
Insertion and deletion keep the 2-3 tree invariant full
This proof shows: correctness.
Write for the set of keys in the subtree and for the number of nodes on a path from down to a leaf. A subtree is valid when it satisfies (T1) each node has one or two keys in increasing order, (T2) an internal node with keys has children, and for keys the child (counted from 0) holds only keys between and , with and , and (T3) all paths from the root of the subtree to a leaf have the same number of nodes. In a valid subtree, is well defined.
Insertion
Lemma 1 (INS). Let be a valid subtree and a key. After INS:
- if , the result is nil and is unchanged;
- else, if the result is nil, is valid with and the same ;
- else the result is , where and are valid with , each key of is less than , each key of is greater than , and .
Proof. By induction on . If is a key of , line L4 returns nil. Else, if is a leaf, line L5 puts into the sorted list of ; if is in a deeper node, then is internal and the case is handled below.
Leaf. After line L5, has two or three keys in order. With two keys, line L13 returns nil, and is a valid leaf with . With three keys , line L12 returns , two leaves, which satisfy part 3.
Internal node. Line L7 picks the child whose range holds ; by (T2), if is in the tree below , it is in . By the induction hypothesis on , which has : if , the result is nil and nothing changed. If line L8 returns nil with , then is valid with the new key and the same depth, and lies in the range of child , so is valid; line L9 returns nil. Else line L8 returns . Line L10 puts at position of and replaces by and . Since and the keys of and come from , they lie in the range of , between and ; so the keys of stay in order, and , lie in the ranges that cuts out of the old range. The number of children is one more than before, as is the number of keys, and , so (T2) and (T3) hold. If now has two keys, line L13 returns nil. If it has three keys and four children , line L12 returns , the node with , and the node with . By (T2) for the 4-node, the keys of and are less than and those of , are greater, and the two new nodes satisfy (T1) to (T3) with depth .
INSERT. If the tree is empty, line L1 makes a valid leaf. Else, by Lemma 1, either the root is valid with the new key, or line L3 makes a new root with the children and . This root is valid: and have the same depth, and their keys lie on the right sides of . All leaves move one level down together.
Deletion
An empty node is a node with no keys and one child (internal) or none (leaf). A subtree is nearly valid when it is valid, or its root is empty and its only child is valid (or it is a single empty leaf). Define of an empty node as 1 plus of its child, or 1 for an empty leaf.
Lemma 2 (FIX). Let be a node whose children are valid, except that is nearly valid with an empty root, and all children have the same . Let the keys of and its children satisfy (T2). After FIX, all children of are valid with the same as before, the keys in the subtree of are the same, the keys of and its children satisfy (T2), and has one key less than before or the same number.
Proof. Assume the sibling is and is the key between them; the case is the mirror image. Every key in is less than and every key in is greater.
Borrow (lines L22 and L23). Let have keys and, if it is internal, children . Then gets the key and, after its own child , the child ; becomes ; and keeps with . The keys of are less than (they were in ), and the keys of are between and (they were in , below ). So is a valid 2-node, and it lies below the new separator . The node is a valid 2-node above . Since and had the same depth, is as before. has the same number of keys.
Merge (lines L24 and L25). Let have one key and, if internal, children . The merged node has keys and children , with the keys of below , those of between and , and those of above . It is a valid 3-node with the old depth. The key and the child leave , which keeps one child fewer and one key fewer, so the key ranges of are still right.
Lemma 3 (DEL). Let be valid. After DEL, the subtree of is nearly valid, holds , and has the same as before.
Proof. By induction on . Leaf (line L17). Removing leaves zero or one key, or two when was absent; the result is a leaf, so it is nearly valid with .
Internal node. If is a key of , line L19 finds the least key of the child by going to the first child until a leaf. By (T2), and every other key of that subtree is greater than . So after , the order (T2) holds at for the tree in which the child still holds , and it is enough to delete from it, which line L20 does with . If is not a key of , line L18 picks the child whose range holds , and can only be there. By the induction hypothesis, after line L20 the child is nearly valid with the right keys and the same depth. If it is not empty, is valid. If it is empty, line L21 calls FIX, and by Lemma 2 all children of are valid with the old depth. Then has at least zero keys and at most its old number, and its children satisfy (T2) and (T3), so the subtree of is nearly valid: either is valid, or lost its only key in a merge and has one child, which is valid.
DELETE. By Lemma 3, after line L15 the root is valid or empty. If it is empty, line L16 replaces it by its only child, which is valid, or by nil. All leaves move one level up together, so (T3) holds.
Conclusion
Theorem. Starting from the empty tree, after any sequence of insertions and deletions the tree is a valid 2-3 tree that holds exactly the keys inserted and not deleted since, and a search returns true exactly for these keys.
Proof. The empty tree is valid. INSERT and DELETE keep the tree valid and change the key set as stated, by Lemma 1 and Lemma 3. A search at a node either finds among its keys, or goes to the child whose range holds ; by (T2), can be in the subtree only there. It reaches a leaf after steps.