Algorithm Search⌘K

Proofs: Splaying

These are the human proofs of Splaying, at full length.

The Access Lemma, an amortized bound of 3(r(t) - r(x)) + 1 for splaying full

This proof shows: amortized-time.

Give each node ii a weight w(i)>0w(i) > 0, which does not change during the splay. For a node xx, let s(x)s(x) be the total weight of the subtree rooted at xx, and r(x)=lg⁡s(x)r(x) = \lg s(x). The potential of the tree is Φ=∑xr(x)\Phi = \sum_x r(x). The cost of a splay is the number of rotations, or 1 if there are none. For a step, write rr and ss for the values before and r′r' and s′s' for the values after.

Fact. For a,b>0a, b > 0, lg⁡a+lg⁡b≤2lg⁡(a+b)−2\lg a + \lg b \le 2 \lg(a + b) - 2.

Proof. ab≤((a+b)/2)2ab \le ((a + b)/2)^2 by the inequality of the arithmetic and geometric means, and taking lg⁡\lg gives lg⁡a+lg⁡b≤2lg⁡(a+b)−2\lg a + \lg b \le 2 \lg(a + b) - 2. □\square

In each step, only xx, its parent yy and (for a double step) its grandparent zz change their subtrees, so only their ranks change. After the step xx roots the subtree that yy (zig) or zz (double step) rooted, so r′(x)=r(y)r'(x) = r(y) or r′(x)=r(z)r'(x) = r(z). Also r(x)≤r(y)≤r(z)r(x) \le r(y) \le r(z) before the step, since each subtree contains the one below it, and r′(x)≥r′(v)r'(x) \ge r'(v) for each vv below xx after it.

Zig. The cost is 1, and ΔΦ=r′(x)+r′(y)−r(x)−r(y)=r′(y)−r(x)≤r′(x)−r(x)\Delta \Phi = r'(x) + r'(y) - r(x) - r(y) = r'(y) - r(x) \le r'(x) - r(x), since r′(x)=r(y)r'(x) = r(y) and r′(y)≤r′(x)r'(y) \le r'(x). So the amortized cost is at most 1+(r′(x)−r(x))≤1+3(r′(x)−r(x))1 + (r'(x) - r(x)) \le 1 + 3(r'(x) - r(x)).

Zig-zig. The cost is 2. With r′(x)=r(z)r'(x) = r(z),

ΔΦ=r′(y)+r′(z)−r(x)−r(y)≤r′(x)+r′(z)−2r(x),\Delta\Phi = r'(y) + r'(z) - r(x) - r(y) \le r'(x) + r'(z) - 2 r(x),

using r′(y)≤r′(x)r'(y) \le r'(x) and r(y)≥r(x)r(y) \ge r(x). Before the step, the subtree of xx holds xx and its two subtrees A,BA, B; after it, zz holds zz, the old sibling subtree of yy and the old other subtree of zz. These two node sets are disjoint and lie inside the new subtree of xx, so s(x)+s′(z)≤s′(x)s(x) + s'(z) \le s'(x). By the fact, r(x)+r′(z)≤2r′(x)−2r(x) + r'(z) \le 2 r'(x) - 2, that is r′(z)≤2r′(x)−r(x)−2r'(z) \le 2 r'(x) - r(x) - 2. So

2+ΔΦ≤2+r′(x)+2r′(x)−r(x)−2−2r(x)=3(r′(x)−r(x)).2 + \Delta\Phi \le 2 + r'(x) + 2r'(x) - r(x) - 2 - 2r(x) = 3(r'(x) - r(x)).

Zig-zag. The cost is 2. With r′(x)=r(z)r'(x) = r(z),

ΔΦ=r′(y)+r′(z)−r(x)−r(y)≤r′(y)+r′(z)−2r(x),\Delta\Phi = r'(y) + r'(z) - r(x) - r(y) \le r'(y) + r'(z) - 2r(x),

using r(y)≥r(x)r(y) \ge r(x). After the step yy and zz are the two children of xx, so s′(y)+s′(z)≤s′(x)s'(y) + s'(z) \le s'(x) and, by the fact, r′(y)+r′(z)≤2r′(x)−2r'(y) + r'(z) \le 2 r'(x) - 2. So

2+ΔΦ≤2r′(x)−2r(x)≤3(r′(x)−r(x)),2 + \Delta\Phi \le 2 r'(x) - 2 r(x) \le 3(r'(x) - r(x)),

since r′(x)≥r(x)r'(x) \ge r(x).

The whole splay. Let the steps be 1,…,m1, \ldots, m, with r0(x)=r(x)r_0(x) = r(x) before the first and rm(x)=r(t)r_m(x) = r(t) after the last, where tt is the root before the splay (xx ends with the subtree of the whole tree, whose weight is s(t)s(t)). Only the last step can be a zig. Adding the bounds of the steps, the sum telescopes:

amortized cost≤∑j=1m3 (rj(x)−rj−1(x))+1=3 (r(t)−r(x))+1.\text{amortized cost} \le \sum_{j=1}^{m} 3\,(r_j(x) - r_{j-1}(x)) + 1 = 3\,(r(t) - r(x)) + 1.

If xx is already the root, the cost is 1 and ΔΦ=0\Delta\Phi = 0, which also meets the bound. ■\blacksquare

Consequences. With w(i)=1w(i) = 1 for all nn nodes, r(t)=lg⁡nr(t) = \lg n and r(x)≥0r(x) \ge 0, so the amortized cost is at most 3lg⁡n+13 \lg n + 1. For a sequence of mm splays, the actual cost is the amortized cost plus Φ0−Φm\Phi_0 - \Phi_m; with unit weights each rank lies between 0 and lg⁡n\lg n, so Φ0−Φm≤nlg⁡n\Phi_0 - \Phi_m \le n \lg n, and the total is at most m(3lg⁡n+1)+nlg⁡nm(3 \lg n + 1) + n \lg n. In general lg⁡w(i)≤r(i)≤lg⁡W\lg w(i) \le r(i) \le \lg W with W=∑iw(i)W = \sum_i w(i), so the net drop of potential is at most ∑ilg⁡(W/w(i))\sum_i \lg(W/w(i)).

Splaying keeps the binary search tree and ends with x at the root full

This proof shows: correctness, termination.

Let TT be a binary search tree with parent pointers in which, for each node vv, v.left.p=vv.left.p = v and v.right.p=vv.right.p = v when the children exist, and the root has no parent. Call this the link invariant.

Lemma 1 (rotation). If xx has a parent yy, ROTATE(x)(x) keeps the link invariant and the in-order sequence, makes yy a child of xx, and gives xx the old parent zz of yy (or no parent). The depth of xx drops by 1, and the subtree that xx now roots holds exactly the nodes of the old subtree of yy.

Proof. Take x=y.leftx = y.left (line L8); the other case is the mirror image. Let α,β\alpha, \beta be the subtrees of xx and γ\gamma the right subtree of yy, so the in-order sequence of the subtree of yy is α,x,β,y,γ\alpha, x, \beta, y, \gamma. After line L8, yy has children β\beta and γ\gamma, and xx has children α\alpha and yy, so the sequence of the new subtree of xx is α,x,β,y,γ\alpha, x, \beta, y, \gamma, the same. The parent of the root of β\beta is set to yy in line L8, the parent of yy to xx and the parent of xx to zz in line L10, and the child pointer of zz that pointed to yy points to xx after line L11. All other links are unchanged. So the link invariant holds, and the in-order sequence of the whole tree is unchanged, since the subtree at that position has the same sequence. The keys of α,x,β,y,γ\alpha, x, \beta, y, \gamma are in increasing order, so the BST property holds at xx and yy; at every other node the set of keys in each subtree is unchanged. □\square

Lemma 2 (one round). Assume $x.p \ne $ NIL at line L1. After the round, the link invariant and the in-order sequence still hold, and the depth of xx has dropped by 1 (line L3) or by 2 (lines L4 and L5).

Proof. Line L3 is one rotation of xx. In line L4 the first rotation, ROTATE(y)(y), moves yy above zz; by Lemma 1 the subtree of yy keeps xx as a child on the same side, so xx is still a child of yy and the second rotation is legal; it moves xx above yy, and xx takes the old place of zz, so its depth drops by 2. In line L5 the first rotation moves xx above yy, and xx takes the place of yy as a child of zz; the second moves xx above zz. Each rotation keeps the invariant and the sequence by Lemma 1. □\square

Theorem. SPLAY(T,x)(T, x) ends after ⌈d/2⌉\lceil d/2 \rceil rounds and dd rotations, where dd is the depth of xx, and then TT is a binary search tree with the same in-order sequence, the link invariant, and root xx.

Proof. By Lemma 2 the depth of xx drops by 2 in each round while it is at least 2, and the last round is a zig exactly when the depth is 1. So the number of rounds is ⌈d/2⌉\lceil d/2 \rceil and the number of rotations is dd. The loop stops when $x.p = $ NIL, that is when xx is the root, and line L6 records it. The invariants hold after each round by Lemma 2. ■\blacksquare

Back to Splaying