Algorithm Search⌘K

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 xx has an integer rank r(x)r(x), with r(nil)=−1r(\text{nil}) = -1. For a child xx of pp, the rank difference is δ(x)=r(p)−r(x)\delta(x) = r(p) - r(x). A node whose children have rank differences ii and jj is an i,ji,j node. The tree is a WAVL tree when:

  1. (W1) it is a binary search tree;
  2. (W2) every rank difference is 1 or 2;
  3. (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 xx takes the place of pp with the rank pp had, the rank difference of that place toward the parent of pp 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 xx of rank 0 as in a binary search tree. Before, its place held nil, with difference r(p)+1∈{1,2}r(p) + 1 \in \{1, 2\}, so r(p)∈{0,1}r(p) \in \{0, 1\}. If r(p)=1r(p) = 1, now δ(x)=1\delta(x) = 1 and all rules hold. If r(p)=0r(p) = 0, then δ(x)=0\delta(x) = 0.

Invariant at line L2. (W1) and (W3) hold, and (W2) holds except that δ(x)\delta(x) may be 0. If δ(x)=0\delta(x) = 0, then either xx is a new leaf, whose parent was a leaf, or xx is a 1,2 node whose child of difference 1 is the previous xx.

Promotion (line L3). Let δ(x)=0\delta(x) = 0 and let ss be the sibling of xx, with δ(s)=1\delta(s) = 1. Promoting pp makes δ(x)=1\delta(x) = 1 and δ(s)=2\delta(s) = 2, so pp is a 1,2 node whose difference-1 child is xx, and pp is not a leaf. Only δ(p)\delta(p) drops by one, to 0 or 1. So the invariant holds with x←px \leftarrow p. A new leaf xx always takes this case: its parent pp has rank 0, so pp was a leaf, ss is nil, and δ(s)=0−(−1)=1\delta(s) = 0 - (-1) = 1.

Rotation (lines L4 and L5). Let δ(x)=0\delta(x) = 0 and δ(s)=2\delta(s) = 2, so by the remark above xx is a 1,2 node, not a new leaf. Let R=r(p)=r(x)R = r(p) = r(x), so r(s)=R−2r(s) = R - 2. Let xx be the left child of pp; the other case is the mirror image. Let z=x.leftz = x.left (outer) and y=x.righty = x.right (inner).

  • Single rotation when δ(y)=2\delta(y) = 2: then r(y)=R−2r(y) = R - 2 and r(z)=R−1r(z) = R - 1. Rotate xx above pp and demote pp to R−1R - 1. Now xx, of rank RR, has children zz and pp, both of rank R−1R - 1; pp has children yy and ss, both of rank R−2R - 2. All differences are 1. If R=1R = 1, then yy and ss are nil and pp is a leaf of rank 0, as (W3) asks.
  • Double rotation when δ(y)=1\delta(y) = 1: then r(y)=R−1r(y) = R - 1, r(z)=R−2r(z) = R - 2, and yy is the previous xx, a leaf or a 1,2 node, so its children aa (left) and bb (right) have ranks in {R−2,R−3}\{R - 2, R - 3\}. Rotate yy above xx and then above pp; promote yy to RR and demote xx and pp to R−1R - 1. Now yy has children xx and pp, with differences 1; xx has children zz (difference 1) and aa (1 or 2); pp has children bb (1 or 2) and ss (difference 1). If R=1R = 1, all of zz, aa, bb, ss are nil and xx and pp are leaves of rank 0.

In both cases the new top has rank RR, the old rank of pp, so all rules hold and the loop stops (line L6).

Termination. Each promotion moves xx up one level. The loop ends at the root, after a promotion that leaves δ(p)=1\delta(p) = 1, 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 zz has at most one child; that child, or nil, takes its place as xx, under pp.

  • If zz was a leaf with δ(z)=1\delta(z) = 1, then r(p)=1r(p) = 1 and the new nil child has difference 2. If pp is now a leaf, it is a leaf of rank 1; line L8 demotes it to 0, which raises δ(p)\delta(p) to 2 or 3, and sets x←px \leftarrow p.
  • If zz was a leaf with δ(z)=2\delta(z) = 2, the nil child now has difference 3.
  • If zz had one child, then r(z)=1r(z) = 1 and its child is a leaf cc of rank 0. Now δ(c)=δ(z)+1∈{2,3}\delta(c) = \delta(z) + 1 \in \{2, 3\}.

Invariant at line L9. (W1) and (W3) hold, and (W2) holds except that δ(x)\delta(x) may be 3.

Let δ(x)=3\delta(x) = 3, R=r(p)R = r(p), so r(x)=R−3r(x) = R - 3 and R≥2R \ge 2. Let yy be the sibling of xx; since δ(y)≤2\delta(y) \le 2, r(y)≥0r(y) \ge 0, so yy is a node. Let xx be the left child; the other case is the mirror image.

Demote pp (line L10) when δ(y)=2\delta(y) = 2. Then δ(x)=2\delta(x) = 2 and δ(y)=1\delta(y) = 1, and pp still has the child yy, so it is not a leaf. Only δ(p)\delta(p) grows, to 2 or 3. The invariant holds with x←px \leftarrow p.

Demote pp and yy (line L11) when δ(y)=1\delta(y) = 1 and yy is a 2,2 node. Here yy has rank R−1R - 1 and is not a leaf by (W3), so its children have rank R−3≥0R - 3 \ge 0 and are both nodes. After the demotions, δ(x)=2\delta(x) = 2, δ(y)=1\delta(y) = 1, and yy is a 1,1 node of rank R−2≥1R - 2 \ge 1. Only δ(p)\delta(p) grows, to 2 or 3. The invariant holds with x←px \leftarrow p.

Single rotation (line L12) when δ(y)=1\delta(y) = 1, yy is not 2,2, and its outer child z=y.rightz = y.right has δ(z)=1\delta(z) = 1. So r(y)=R−1r(y) = R - 1, r(z)=R−2r(z) = R - 2, and the inner child v=y.leftv = y.left has rank R−2R - 2 or R−3R - 3. Rotate yy above pp, promote yy to RR and demote pp to R−1R - 1. Now yy has children pp (difference 1) and zz (difference 2); pp has children xx (difference 2) and vv (1 or 2). If pp is now a leaf, then xx and vv are nil, R=2R = 2, and pp has rank 1; it is demoted again to 0, and then δ(p)=2\delta(p) = 2. All rules hold and the loop stops.

Double rotation (line L13) when δ(y)=1\delta(y) = 1, δ(z)=2\delta(z) = 2, and so, since yy is not 2,2, δ(v)=1\delta(v) = 1. Then r(y)=R−1r(y) = R - 1, r(z)=R−3r(z) = R - 3, r(v)=R−2r(v) = R - 2, and the children aa (left) and bb (right) of vv have ranks in {R−3,R−4}\{R - 3, R - 4\}. Rotate vv above yy and then above pp; promote vv twice to RR, demote yy to R−2R - 2 and pp twice to R−2R - 2. Now vv has children pp and yy, both with difference 2; pp has children xx (difference 1) and aa (1 or 2); yy has children bb (1 or 2) and zz (difference 1). If R=2R = 2, then vv was a leaf, xx, aa, bb, zz are nil, and pp and yy are leaves of rank 0. All rules hold and the loop stops (line L14).

In both rotation cases the new top has rank RR, so the difference toward the parent is unchanged.

Termination. Lines L10 and L11 move xx up one level. The loop ends at the root, when δ(x)≤2\delta(x) \le 2, 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 2lg⁡(n+1)−22 \lg(n + 1) - 2 for n≥1n \ge 1. 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 mkm_k be the least number of nodes in a subtree whose root has rank kk. Then m0=1m_0 = 1, m1=2m_1 = 2 (a node of rank 1 is not a leaf), and mk≥2mk−2+1m_k \ge 2 m_{k-2} + 1, since both children have rank at least k−2k - 2. By induction, mk≥2k/2+1−1m_k \ge 2^{k/2 + 1} - 1. So the root rank kk satisfies k≤2lg⁡(n+1)−2k \le 2 \lg(n + 1) - 2.

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. ■\blacksquare

Back to WAVL tree