Proofs: AA tree
These are the human proofs of AA tree, at full length.
AA tree insertion and deletion keep the level rules full
This proof shows: correctness.
Write for the level of , with . A subtree is valid when it is a binary search tree and every node satisfies
- (A2) ,
- (A3) ,
- (A4) when .
The rules (A1), a leaf has level 1, and (A5), a node above level 1 has two children, follow: a leaf has , so by (A2); and if , both children have level at least 1 by (A2) and (A3), so they are not nil. A link from to a child of the same level is horizontal; (A2) forbids horizontal left links, and (A4) forbids two horizontal right links in a row. Lines L1 to L5 are SKEW and SPLIT; a rotation keeps the in-order sequence, so the search order is never at risk, and the proofs below only check levels. "No-op" means that SKEW or SPLIT finds its test false and returns its argument. On a valid subtree, both are no-ops, since their tests are exactly the negations of (A2) and (A4) at the root.
Insertion
Lemma 1. Let be valid with (with when is nil), and let be absent. INSERT returns a valid subtree with the keys of and , and . If , then both children of have level , and is nil or has a right child of level .
Proof. By induction on the height. If is nil, line L6 returns a leaf of level whose children have level .
Let be a node, with of level and .
Left case (line L7 goes left), with the result. By induction . If , the rules at are as before, SKEW and SPLIT are no-ops, and is returned at level . If , both children of have level , and SKEW rotates: becomes the top, its right child is , and becomes the old , of level . Now is valid at level , and has a left child of level and the horizontal right child . If , (A4) holds at , SPLIT is a no-op, and the result has level . If , SPLIT rotates left at : becomes the top at level , with left child and right child , both of level . The node now has two children of level , and (A4) at holds because by the old (A4) at . This is the case , and had the right child of level .
Right case, with the result. If , then by induction , and if , its right child has level . Either way (A3) and (A4) hold at , SKEW and SPLIT are no-ops, and the result has level . If , then is a node whose right child has level less than by (A4) at , so has no right child of level , and by induction . (A3) holds. If , (A4) holds and is returned at level . If , SPLIT rotates left at : becomes the top at level , with left child and right child , both of level . The node has the children and the old , both of level , and (A4) holds at because is valid. This is the case , and had the right child of level . SKEW at line L8 is a no-op in the right case, because did not change.
Deletion
Lemma 2. Let be valid with , and let be a key of . DELETE returns a valid subtree with the keys of without , and
- (a) ;
- (b) if has no right child of level and , then has no right child of level ;
- (c) if has a right child of level , then .
Proof. By induction on the height. If is a leaf holding , line L10 returns nil, of level ; (b) and (c) hold since has no right child. Otherwise lines L9, L11 or L12 replace one child of by the result of a recursive call on it, and maybe replace by the least key of or the largest key of , which is then deleted from that child. In each case the keys are those of without , and the search order holds: is less than all other keys of and greater than the keys of , and symmetrically for . At line L11, is nil, so , and is a node of level 1. Let , of level , and , of level or .
Case L1: the left child, . DECREASE-LEVEL computes and changes nothing. All rules at hold as before, and lines L14 and L15 are no-ops: SKEW at because , and SKEW and SPLIT at the right side because those subtrees are unchanged and valid, and SPLIT at because (A4) holds at . The result is at level with the same right child, so (b) and (c) hold.
Case L2: the left child, . Lines L16 and L17 set , and lower to if it was at .
L2a: before. The children of have levels and or . The three SKEWs are no-ops: and are one level below their parents, and is valid. If , SPLIT is a no-op and is returned at level : its children have levels and , and (A4) holds. If , SPLIT rotates left at : becomes the top at level with left child and right child of level . The node , at level , has children and the old , both of level . (A4) at holds since . The second SPLIT, at , is a no-op. The result has level and no right child of level , as (b) needs.
L2b: before. By (A4) at and (A3) at , both children of had level ; call them and . After line L17, , , and all have level . The node has a left child of level and a right child of level or ; if , then by (A4) at the children of have level . The first SKEW, at , is a no-op. The second, at , rotates right: takes the place of , and becomes the right child of with left child . The third, at again: if , it rotates right, so is the right child of , and the right child of with a left child of level . Now the right spine from is , all at level , and each of them has a left child of level ; after comes , of level or .
The SPLIT at rotates left, because (the node or ) has level : becomes the top at level , with left child , which now has two children of level . The SPLIT at then acts on the node after .
- If : and have level , so it rotates left and rises to level , with children and of level . The node has two children of level . (A4) at holds since , and (A4) at holds since .
- If and : it rotates left, and rises to level , with children (whose children have level ) and of level . (A4) holds at , since , and at , since is valid.
- If and : it is a no-op. The node has children of levels and , and (A4) at holds; (A4) at holds since .
In all three cases the result is valid at level , as (c) needs.
Case R1: the right child, before. By (A4) at , has no right child of level . By induction , and if , by (b) has no right child of level . DECREASE-LEVEL computes and changes nothing. (A3) and (A4) hold at , all SKEWs and SPLITs are no-ops, and is returned at level , as (c) needs.
Case R2: the right child, before, . As in case L1, nothing changes at , (A4) holds because , and is returned at level with a right child of level , as (b) needs.
Case R3: the right child, before, . DECREASE-LEVEL sets ; is lower and does not change. Now , of level , is a horizontal left child. Its children have level and, for , or ; if , the children of have level by (A4) at . The first SKEW rotates right at : is the top, with right child , and .
- If : the second and third SKEWs are no-ops ( is one below , and is valid), and SPLIT at is a no-op since . The result is at level , with children of levels and ; has two children of level .
- If : the second SKEW rotates right at , so the right spine is at level , and has children of level . The third SKEW, at , is a no-op. SPLIT at rotates left: rises to level , with left child (children of level ) and right child of level . (A4) at holds since , and SPLIT at is a no-op. The result has level and no right child of level , as (b) needs.
These are all cases.
Conclusion
Theorem. Starting from the empty tree, after any sequence of insertions and deletions the tree is a valid AA tree with exactly the keys inserted and not deleted since, and its height is at most .
Proof. The class methods search first, so INSERT gets an absent key and DELETE a present one; Lemmas 1 and 2 at the root give a valid tree with the right keys. For the height: a node of level has at least nodes in its subtree, by induction, because its two children have level at least by (A2) and (A3). On a downward path, a left step lowers the level by one, and of two right steps in a row at least one lowers it, by (A4). So a path from the root, of level , has at most nodes, and gives .