Algorithm Search⌘K

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 K(t)K(t) for the set of keys of the subtree tt, h(t)h(t) for its height (h(nil)=0h(\text{nil}) = 0), and (L,x,R)(L, x, R) for the tree with root key xx, left subtree LL and right subtree RR. A tree is AVL when it satisfies (A1) the search-tree order, (A2) correct stored heights and (A3) bf∈{−1,0,1}bf \in \{-1, 0, 1\} 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 tt be a node whose subtrees L=t.leftL = t.left and R=t.rightR = t.right are AVL, with each key of LL less than t.keyt.key and each key of RR greater, and with ∣h(R)−h(L)∣≤2|h(R) - h(L)| \le 2. Then REBALANCE(t)(t) returns an AVL tree with the same in-order sequence of keys, and of height 1+max⁡(h(L),h(R))1 + \max(h(L), h(R)) when ∣h(R)−h(L)∣≤1|h(R) - h(L)| \le 1, and of height max⁡(h(L),h(R))\max(h(L), h(R)) or max⁡(h(L),h(R))+1\max(h(L), h(R)) + 1 when ∣h(R)−h(L)∣=2|h(R) - h(L)| = 2.

Proof. Line L6 sets t.ht.h correctly. If ∣bf(t)∣≤1|bf(t)| \le 1, line L13 returns tt, which is AVL with the stated height.

Let bf(t)=2bf(t) = 2, and write p=h(L)p = h(L), so h(R)=p+2≥2h(R) = p + 2 \ge 2 and RR is not nil. Let aa and bb be the left and right subtrees of r=Rr = R. Since RR is AVL, max⁡(h(a),h(b))=p+1\max(h(a), h(b)) = p + 1 and ∣h(a)−h(b)∣≤1|h(a) - h(b)| \le 1.

Case bf(r)≥0bf(r) \ge 0. Then h(b)=p+1h(b) = p + 1 and h(a)∈{p,p+1}h(a) \in \{p, p + 1\}. Line L8 does nothing and line L9 rotates left at tt. The result is ((L,t,a),r,b)((L, t, a), r, b). The node tt has subtrees of heights pp and h(a)h(a), so its balance factor is h(a)−p∈{0,1}h(a) - p \in \{0, 1\} and its height is h(a)+1∈{p+1,p+2}h(a) + 1 \in \{p + 1, p + 2\}. The node rr has subtrees of heights h(a)+1h(a) + 1 and p+1p + 1, so its balance factor is p−h(a)∈{−1,0}p - h(a) \in \{-1, 0\} and its height is h(a)+2h(a) + 2, which is p+2p + 2 or p+3p + 3. Here max⁡(h(L),h(R))=p+2\max(h(L), h(R)) = p + 2.

Case bf(r)=−1bf(r) = -1. Then h(a)=p+1h(a) = p + 1 and h(b)=ph(b) = p. So aa is a node, with subtrees cc and dd such that max⁡(h(c),h(d))=p\max(h(c), h(d)) = p and h(c),h(d)∈{p−1,p}h(c), h(d) \in \{p - 1, p\}. Line L8 rotates right at rr and line L9 rotates left at tt. The result is ((L,t,c),a,(d,r,b))((L, t, c), a, (d, r, b)). The node tt has subtrees of heights pp and h(c)h(c): its balance factor is h(c)−p∈{−1,0}h(c) - p \in \{-1, 0\}, and its height is p+1p + 1. The node rr has subtrees of heights h(d)h(d) and pp: its balance factor is p−h(d)∈{0,1}p - h(d) \in \{0, 1\}, and its height is p+1p + 1. The root aa has two subtrees of height p+1p + 1, balance factor 0 and height p+2=max⁡(h(L),h(R))p + 2 = \max(h(L), h(R)).

In both cases the subtrees LL, aa, bb, cc, dd 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 bf(t)=−2bf(t) = -2 is the mirror image, with lines L11 and L12. □\square

Lemma 2 (INSERT). If tt is AVL, then INSERT(t,k)(t, k) returns an AVL tree t′t' with K(t′)=K(t)∪{k}K(t') = K(t) \cup \{k\} and h(t′)∈{h(t),h(t)+1}h(t') \in \{h(t), h(t) + 1\}. Moreover, if tt is not nil and h(t′)=h(t)+1h(t') = h(t) + 1, then the root of t′t' has a nonzero balance factor, and no rotation was done at the root of t′t'.

Proof. By induction on h(t)h(t). If t=nilt = \text{nil}, line L1 returns a single node, which is AVL, with height 1=h(t)+11 = h(t) + 1. If k=t.keyk = t.key, line L4 returns tt unchanged.

Let k<t.keyk < t.key (the case k>t.keyk > t.key is the mirror image). Write p=h(L)p = h(L) and q=h(R)q = h(R) for the old subtrees, so ∣p−q∣≤1|p - q| \le 1 and h(t)=1+max⁡(p,q)h(t) = 1 + \max(p, q). By the induction hypothesis, line L2 makes t.left=L′t.left = L', an AVL tree with keys K(L)∪{k}K(L) \cup \{k\}, all less than t.keyt.key, and h(L′)∈{p,p+1}h(L') \in \{p, p + 1\}. So ∣h(L′)−q∣≤2|h(L') - q| \le 2, and Lemma 1 applies to line L5. The keys are K(t)∪{k}K(t) \cup \{k\}.

No rotation at tt. Then ∣h(L′)−q∣≤1|h(L') - q| \le 1, and h(t′)=1+max⁡(h(L′),q)h(t') = 1 + \max(h(L'), q), which is h(t)h(t) or h(t)+1h(t) + 1. It is h(t)+1h(t) + 1 only if h(L′)=p+1h(L') = p + 1 and p≥qp \ge q. Then bf(t′)=q−(p+1)≤−1bf(t') = q - (p + 1) \le -1, which is nonzero.

A rotation at tt. Then h(L′)=q+2h(L') = q + 2. Since p≤q+1p \le q + 1, this forces h(L′)=p+1h(L') = p + 1 and p=q+1p = q + 1, so h(t)=p+1h(t) = p + 1. The tree LL is not nil, because p=q+1≥1p = q + 1 \ge 1, and L′L' grew, so by the induction hypothesis bf(L′)≠0bf(L') \ne 0. In the proof of Lemma 1 (mirrored), the only case with height max⁡(h(L′),q)+1\max(h(L'), q) + 1 is the one with bf(L′)=0bf(L') = 0. So h(t′)=max⁡(h(L′),q)=p+1=h(t)h(t') = \max(h(L'), q) = p + 1 = h(t): the height did not grow. □\square

Corollary (one rebalancing per insertion). An insertion does rotations at one node at most, one single rotation or one double rotation. Proof. Let tt be the lowest node of the path where REBALANCE rotates. By Lemma 2, the subtree of tt has the same height after the insertion as before. So each ancestor of tt has children of the same heights as before, its balance factor is the old one, which is in {−1,0,1}\{-1, 0, 1\}, and REBALANCE does not rotate there. □\square

Lemma 3 (DELETE). If tt is AVL, then DELETE(t,k)(t, k) returns an AVL tree t′t' with K(t′)=K(t)∖{k}K(t') = K(t) \setminus \{k\} and h(t′)∈{h(t)−1,h(t)}h(t') \in \{h(t) - 1, h(t)\} (with h(t′)=0h(t') = 0 when tt is nil).

Proof. By induction on h(t)h(t). If t=nilt = \text{nil}, line L14 returns nil. Write LL, RR, p=h(L)p = h(L), q=h(R)q = h(R) as before.

Case k<t.keyk < t.key (the case k>t.keyk > t.key is the mirror image). By the induction hypothesis, line L15 makes t.left=L′t.left = L', AVL, with keys K(L)∖{k}K(L) \setminus \{k\} and h(L′)∈{p−1,p}h(L') \in \{p - 1, p\}. So ∣h(L′)−q∣≤2|h(L') - q| \le 2 and Lemma 1 applies at line L22. The keys are K(t)∖{k}K(t) \setminus \{k\}. By Lemma 1, h(t′)≤max⁡(h(L′),q)+1≤h(t)h(t') \le \max(h(L'), q) + 1 \le h(t). If REBALANCE does not rotate, h(t′)=1+max⁡(h(L′),q)≥1+max⁡(p−1,q)≥max⁡(p,q)=h(t)−1h(t') = 1 + \max(h(L'), q) \ge 1 + \max(p - 1, q) \ge \max(p, q) = h(t) - 1. If it rotates, then q=h(L′)+2q = h(L') + 2, so q≥p+1q \ge p + 1, hence q=p+1q = p + 1 and h(t)=q+1h(t) = q + 1; and h(t′)≥max⁡(h(L′),q)=q=h(t)−1h(t') \ge \max(h(L'), q) = q = h(t) - 1.

Case k=t.keyk = t.key and t.left=nilt.left = \text{nil}. Line L18 returns RR, an AVL tree. Since bf(t)∈{0,1}bf(t) \in \{0, 1\} and p=0p = 0, h(R)=q=h(t)−1h(R) = q = h(t) - 1. Its keys are K(t)∖{k}K(t) \setminus \{k\}, since the keys are distinct. The case t.right=nilt.right = \text{nil} (line L19) is the mirror image.

Case k=t.keyk = t.key with two children. Line L20 finds the node mm of least key in RR. Line L21 sets t.key=m.keyt.key = m.key and, by the induction hypothesis applied to RR, makes t.right=R′t.right = R', AVL, with keys K(R)∖{m.key}K(R) \setminus \{m.key\} and h(R′)∈{q−1,q}h(R') \in \{q - 1, q\}. Each key of LL is less than kk, which is less than m.keym.key, and each key of R′R' is greater than m.keym.key, because m.keym.key was the least key of RR 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 K(L)∪{m.key}∪(K(R)∖{m.key})=K(t)∖{k}K(L) \cup \{m.key\} \cup (K(R) \setminus \{m.key\}) = K(t) \setminus \{k\}. □\square

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(k)(k) returns true exactly when kk 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 h(t)+1h(t) + 1 calls. The bound on rotations is the corollary. ■\blacksquare

Back to AVL tree