Lecture 12 Splay Trees
Total Page:16
File Type:pdf, Size:1020Kb
Lecture 12 Splay Trees A splay tree is a data structure invented by Sleator and Tarjan [94, 100] for maintaining a set of elements drawn from a totally ordered set and allowing memb ership testing, insertions, and deletions among other op erations at an amortized cost of O log n per op eration. The most interesting asp ect of the structure is that, unlike balanced tree schemes such as 2-3 trees or AVL trees, it is not necessary to rebalance the tree explicitly after every op eration|it happ ens automatically. Splay trees are binary trees, but they need not b e balanced. The heightof a splay tree of n elements can b e greater than log n; indeed, it can b e as great as n 1. Thus individual op erations can takeasmuch as linear time. However, as op erations are p erformed on the tree, it tends to rebalance itself, and in the long run the amortized complexityworks out to O log nper op eration. Data is represented at all no des of a splay tree. The data values are distinct and drawn from a totally ordered set. The data items will always be maintained in inorder; that is, for anynodex, the elements o ccupying the left subtree of x are all less than x, and those o ccupying the right subtree of x are all greater than x. Splay trees supp ort the following op erations: memberi; S : determine whether element i is in splay tree S inserti; S : insert i into S if it is not already there deletei; S : delete i from S if it is there 58 Lecture 12 Splay Trees 59 0 0 joinS; S : join S and S into a single splay tree, assuming that x<y 0 for all x 2 S and y 2 S 0 00 spliti; S : split the splay tree S into two new splay trees S and S 0 00 such that x i y for all x 2 S and y 2 S . All these op erations are implemented in terms of a single basic op eration, called a splay: splay i; S : reorganize the splay tree S so that element i is at the ro ot if i 2 S , and otherwise the new ro ot is either maxfk 2 S j k<ig or minfk 2 S j k>ig : All of the op erations mentioned ab ove can be p erformed with a constant number of splays in addition to a constant number of other low-level op er- ations such as p ointer manipulations and comparisons. For example, to do 0 joinS; S , rst call splay +1;S to reorganize S so that its largest element is at the ro ot and all other elements are contained in the left subtree of the 0 ro ot; then make S the right subtree. To do deletei; S , call splay i; S to bring i to the ro ot if it is there; then remove i and call join to merge the left and right subtrees. 12.1 Implementation of Splay The splay op eration can b e implemented in terms of the even more elementary rotate op eration. Given a binary tree S and a no de x with parent y , the op eration rotatex moves x up and y down and changes a few p ointers, according to the following picture: s x y s @ @ @ @ @ @ x s s y A A @ @ rotatex A A - @ @ C A A A @ @ A A A A A A rotatey A A A A A B B C A A A A A A A A Avery simple but imp ortant observation to make at this p oint is that the rotate op eration preserves inorder numb ering. To implement splay x; S , we might rotate x up until it b ecomes the ro ot. However, in order to achieve the desired amortized complexity b ounds, we need to be a little more careful. Dep ending on the relationship of x to its parent and grandparent, we distinguish three di erent cases: 60 Lecture 12 Splay Trees i if x has a parent but no grandparent, we just rotatex; ii if x has a parent y and a grandparent, and if x and y are either b oth left children or b oth rightchildren, we rst rotatey , then rotatex; iii if x has a parent y and a grandparent, and if one of x, y is a left child and the other is a right child, we rst rotatex and then rotatex again. Example 12.1 Apply splay 1;Sto the following tree S : 10 10 s s 9 9 s s 8 8 s s 7 7 s s 6 6 s s case ii case ii - - 5 5 s s 4 4 s s 3 1 s s @ 2 2 s @ s @ 1 3 s @ s 10 s 10 s 9 s 9 s 8 s 8 s 7 s 1 s case ii case ii 6 s @ - - 6 s @ 1 s @ 4 7 @ s s @ 4 @s @ 2 5 @ s s @ 2 5 s @s @ 3 @ s @ 3 s @ 10 1 s s @ 1 10 s @ s @ 8 8 @ s s @ @ case i 6 9 6 9 @ s s s s - @ @ @ 4 7 4 7 s s @ s s @ @ @ 2 5 2 5 s s @ s s @ @ @ 3 3 s @ s @ Lecture 12 Splay Trees 61 Applying splay to no de 2 of the resulting tree yields: 1 s @ 10 2 @ s s @ 8 1 8 @ s s s @ @ 6 9 10 @ - @ s s s @ 4 7 4 9 @ s s s s @ @ 2 5 3 6 @ @ s s s s @ @ 3 5 7 @ @ s s s Note that the tree app ears to b ecome more balanced with each splay. 2 12.2 Analysis We will now show that the time required to p erform m op erations on a set of n elements is O m log n. To do this, we use a credit accounting scheme similar to the one used in our analysis of Fib onacci heaps. Eachnodex of the splay tree has a savings account containing a certain number of credits. When x is created, some numb er of credits are charged to the insert op eration that created x, and these credits are dep osited to x's account. These credits can be used later to pay for restructuring op erations. For x a no de of a splay tree, let S x denote the subtree ro oted at x. Let jS j denote the number of no des in tree S . De ne S = blog j S jc x = S x : We maintain the following credit invariant: No de x always has at least x credits on dep osit. Lemma 12.2 Each operation splay x; S requires no more than 3S x+1 credits to perform the operation and maintain the credit invariant. Proof. Let y b e the parentofx and z b e the parentofy , if it exists. Let 0 and be the values of b efore and after the splay op eration, resp ectively. We consider three cases: 62 Lecture 12 Splay Trees i Node z does not exist. This is the last rotation in the splay;we p erform a single rotatex. We are willing to pay no more than 0 3 x x+1 credits for this rotation. Note that 0 x = y 0 0 y x : In order to maintain the invariant, we need to sp end 0 0 0 x+ y x y = y x 0 x x 0 3 x x credits. We are left with at least one credit left over to pay for the constant number of low-level op erations such as p ointer manipulations and comparisons. ii Node x is the left child of y and y is the left child of z or both x and y are right children. In this case we p erform a rotatey followed by 0 a rotatex. We will show that it costs no more than 3 x x credits to p erform these two rotate op erations and maintain the credit invariant. Thus if a sequence of these are done to move x up the tree as in the example ab ove, we will get a telescoping sum, so that the total amount sp ent will be no more than 3S x + 1 the +1 comes from the last rotation as discussed in case i. In order to maintain the invariant, we need 0 0 0 x+ y + z x y z 20 0 extra credits. Since x=z , we have 0 0 0 x+ y + z x y z 0 0 = y + z x y 0 0 = y x+ z y 0 0 x x+ x x 0 = 2 x x : 0 We can a ord to pay for this and have x x credits left over to pay for the constant number of low-level op erations needed to p erform 0 these two rotations. Unfortunately, it may turn out that x = x, in which case we have nothing left over. We show that in this case the quantity 20 is in fact strictly negative, thus the invariant is maintained Lecture 12 Splay Trees 63 for free and we can even a ord to sp end one of our saved credits to pay for the low-level op erations. All we need to do is to show that the two assumptions 0 x = x 0 0 0 x+ y + z x+y + z 0 lead to a contradiction.