Proofs: AVL tree
These are the human proofs of AVL tree, at full length.
Insertion and deletion keep the AVL invariant full
This proof shows: correctness, rotations.insert.
We write for the set of keys of the subtree , for its height (), and for the tree with root key , left subtree and right subtree . A tree is AVL when it satisfies (A1) the search-tree order, (A2) correct stored heights and (A3) at each node. Rotations keep the in-order sequence of the keys and recompute the heights of the two moved nodes from their children, lower node first (see tree rotation).
Lemma 1 (REBALANCE). Let be a node whose subtrees and are AVL, with each key of less than and each key of greater, and with . Then REBALANCE returns an AVL tree with the same in-order sequence of keys, and of height when , and of height or when .
Proof. Line L6 sets correctly. If , line L13 returns , which is AVL with the stated height.
Let , and write , so and is not nil. Let and be the left and right subtrees of . Since is AVL, and .
Case . Then and . Line L8 does nothing and line L9 rotates left at . The result is . The node has subtrees of heights and , so its balance factor is and its height is . The node has subtrees of heights and , so its balance factor is and its height is , which is or . Here .
Case . Then and . So is a node, with subtrees and such that and . Line L8 rotates right at and line L9 rotates left at . The result is . The node has subtrees of heights and : its balance factor is , and its height is . The node has subtrees of heights and : its balance factor is , and its height is . The root has two subtrees of height , balance factor 0 and height .
In both cases the subtrees , , , , are unchanged AVL trees, the three rebuilt nodes satisfy (A3), and their heights are recomputed from children whose heights are already right, so (A2) holds. Rotations keep the in-order sequence, so (A1) holds and the keys are the same. The case is the mirror image, with lines L11 and L12.
Lemma 2 (INSERT). If is AVL, then INSERT returns an AVL tree with and . Moreover, if is not nil and , then the root of has a nonzero balance factor, and no rotation was done at the root of .
Proof. By induction on . If , line L1 returns a single node, which is AVL, with height . If , line L4 returns unchanged.
Let (the case is the mirror image). Write and for the old subtrees, so and . By the induction hypothesis, line L2 makes , an AVL tree with keys , all less than , and . So , and Lemma 1 applies to line L5. The keys are .
No rotation at . Then , and , which is or . It is only if and . Then , which is nonzero.
A rotation at . Then . Since , this forces and , so . The tree is not nil, because , and grew, so by the induction hypothesis . In the proof of Lemma 1 (mirrored), the only case with height is the one with . So : the height did not grow.
Corollary (one rebalancing per insertion). An insertion does rotations at one node at most, one single rotation or one double rotation. Proof. Let be the lowest node of the path where REBALANCE rotates. By Lemma 2, the subtree of has the same height after the insertion as before. So each ancestor of has children of the same heights as before, its balance factor is the old one, which is in , and REBALANCE does not rotate there.
Lemma 3 (DELETE). If is AVL, then DELETE returns an AVL tree with and (with when is nil).
Proof. By induction on . If , line L14 returns nil. Write , , , as before.
Case (the case is the mirror image). By the induction hypothesis, line L15 makes , AVL, with keys and . So and Lemma 1 applies at line L22. The keys are . By Lemma 1, . If REBALANCE does not rotate, . If it rotates, then , so , hence and ; and .
Case and . Line L18 returns , an AVL tree. Since and , . Its keys are , since the keys are distinct. The case (line L19) is the mirror image.
Case with two children. Line L20 finds the node of least key in . Line L21 sets and, by the induction hypothesis applied to , makes , AVL, with keys and . Each key of is less than , which is less than , and each key of is greater than , because was the least key of and the keys are distinct. So the hypotheses of Lemma 1 hold, and the height bound follows as in the first case. The keys are .
Theorem. Starting from the empty tree, after any sequence of insertions and deletions the tree is AVL and holds exactly the keys inserted and not deleted since. CONTAINS returns true exactly when is in the tree. Each insertion does at most one single or one double rotation.
Proof. The empty tree is AVL. Lemmas 2 and 3, applied to the root, keep the tree AVL and give the stated key sets. CONTAINS is the search of a binary search tree, which is correct by (A1): at each node, a key less than the node's key can only be in the left subtree and a greater key only in the right subtree. Each recursive call works on a child, whose height is one less, so each operation stops after at most calls. The bound on rotations is the corollary.