Proofs: WAVL tree
These are the human proofs of WAVL tree, at full length.
WAVL insertion and deletion keep the rank rules full
This proof shows: correctness.
Each node has an integer rank , with . For a child of , the rank difference is . A node whose children have rank differences and is an node. The tree is a WAVL tree when:
- (W1) it is a binary search tree;
- (W2) every rank difference is 1 or 2;
- (W3) every leaf has rank 0.
A rotation keeps the in-order sequence of the keys and so keeps (W1); we only check (W2) and (W3) at the nodes whose children or ranks change. If a node takes the place of with the rank had, the rank difference of that place toward the parent of is unchanged. Two facts follow from the rules: a node that is not a leaf has rank at least 1, and a node with exactly one child has rank 1, with a leaf child (difference 1) and a nil child (difference 2).
Insertion
Lines L1 to L6 first add a leaf of rank 0 as in a binary search tree. Before, its place held nil, with difference , so . If , now and all rules hold. If , then .
Invariant at line L2. (W1) and (W3) hold, and (W2) holds except that may be 0. If , then either is a new leaf, whose parent was a leaf, or is a 1,2 node whose child of difference 1 is the previous .
Promotion (line L3). Let and let be the sibling of , with . Promoting makes and , so is a 1,2 node whose difference-1 child is , and is not a leaf. Only drops by one, to 0 or 1. So the invariant holds with . A new leaf always takes this case: its parent has rank 0, so was a leaf, is nil, and .
Rotation (lines L4 and L5). Let and , so by the remark above is a 1,2 node, not a new leaf. Let , so . Let be the left child of ; the other case is the mirror image. Let (outer) and (inner).
- Single rotation when : then and . Rotate above and demote to . Now , of rank , has children and , both of rank ; has children and , both of rank . All differences are 1. If , then and are nil and is a leaf of rank 0, as (W3) asks.
- Double rotation when : then , , and is the previous , a leaf or a 1,2 node, so its children (left) and (right) have ranks in . Rotate above and then above ; promote to and demote and to . Now has children and , with differences 1; has children (difference 1) and (1 or 2); has children (1 or 2) and (difference 1). If , all of , , , are nil and and are leaves of rank 0.
In both cases the new top has rank , the old rank of , so all rules hold and the loop stops (line L6).
Termination. Each promotion moves up one level. The loop ends at the root, after a promotion that leaves , or after one rotation case. At the end all rules hold. An insertion does at most two rotations.
Deletion
Line L7 deletes as in a binary search tree. A node with two children takes the key of its successor, which has no left child, and that node is removed instead. So the removed node has at most one child; that child, or nil, takes its place as , under .
- If was a leaf with , then and the new nil child has difference 2. If is now a leaf, it is a leaf of rank 1; line L8 demotes it to 0, which raises to 2 or 3, and sets .
- If was a leaf with , the nil child now has difference 3.
- If had one child, then and its child is a leaf of rank 0. Now .
Invariant at line L9. (W1) and (W3) hold, and (W2) holds except that may be 3.
Let , , so and . Let be the sibling of ; since , , so is a node. Let be the left child; the other case is the mirror image.
Demote (line L10) when . Then and , and still has the child , so it is not a leaf. Only grows, to 2 or 3. The invariant holds with .
Demote and (line L11) when and is a 2,2 node. Here has rank and is not a leaf by (W3), so its children have rank and are both nodes. After the demotions, , , and is a 1,1 node of rank . Only grows, to 2 or 3. The invariant holds with .
Single rotation (line L12) when , is not 2,2, and its outer child has . So , , and the inner child has rank or . Rotate above , promote to and demote to . Now has children (difference 1) and (difference 2); has children (difference 2) and (1 or 2). If is now a leaf, then and are nil, , and has rank 1; it is demoted again to 0, and then . All rules hold and the loop stops.
Double rotation (line L13) when , , and so, since is not 2,2, . Then , , , and the children (left) and (right) of have ranks in . Rotate above and then above ; promote twice to , demote to and twice to . Now has children and , both with difference 2; has children (difference 1) and (1 or 2); has children (1 or 2) and (difference 1). If , then was a leaf, , , , are nil, and and are leaves of rank 0. All rules hold and the loop stops (line L14).
In both rotation cases the new top has rank , so the difference toward the parent is unchanged.
Termination. Lines L10 and L11 move up one level. The loop ends at the root, when , or after one rotation case. A deletion does at most two rotations.
Conclusion
Theorem. Starting from the empty tree, after any sequence of insertions and deletions the tree is a WAVL tree on exactly the keys inserted and not deleted, and each update does at most two rotations. The height is at most for . If no deletion has been done, the tree is an AVL tree, and each rank is the height of its node.
Proof. The empty tree satisfies the rules, and the two parts above show that each update keeps them; the key sets change as in a binary search tree.
Height. Each rank difference is at least 1 and leaves have rank 0, so the height of a node is at most its rank. Let be the least number of nodes in a subtree whose root has rank . Then , (a node of rank 1 is not a leaf), and , since both children have rank at least . By induction, . So the root rank satisfies .
Insertions only. We show that insertions never create a 2,2 node. A new leaf is 1,1. A promotion raises one difference to 2, at the promoted node, whose other difference becomes 1. The rotations leave 1,1 and 1,2 nodes, as listed above. Every other change lowers a difference. So every node is 1,1 or 1,2, and by induction on the height, each rank is $1 + $ the larger rank of its children, which is the height. The differences of heights are then 1 or 2 at each node, which is the AVL rule.