Algorithm Search⌘K

Proofs: Weight-balanced tree

These are the human proofs of Weight-balanced tree, at full length.

Weight-balanced tree operations keep the weight rule full

This proof shows: correctness.

The weight of a tree is its number of keys plus one, so w(nil)=1w(\text{nil}) = 1 and w(t)=w(t.left)+w(t.right)w(t) = w(t.left) + w(t.right) for a node tt. A tree is balanced when each node tt satisfies

w(t.left)≤3 w(t.right)andw(t.right)≤3 w(t.left).(B)w(t.left) \le 3\, w(t.right) \quad\text{and}\quad w(t.right) \le 3\, w(t.left). \tag{B}

This is the rule with Δ=3\Delta = 3; the rotation test uses Γ=2\Gamma = 2. A pair of trees (l,r)(l, r) is nearly balanced when

w(l)≤3 w(r)+3andw(r)≤3 w(l)+3.(P)w(l) \le 3\, w(r) + 3 \quad\text{and}\quad w(r) \le 3\, w(l) + 3. \tag{P}

If (l,r)(l, r) satisfies (B) and then one key is added to or removed from one of them, the new pair satisfies (P): adding a key to rr gives w(r)≤3w(l)+1w(r) \le 3w(l) + 1, removing one from ll gives w(r)≤3(w(l)+1)w(r) \le 3(w(l) + 1), and the other side is only easier. NODE(l,k,r)(l, k, r) makes a node with the right size, so the stored sizes are always correct, and we do not mention them again.

Lemma 1 (BALANCE). Let ll and rr be balanced search trees with every key of ll less than kk and every key of rr greater, and let (l,r)(l, r) satisfy (P). Then BALANCE(l,k,r)(l, k, r) returns a balanced search tree with the keys of ll, then kk, then the keys of rr, in order.

Proof. Rotations keep the in-order sequence of the keys, so only (B) needs a proof, and only at the nodes that BALANCE builds; all other nodes are nodes of ll and rr. If lines L1 and L2 both fail, (B) holds at the new root, and line L3 is right.

Let line L1 succeed, so 3a<w(r)3a < w(r) with a=w(l)≥1a = w(l) \ge 1. Then w(r)>3w(r) > 3, so rr is a node; let b=w(r.left)b = w(r.left) and c=w(r.right)c = w(r.right), so 3a<b+c≤3a+33a < b + c \le 3a + 3 by (P), and b≤3cb \le 3c, c≤3bc \le 3b by (B) at rr. Line L2 cannot also succeed.

Single rotation, b<2cb < 2c (line L4). The new nodes are x=(l,k,r.left)x = (l, k, r.left), with weights aa and bb, and the root (x,r.key,r.right)(x, r.key, r.right), with weights a+ba + b and cc.

  • b≤3ab \le 3a: from b≤2c−1b \le 2c - 1 we get c≥(b+1)/2c \ge (b + 1)/2, and b+c≤3a+3b + c \le 3a + 3 gives 3b+1≤6a+63b + 1 \le 6a + 6, so b≤2a+1≤3ab \le 2a + 1 \le 3a.
  • a≤3ba \le 3b: 3a<b+c≤4b3a < b + c \le 4b, so a<4b/3≤3ba < 4b/3 \le 3b.
  • a+b≤3ca + b \le 3c: 3a≤b+c−13a \le b + c - 1, so 3(a+b)≤4b+c−1≤4(2c−1)+c−1<9c3(a + b) \le 4b + c - 1 \le 4(2c - 1) + c - 1 < 9c.
  • c≤3(a+b)c \le 3(a + b): c≤3bc \le 3b.

Double rotation, b≥2cb \ge 2c (line L5). Then b≥2b \ge 2, so r.leftr.left is a node; let dd and ee be the weights of its subtrees, so d+e=bd + e = b, and by (B) at r.leftr.left, d≤3ed \le 3e and e≤3de \le 3d, hence b/4≤d,e≤3b/4b/4 \le d, e \le 3b/4. The new nodes are x=(l,k,d-subtree)x = (l, k, d\text{-subtree}) with weights aa and dd, y=(e-subtree,r.key,r.right)y = (e\text{-subtree}, r.key, r.right) with weights ee and cc, and the root (x,r.left.key,y)(x, r.left.key, y) with weights a+da + d and e+ce + c. From b≥2cb \ge 2c and b≤3cb \le 3c we have b/3≤c≤b/2b/3 \le c \le b/2. Also 3a<b+c≤3b/23a < b + c \le 3b/2, so a<b/2a < b/2.

  • a≤3da \le 3d: 3d≥3b/4>b/2>a3d \ge 3b/4 > b/2 > a.
  • d≤3ad \le 3a: from b+b/3≤b+c≤3a+3b + b/3 \le b + c \le 3a + 3, b≤9(a+1)/4b \le 9(a + 1)/4, so d≤3b/4≤27(a+1)/16d \le 3b/4 \le 27(a + 1)/16, which is at most 3a3a when a≥2a \ge 2. When a=1a = 1, b≤4b \le 4, so d≤3=3ad \le 3 = 3a.
  • e≤3ce \le 3c: e≤3b/4≤9c/4e \le 3b/4 \le 9c/4.
  • c≤3ec \le 3e: c≤b/2<3b/4≤3ec \le b/2 < 3b/4 \le 3e.
  • a+d≤3(e+c)a + d \le 3(e + c): 3(e+c)≥3e+b=d+4e3(e + c) \ge 3e + b = d + 4e, and a+d<(d+e)/2+d=(3d+e)/2a + d < (d + e)/2 + d = (3d + e)/2, which is at most d+4ed + 4e because d≤7ed \le 7e.
  • e+c≤3(a+d)e + c \le 3(a + d): 3a≥b+c−3=d+e+c−33a \ge b + c - 3 = d + e + c - 3, so 3(a+d)≥4d+e+c−3≥e+c+13(a + d) \ge 4d + e + c - 3 \ge e + c + 1, because d≥1d \ge 1.

So (B) holds at the three new nodes. The case where line L2 succeeds is the mirror image. □\square

Lemma 2 (INSERT). If tt is a balanced search tree, INSERT(t,k)(t, k) returns a balanced search tree with the keys of tt and kk, of size ∣t∣|t| or ∣t∣+1|t| + 1.

Proof. By induction on the size of tt. Line L6 returns one node. If k<t.keyk < t.key, the induction hypothesis makes the new left subtree balanced, with the keys of t.leftt.left and kk, all less than t.keyt.key, and with at most one more key. So the pair of subtrees satisfies (P), and Lemma 1 applies to line L7. Line L8 is the mirror image, and if k=t.keyk = t.key the tree is returned unchanged. □\square

Lemma 3 (DELETE-MIN, DELETE-MAX). For a balanced nonempty tt, DELETE-MIN(t)(t) returns the least key mm of tt and a balanced search tree with the other keys. The same holds for DELETE-MAX with the largest key.

Proof. By induction. If t.leftt.left is nil, t.keyt.key is the least key, and t.rightt.right is balanced. Otherwise the induction hypothesis gives the least key of t.leftt.left, which is the least of tt, and a balanced left subtree with one key fewer, so Lemma 1 applies. □\square

Lemma 4 (GLUE). Let ll and rr be the subtrees of a balanced node, without its key. GLUE(l,r)(l, r) (lines L11 and L12) returns a balanced search tree with the keys of ll and rr.

Proof. If one side is nil, the other is returned. Let a=w(l)a = w(l) and c=w(r)c = w(r), both at least 2, with a≤3ca \le 3c and c≤3ac \le 3a. If ∣l∣>∣r∣|l| > |r|, then DELETE-MAX takes the largest key mm of ll, which is greater than the rest of ll and less than all of rr, and leaves a tree of weight a−1≥ca - 1 \ge c. Then a−1≤3ca - 1 \le 3c and c≤a−1≤3(a−1)c \le a - 1 \le 3(a - 1), so the pair satisfies (B), and by Lemma 1 the result is right. If ∣l∣≤∣r∣|l| \le |r|, DELETE-MIN on rr leaves weight c−1≥ac - 1 \ge a, and the same argument applies. □\square

Lemma 5 (DELETE). If tt is a balanced search tree, DELETE(t,k)(t, k) returns a balanced search tree with the keys of tt other than kk, of size ∣t∣|t| or ∣t∣−1|t| - 1.

Proof. By induction. If k<t.keyk < t.key, the induction hypothesis gives a balanced left subtree with at most one key fewer, so (P) holds and Lemma 1 applies to line L9; the case k>t.keyk > t.key is the mirror image. If k=t.keyk = t.key, line L10 returns GLUE of the two subtrees, which is right by Lemma 4. □\square

Height. At a node tt of a balanced tree, w(t.left)≤3 w(t.right)=3(w(t)−w(t.left))w(t.left) \le 3\,w(t.right) = 3(w(t) - w(t.left)), so w(t.left)≤34w(t)w(t.left) \le \frac34 w(t), and the same holds on the right. A node at depth hh has weight at least 2, so 2≤(3/4)h(n+1)2 \le (3/4)^h (n + 1) and h≤log⁡4/3n+12h \le \log_{4/3} \frac{n + 1}{2}.

Theorem. Starting from the empty tree, after any sequence of insertions and deletions the tree is balanced, holds exactly the keys inserted and not deleted, and has height at most log⁡4/3n+12<2.41lg⁡(n+1)\log_{4/3}\frac{n+1}{2} < 2.41 \lg (n + 1). Search, rank and select are correct.

Proof. The empty tree is balanced. Lemmas 2 and 5 keep it balanced with the stated keys. The height bound is the paragraph above. Search is the search of a binary search tree. Select and rank are correct because each stored size is the number of keys in its subtree, as for an order-statistic tree. ■\blacksquare

Back to Weight-balanced tree