Proofs: Weight-balanced tree
These are the human proofs of Weight-balanced tree, at full length.
Weight-balanced tree operations keep the weight rule full
This proof shows: correctness.
The weight of a tree is its number of keys plus one, so and for a node . A tree is balanced when each node satisfies
This is the rule with ; the rotation test uses . A pair of trees is nearly balanced when
If satisfies (B) and then one key is added to or removed from one of them, the new pair satisfies (P): adding a key to gives , removing one from gives , and the other side is only easier. NODE makes a node with the right size, so the stored sizes are always correct, and we do not mention them again.
Lemma 1 (BALANCE). Let and be balanced search trees with every key of less than and every key of greater, and let satisfy (P). Then BALANCE returns a balanced search tree with the keys of , then , then the keys of , in order.
Proof. Rotations keep the in-order sequence of the keys, so only (B) needs a proof, and only at the nodes that BALANCE builds; all other nodes are nodes of and . If lines L1 and L2 both fail, (B) holds at the new root, and line L3 is right.
Let line L1 succeed, so with . Then , so is a node; let and , so by (P), and , by (B) at . Line L2 cannot also succeed.
Single rotation, (line L4). The new nodes are , with weights and , and the root , with weights and .
- : from we get , and gives , so .
- : , so .
- : , so .
- : .
Double rotation, (line L5). Then , so is a node; let and be the weights of its subtrees, so , and by (B) at , and , hence . The new nodes are with weights and , with weights and , and the root with weights and . From and we have . Also , so .
- : .
- : from , , so , which is at most when . When , , so .
- : .
- : .
- : , and , which is at most because .
- : , so , because .
So (B) holds at the three new nodes. The case where line L2 succeeds is the mirror image.
Lemma 2 (INSERT). If is a balanced search tree, INSERT returns a balanced search tree with the keys of and , of size or .
Proof. By induction on the size of . Line L6 returns one node. If , the induction hypothesis makes the new left subtree balanced, with the keys of and , all less than , and with at most one more key. So the pair of subtrees satisfies (P), and Lemma 1 applies to line L7. Line L8 is the mirror image, and if the tree is returned unchanged.
Lemma 3 (DELETE-MIN, DELETE-MAX). For a balanced nonempty , DELETE-MIN returns the least key of and a balanced search tree with the other keys. The same holds for DELETE-MAX with the largest key.
Proof. By induction. If is nil, is the least key, and is balanced. Otherwise the induction hypothesis gives the least key of , which is the least of , and a balanced left subtree with one key fewer, so Lemma 1 applies.
Lemma 4 (GLUE). Let and be the subtrees of a balanced node, without its key. GLUE (lines L11 and L12) returns a balanced search tree with the keys of and .
Proof. If one side is nil, the other is returned. Let and , both at least 2, with and . If , then DELETE-MAX takes the largest key of , which is greater than the rest of and less than all of , and leaves a tree of weight . Then and , so the pair satisfies (B), and by Lemma 1 the result is right. If , DELETE-MIN on leaves weight , and the same argument applies.
Lemma 5 (DELETE). If is a balanced search tree, DELETE returns a balanced search tree with the keys of other than , of size or .
Proof. By induction. If , the induction hypothesis gives a balanced left subtree with at most one key fewer, so (P) holds and Lemma 1 applies to line L9; the case is the mirror image. If , line L10 returns GLUE of the two subtrees, which is right by Lemma 4.
Height. At a node of a balanced tree, , so , and the same holds on the right. A node at depth has weight at least 2, so and .
Theorem. Starting from the empty tree, after any sequence of insertions and deletions the tree is balanced, holds exactly the keys inserted and not deleted, and has height at most . Search, rank and select are correct.
Proof. The empty tree is balanced. Lemmas 2 and 5 keep it balanced with the stated keys. The height bound is the paragraph above. Search is the search of a binary search tree. Select and rank are correct because each stored size is the number of keys in its subtree, as for an order-statistic tree.