SelfAdjusting Binary Search Trees DANIEL
DOMINIC
SLEATOR
AND
ROBERT ENDRE TARJAN
AT&T Bell Laboratories, Murray Hill, NJ Abstract. The splay tree, a selfadjusting form of binary search tree, is developed and analyzed. The binary search tree is a data structure for representing tables and lists so that accessing, inserting, and deleting items is easy. On an nnode splay tree, all the standard search tree operations have an amortized time bound of O(log n) per operation, where by “amortized time” is meant the time per operation averaged over a worstcase sequence of operations. Thus splay trees are as efficient as balanced trees when total running time is the measure of interest. In addition, for sufficiently long access sequences, splay trees are as efficient, to within a constant factor, as static optimum search trees. The efftciency of splay trees comes not from an explicit structural constraint, as with balanced trees, but from applying a simple restructuring heuristic, called splaying, whenever the tree is accessed.Extensions of splaying give simplified forms of two other data structures: lexicographic or multidimensional search trees and link/ cut trees. Categories and Subject Descriptors: E. 1 [Data]: Data Structurestrees; F.2.2 [Analysis of Algorithms and Problem Complexity]: Nonnumerical Algorithms and Problemssorting and searching General Terms: Algorithms, Theory Additional Key Words and Phrases: Amortized complexity, balanced trees, multidimensional searching, network optimization, selforganizing data structures
1. Introduction In this paper we apply the related concepts of amortized complexity and selfadjustment to binary search trees. We are motivated by the observation that the known kinds of efficient search trees have various drawbacks. Balanced trees, such as heightbalanced trees [2, 221, weightbalanced trees [26], and Btrees [6] and their variants [5, 18, 19,241 have a worstcase time bound of O(log n) per operation on an nnode tree. However, balanced trees are not as efficient as possible if the accesspattern is nonuniform, and they also need extra space for storage of balance information. Optimum search trees [ 16,20,22] guarantee minimum average access time, but only under the assumption of fixed, known accessprobabilities and no correlation among accesses.Their insertion and deletion costs are also very high. Biased search trees [7, 8, 131 combine the fast average access time of optimum trees with the fast updating of balanced trees but have structural constraints even more complicated and harder to maintain than the constraints of balanced trees. Finger search trees [ 11, 14, 19, 23, 241 allow fast accessin the vicinity of one or more “lingers” but require the storage of extra pointers in each node. Authors’ address: AT&T Bell Laboratories, 600 Mountain Avenue, Murray Hill, NJ 07974. Permission to copy without fee all or part of this material is granted provided that the copies are not made or distributed for direct commercial advantage, the ACM copyright notice and the title of the publication and its date appear, and notice is given that copying is by permission of the Association for Computing Machinery. To copy otherwise, or to republish, requires a fee and/or specific permission. 0 1985 ACM 0004541 l/85/07000652 $00.75 Journal
of the Association
for Computing
Machinery,
Vol. 32, No. 3, July
1985, pp. 652686
SelfAdjusting Binary Search Trees
653
These data structures are all designed to reduce the worstcase time per operation. However, in typical applications of search trees, not one but a sequence of operations is performed, and what matters is the total time the sequence takes, not the individual times of the operations. In such applications, a better goal is to reduce the amortized times of the operations, where by “amortized time” we mean the average time of an operation in a worstcase sequence of operations. One way to obtain amortized efficiency is to use a “selfadjusting” data structure. We allow the structure to be in an arbitrary state, but during each operation we apply a simple restructuring rule intended to improve the efficiency of future operations. Examples of restructuring heuristics that give amortized efficiency are the movetofront rule on linear lists [9, 301 and path compression in balanced trees [33, 371. Selfadjusting data structures have several possible advantages over balanced or otherwise explicitly constrained structures: (i) In an amortized sense, ignoring constant factors, they are never much worse than constrained structures, and since they adjust according to usage, they can be much more efficient if the usage pattern is skewed. (ii) They need less space, since no balance or other constraint information is stored. (iii) Their access and update algorithms are conceptually simple and easy to implement. Selfadjusting structures have two potential disadvantages: (i) They require more local adjustments, especially during accesses (lookup operations). (Explicitly constrained structures need adjusting only during updates, not during accesses.) (ii) Individual operations within a sequence can be expensive, which may be a drawback in realtime applications. In this paper we develop and analyze the splay tree, a selfadjusting form of binary search tree. The restructuring heuristic used in splay trees is splaying, which moves a specified node to the root of the tree by performing a sequence of rotations along the (original) path from the node to the root. In an amortized sense and ignoring constant factors, splay trees are as efficient as both dynamically balanced trees and static optimum trees, and they may have even stronger optimality properties. In particular, we conjecture that splay trees are as efficient on any sufficiently long sequence of accessesas any form of dynamically updated binary search tree, even one tailored to the exact accesssequence. The paper contains seven sections. In Section 2 we define splaying and analyze its amortized complexity. In Section 3 we discuss update operations on splay trees. In Section 4 we study the practical efficiency and ease of implementation of splaying and some of its variants. In Section 5 we explore ways of reducing the amount of restructuring needed in splay trees. In Section 6 we use extensions of splaying to simplify two more complicated data structures: lexicographic or multidimensional search trees [15, 251 and link/cut trees [29, 341. In Section 7 we make some final remarks and mention several open problems, including a formal version of our dynamic optimality conjecture. The appendix contains our tree terminology. The work described here is a continuation of our research on amortized complexity and selfadjusting data structures, which has included an amortized analysis
654
D. D. SLEATOR
AND
R. E. TARJAN
of the movetofront list update rule [9, 301 and the development of a selfadjusting form of heap [31]. Some of our results on selfadjusting heaps and search trees appeared in preliminary form in a conference paper [28]. A survey paper by the second author examines a variety of amortized complexity results [35].
2. Splay Trees We introduce splay trees by way of a specific application. Consider the problem of performing a sequence of accessoperations on a set of items selected from a totally ordered universe. Each item may have some associated information. The input to each operation is an item; the output of the operation is an indication of whether the item is in the set, along with the associated information if it is. One way to solve this problem is to represent the set by a binary search tree. This is a binary tree containing the items of the set, one item per node, with the items arranged in symmetric order: If x is a node containing an item i, the left subtree of x contains only items lessthan i and the right subtree only items greater than i. The symmetricorder position of an item is one plus the number of items preceding it in symmetric order in the tree. The “search” in “binary search tree” refers to the ability to accessany item in the tree by searching down from the root, branching left or right at each step according to whether the item to be found is less than or greater than the item in the current node, and stopping when the node containing the item is reached. Such a search takes 8(d) time, where d is the depth of the node containing the accessed item. If accessing items is the only operation required, then there are better solutions than binary search trees, e.g., hashing. However, as we shall seein Section 3, binary search trees also support several useful update operations. Furthermore, we can extend binary search trees to support accessesby symmetricorder position. To do this, we store in each node the number of descendants of the node. Alternatively, we can store in each node the number of nodes in its left subtree and store in a tree header the number of nodes in the entire tree. When referring to a binary search tree formally, as in a computer program, we shall generally denote the tree by a pointer to its root; a pointer to the null node denotes an empty tree. When analyzing operations on binary search trees, we shall use n to denote the number of nodes and m to denote the total number of operations. Suppose we wish to carry out a sequence of accessoperations on a binary search tree. For the total accesstime to be small, frequently accesseditems should be near the root of the tree often. Our goal is to devise a simple way of restructuring the tree after each access that moves the accessed item closer to the root, on the plausible assumption that this item is likely to be accessedagain soon. As an O(l)time restructuring primitive, we can use rotation, which preserves the symmetric order of the tree. (See Figure 1.) Allen and Munro [4] and Bitner [lo] proposed two restructuring heuristics (see Figure 2):
Single rotation. After accessing an item i in a node x, rotate the edge joining x to its parent (unless x is the root). Move to root. After accessing an item i in a node x, rotate the edge joining x to its parent, and repeat this step until x is the root.
SelfAdjusting Binary Search Trees r(Hate right 
Yn X A? A
rotate left 
c 0
655 X
FIG. I. Rotation of the edge joining nodes x and y. Triangles denote subtrees. The tree shown can be a subtree of a larger tree.
Y A
i%h
0
c
e d F
C
a single rotation /
b
A
4
E
0 c
D
move to root 0
c
FIG. 2. Restructuring heuristics. The node accessedis u.
Unfortunately, neither of these heuristics is efficient in an amortized sense: for each, there are arbitrarily long access sequences such that the time per access is O(n) [4]. Allen and Munro did show that the movetoroot heuristic has an asymptotic average accesstime that is within a constant factor of minimum, but only under the assumption that the access probabilities of the various items are fixed and the accessesare independent. We seek heuristics that have much stronger properties. Our restructuring heuristic, called splaying, is similar to movetoroot in that it does rotations bottomup along the access path and moves the accesseditem all the way to the root. But it differs in that it does the rotations in pairs, in an order that depends on the structure of the accesspath. To splay a tree at a node x, we repeat the following splaying step until x is the root of the tree (see Figure 3): Splaying Step Case 1 (zig). If p(x), the parent of x, is the tree root, rotate the edge joining x with p(x). (This case is terminal.)
656
D. D. SLEATOR AND R. E. TARJAN
Y
X
A?
Y
X
C
A
A a
0
0
c
(4 X
z
Y
Y
1’
I
L5rQ A
A
D
X C
2 0
%
0
C
D
C
D
(b)
0
c
A
0
(c) FIG. 3. A splaying step. The node accessedis x. Each case has a symmetric variant (not shown). (a) Zig: terminating single rotation. (b) Zigzig: two single rotations. (c) Zigzag: double rotation.
Case2 (zigzig). If p(x) is not the root and x and p(x) are both left or both right children, rotate the edgejoining p(x) with its grandparentg(x) and then rotate the edgejoining x with p(x). Case3 (zigzag). If p(x) is not the root and x is a left child and p(x) a right child, or viceversa,rotate the edgejoining x with p(x) and then rotate the edgejoining x with the newp(x). Splaying at a node x of depth d takes 8(d) time, that is, time proportional to the time to accessthe item in x. Splaying not only moves x to the root, but roughly halves the depth of every node along the accesspath. (See Figures 4 and 5.) This halving effect makes splaying efficient and is a property not shared by other, simpler heuristics, such as move to root. Producing this effect seemsto require dealing with the zigzig and zigzag casesdifferently. We shall analyze the amortized complexity of splaying by using a potential function [3 1, 351to carry out the amortization. The idea is to assign to each possible configuration of the data structure a real number called its potential and to define the amortized time a of an operation by a = t + G’  a, where t is the actual time of the operation, @ is the potential before the operation, and a’ is the potential after the operation. With this definition, we can estimate the total time of a sequence of m operations by f j=l
tj = jf,
(Uj
+
@jl

3)
= jE,
Uj +
a’0 
@‘m,
SelfAdjusting Binary Search Trees
E
657
F FIG. 4. Splaying at node a.
C
0
D
E
(b) FIG. 5. Extreme casesof splaying. (a) All zigzig steps. (b) All zigzag steps.
where tj and a, are the actual and amortized times of operation j, +O is the initial potential, and +j for j L 1 is the potential after operation j. That is, the total actual time equals the total amortized time plus the net decrease in potential from the initial to the final configuration. If the final potential is no less than the initial potential, then the total amortized time is an upper bound on the total actual time.
658
D. D. SLEATOR
AND
R. E. TARJAN
To define the potential of a splay tree, we assume that each item i has a positive weight w(i), whose value is arbitrary but fixed. We define the size s(x) of a node x in the tree to be the sum of the individual weights of all items in the subtree rooted at x. We define the rank Y(X) of node x to be log s(x).’ Finally, we define the potential of the tree to be the sum of the ranks of all its nodes. As a measure of the running time of a splaying operation, we use the number of rotations done, unless there are no rotations, in which case we charge one for the splaying. The amortized time to splay a tree with root t at a LEMMA 1 (ACCESS LEMMA). node x is at most 3( r(t)  r(x)) + 1 = O(log(s(t)/s(x))). If there are no rotations, the bound is immediate. Thus suppose there PROOF. is at least one rotation. Consider any splaying step. Let s and s’, r and r’ denote the size and rank functions just before and just after the step, respectively. We show that the amortized time for the step is at most 3(r’(x)  r(x)) + 1 in case 1 and at most 3(r’(x)  r(x)) in case 2 or case 3. Let y be the parent of x and z be the parent of y (if it exists) before the step. Case 1. One rotation is done, so the amortized time of the step is 1 + r’(x) + r’(y)  r(x)  r(y) 5 1 + r’(x)  r(x) 5 1 + 3(r’(x)  r(x))
since .only x and y can change rank since r(y) I r’(y) since r’(x) 2 r(x).
Case 2. Two rotations are done, so the amortized time of the step is 2 + r’(x) + r’(y) + r’(z)  r(x)  r(y)  r(z) = 2 + r’(y) + r’(z)  r(x)  r(y) 5 2 + r’(x) + r’(z)  2r(x)
since only x, y, and z can change rank since r’(x) = r(z) since r’(x) 2 r’(y) and r(y) 2 r(x).
We claim that this last sum is at most 3(r’(x)  r(x)), that is, that 2r’(x)  r(x) r’(z) 2 2. The convexity of the log function implies that log x + log y for x, y > 0, x + y I 1 is maximized at value 2 when x = y = i. It follows that r(x) + r’(z) 2r’(x) = log(s(x)/s’(x)) + log(s’(z)/s’(x)) 5 2, since s(x) + s’(z) 5 s’(x). Thus the claim is true, and the amortized time in case 2 is at most 3(r’(x)  r(x)). Case 3. The amortized time of the step is 2 + r’(x) + r’(y) + r’(z)  44  r(y)  r(z) 5 2 + r’(y) + r’(z)  2r(x)
since r’(x) = r(z) and r(x) I r(y).
We claim that this last sum is at most 2(r’(x)  r(x)), that is, that 2r’(x)  r’(y) r’(z) I 2. This follows as in case 2 from the inequality s’(y) + s’(z) 5 s’(x). Thus the amortized time in case 3 is at most 2(r’(x)  r(x)) I 3(r’(x)  r(x)). The lemma follows by summing the amortized time estimates for all the splaying steps, since the sum telescopes to yield an estimate of at most 3(r’(x)  r(x)) + 1 = 3(r(t)  r(x)) + 1, where r and r’ are the rank functions before and after the entire splaying operation, respectively. Cl ’ Throughout
this paper we use binary logarithms.
SelfAdjusting Binary Search Trees
659
The analysis in Lemma 1 shows that the zigzig case is the expensive case of a splaying step. The analysis differs from our original analysis [28] in that the definition of rank uses the continuous instead of the discrete logarithm. This gives us a bound that is tighter by a factor of two. The idea of tightening the analysis in this way was also discovered independently by Huddleston [ 171. The weights of the items are parameters of the analysis, not of the algorithm: Lemma 1 holds for any assignment of positive weights to items. By choosing weights cleverly, we can obtain surprisingly strong results from Lemma 1. We shall give four examples. Consider a sequence of m accesseson an nnode splay tree. In analyzing the running time of such a sequence, it is useful to note that if the weights of all items remain fixed, then the net decrease in potential over the sequence is at most CYZ’=l log(W/w(i)), where W = CEI w(i), since the size of the node containing item i is at most Wand at least w(i). THEOREM 1 (BALANCE THEOREM). The total access time is O((m + n)log
n + m). PROOF. Assign a weight of l/n to each item. Then W = 1, the amortized access time is at most 3 log n + 1 for any item, and the net potential drop over the sequence is at most n log n. The theorem follows. Cl For any item i, let q(i) be the accessfrequency of item i, that is, the total number of times i is accessed. THEOREM2 (STATICOPTIMALITYTHEOREM). If every item is accessedat least once, then the total accesstime is 0 m + i q(i)log $ . i=l i ( )) PROOF. Assign a weight of q(i)/m to item i. Then W = 1, the amortized access time of item i is O(log(m/q(i))), and the net ootential drop over the sequence is at most C:=, log(m/q(i)). The theorem follows. 0 Assume that the items are numbered from 1 through n in symmetric order. Let the sequence of accesseditems be i,, iz, . . . ) lm. ’ THEOREM3 (STATICFINGERTHEOREM). Iff is anyfixed item, the total access timeisO(nfogn+m+~,“=lZOg(~ijfJ + 1)). PROOF. Assign a weight of l/( 1i  f ] + 1)2to item i. Then W 5 2Cr==1 l/k2 = O(l), the amortized time of the jth access is O(log( ] ij  f I + l)), and the net potential drop over the sequence is O(n log n), since the weight of any item is at least l/n*. The theorem follows. 0 We can obtain another interesting result by changing the item weights as the accessestake place. Number the accessesfrom 1 to m in the order they occur. For any accessj, let t(j) be the number of different items accessedbefore accessj since the last accessof item ij, or since the beginning of the sequence ifj is the first of item ij. (Note that t(j) + i is the position of item ij in a linear list maintained by the movetofront heuristic [30] and initialized in order of first access.) THEOREM4 (WORKING SET THEOREM). The total access time is O(n log n + m + Clm,,log(t(j) + 1)).
660
D. D. SLEATORAND R. E. TARJAN
PROOF. Assign the weights 1, l/4, l/9, . . . , l/n* to the items in order by first access.(The item accessedearliest gets the largest weight; any items never accessed get the smallest weights.) As each access occurs, redefine the weights as follows. Suppose the weight of item ij during accessj is l/k2. After accessj, assign weight I to ij, and for each item i having a weight of 1/(k’)2 with k’ < k, assign weight l/(k’ + l)* to i. This reassignment permutes the weights 1, l/4, l/9, . . . , l/n’ among the items. Furthermore, it guarantees that, during accessj, the weight of item ij will be l/(Q) + 1)2.We have W= C&i l/k2 = O(l), so the amortized time of accessj is O(log(t(j) + 1)). The weight reassignment after an access increases the weight of the item in the root (because splaying at the accesseditem moves it to the root) and only decreasesthe weights of other items in the tree. The size of the root is unchanged, but the sizes of other nodes can decrease. Thus the weight reassignment can only decrease the potential, and the amortized time for weight reassignment is either zero or negative. The net potential drop over the sequence is O(n log n). The theorem follows. Cl Let us interpret the meaning of these theorems. The balance theorem states that on a sufficiently long sequence of accessesa splay tree is as efficient as any form of uniformly balanced tree. The static optimality theorem implies that a splay tree is as efficient as any fixed search tree, including the optimum tree for the given access sequence, since by a standard theorem of information theory [l] the total access time for any fixed tree is d(m + J$f’=lq(i)log(m/q(i))). The static finger theorem states that splay trees support accessesin the vicinity of a fixed finger with the same efficiency as finger search trees. The working set theorem states that the time to access an item can be estimated as the logarithm of one plus the number of different items accessedsince the given item was last accessed.That is, the most recently accesseditems, which can be thought of as forming the “working set,” are the easiest to access.All these results are to within a constant factor. Splay trees have all these behaviors automatically; the restructuring heuristic is blind to the properties of the access sequence and to the global structure of the tree. Indeed, splay trees have all these behaviors simultaneously; at the cost of a constant factor we can combine all four theorems into one. THEOREM5 (UNIFIED THEOREM). The total time of a sequenceof m accesses
on an nnode splay tree is 0 n log II + m + $ log min Gy j=l ( { J wheref is any fixed item.
1i.ifl
+ 1, t(j) + 1 , 1)
PROOF. Assign to each item a weight equal to the sum of the weights assigned to it in Theorems 24 and combine the proofs of these theorems. 0
Remark. Since ] ij f ] < n, Theorem 5 implies Theorem 1 as well as Theorems 24. If each item is accessedat least once, the additive term n log n in the bound of Theorem 5 can be dropped. 3. Update Operations on Splay Trees Using splaying, we can implement all the standard update operations on binary search trees. We consider the following operations:
access(i,t): If item i is in tree t, return a pointer to its location; otherwise, return a pointer to the null node.
SelfAdjusting Binary Search Trees
661
FIG. 6. Splaying after an unsuccessful accessof item 80.
insert( i, t):
Insert item i in tree t, assuming that it is not there already.
delete(i, t):
Delete item i from tree t, assuming that it is present.
join( t ,, t2):
Combine trees t, and t2 into a single tree containing all items from both trees and return the resulting tree. This operation assumes that all items in t, are less than all those in t2 and destroys both t, and t2.
spfit( i, t):
Construct and return two trees tl and t2, where tl contains all items in t less than or equal to i, and t2 contains all items in t greater than i. This operation destroys t.
We can carry out these operations on splay trees as follows. To perform access(i, t), we search down from the root oft, looking for i. If the search reaches a node x containing i, we complete the accessby splaying at x and returning a pointer to x. If the search reaches the null node, indicating that i is not in the tree, we complete the accessby splaying at the last nonnull node reached during the search (the node from which the search ran off the bottom of the tree) and returning a pointer to null. If the tree is empty, we omit the splaying operation. (See Figure 6.) Splaying’s effect of moving a designated node to the root considerably simplifies the updating of splay trees. It is convenient to implement insert and delete using join and split. To carry out join(t,, t2), we begin by accessing the largest item, say
662
D. D. SLEATOR AND R. E. TARJAN
(4
at splpy At  [aor a\ ‘I
FIG. 7. Implementation split(i, I).
‘I
‘2
‘2
of join and split: (a) join(t,, t2). (b)
split
i
9 
na

A
(4
?
join
i
access A
an~
(W FIG. 8. Implementation of insertion and deletion using join and split: (a) insert(i, t). (b) delete(i, t).
i, in tl. After the access,the root of tl contains i and thus has a null right child. We complete the join by making t2 the right subtree of this root and returning the resulting tree. To carry out split(i, t), we perform access(i, t) and then return the two trees formed by breaking either the left link or the right link from the new root oft, depending on whether the root contains an item greater than i or not greater than i. (See Figure 7.) In both join and split we must deal specially with the case of an empty input tree (or trees). To carry out insert(i, t), we perform spZit(i, t) and then replace t by a tree consisting of a new root node containing i, whose left and right subtrees are the trees tl and t2 returned by the split. To carry out deZete(i, t), we perform access(i, t) and then replace t by the join of its left and right subtrees. (See Figure 8.) There are alternative implementations of insert and delete that have slightly better amortized time bounds. To carry out insert(i, t), we search for i, then replace the pointer to null reached during the search by a pointer to a new node containing i, and finally splay the tree at the new node. To carry out delete(i, t), we search for the node containing i. Let this node be x and let its parent be y. We replace x as a child of y by the join of the left and right subtrees of x, and then we splay at y. (See Figure 9.)
SelfAdjusting Binary Search Trees
/
663
JOIN CHILDREN OF 30
FIG. 9. Alternative implementations of insertion and deletion. Insertion of 80 followed by deletion of 30.
In analyzing the amortized complexity of the various operations on splay trees, let us assume that we begin with a collection of empty trees and that every item is only in one tree at a time. We define the potential of a collection of trees to be the sum of the potentials of the trees plus the sum of the logarithms of the weights of all items not currently in a tree. Thus the net potential drop over a sequence of operations is at most CiEulog(w(i)/w’(i)), where U is the universe of possible items and w and w’, respectively, are the initial and final weight functions. In particular, if the item weights never change, the net potential change over the sequence is nonnegative. For any item i in a tree t, let i and i+, respectively, denote the item preceding i and the item following i in t (in symmetric order). If i is undefined, let w(i) = co; if i+ is undefined, let w(i+) = 03. LEMMA 2 (UPDATE LEMMA). The amortized times of the splay tree operations have the following upper bounds, where W is the total weight of the items in the
664
D. D. SLEATOR
AND
R. E. TARJAN
tree or trees involved in the operation:
access(i, t):
W 3 log +1 ( w(i) )
1
3 log
join(t,, t2):
split(i, t):
if i is in t;
W +1 min{w(i), w(i+)] ) ( + O(l),
if i is not in t.
where i is the last item in tl.
W 3 log + O(l) ( w(i) )
1
3 log
if i is in t;
W min(w(i), w(i+)) 1 + O(l) (
if i is not in t.
W  w(i) )+log($)+CV). min(w(i), w(i+)]
insert(i, t):
3 log
delete(i, t):
3 log($)
+ 3 log(W;i:;i))
+ O(1).
Increasing the weight of the item in the root of a tree t by an amount 6 takes at most log( 1 +6/W) amortized time, and decreasing the weight of any item takes negative amortized time. PROOF. These bounds follow from Lemma 1 and a straightforward analysis of the potential change caused by each operation. Let s be the size function just before the operation. In the case of an accessor split, the amortized time is at most 3 log(s(t)/s(x)) + 1 by Lemma 1, where x is the node at which the tree is splayed. If item i is in the tree, it is in x, and s(x) 2 w(i). If i is not in the tree, either i or i+ is in the subtree rooted at x, and s(x) z min(w(i), w(i+)). This gives the bound on accessand split. The bound on join is immediate from the bound on access: the splaying time is at most 3 log(s(t,)/w(i)) + 1, and the increase in potential caused by linking tl and t2 is
10p(i”“s;,~“‘)
5 3 log($).
(We have W = s(tJ + s(t2).)The bound on insert also follows from the bound on access;the increase in potential caused by adding a new root node containing i is log(s(t)$;i))
= log&).
The bound on delete is immediate from the bounds on accessandjoin.
Cl
SelfAdjusting Binary Search Trees
665
Remark. The alternative implementations of insertion and deletion have the following upper bounds on their amortized times: insert(i, t):
2 log
delete(i, t):
3 log
ww(i)
min( Ni),
)+log(+$+MI). w(i+>)
W  w(i) + O(1). mini w(i), w(i))
These bounds follow from a modification of the proof of Lemma 1. For the case of equalweight items, the alternative forms of insertion and deletion each take at most 3 log n + O(1) amortized time on a nnode tree. This bound was obtained independently by Huddleston [ 171 for the same insertion algorithm and a slightly different deletion algorithm. The bounds in Lemma 2 are comparable to the bounds for the same operations on biased trees [7, 8, 131, but the biased tree algorithms depend explicitly on the weights. By using Lemma 2, we can extend the theorems of Section 2 in various ways to include update operations. (An exception is that we do not see how to include deletion in a theorem analogous to Theorem 3.) We give here only the simplest example of such an extension. THEOREM 6 (BALANCE THEOREM WITH UPDATES). A sequenceof m arbitrary operations on a collection of initially empty splay trees takes O(m + J$Z, log nj) time, where nj is the number of items in the tree or trees involved in operationj. PROOF.
Assign to each item a fixed weight of one and apply Lemma 2. Cl
We can use splay trees as we would use any other binary search tree; for example, we can use them to implement various abstract data structures consisting of sorted setsor lists subject to certain operations. Such structures include dictionaries, which are sorted sets with access, insertion, and deletion, and concatenatable queues, which are lists with access by position, insertion, deletion, concatenation, and splitting [3, 221. We investigate two further applications of splaying in Section 6.
4. Implementations of Splaying and Its Variants In this section we study the implementation of splaying and some of its variants. Our aim is to develop a version that is easy to program and efficient in practice. As a programming notation, we shall use a version of Dijkstra’s guarded command language [ 121, augmented by the addition of procedures and “initializing guards” (G. Nelson, private communication). We restrict our attention to successful accesses,that is, accessesof items known to be in the tree. Splaying, as we have defined it, occurs during a second, bottomup pass over an accesspath. Such a pass requires the ability to get from any node on the access path to its parent. To make this possible, we can save the access path as it is traversed (either by storing it in an auxiliary stack or by using “pointer reversal” to encode it in the tree structure), or we can maintain parent pointers for every node in the tree. If space is expensive, we can obtain the effect of having parent pointers without using extra space, by storing in each node a pointer to its leftmost child and a pointer to its right sibling, or to its parent if it has no right sibling. (See Figure 10.) This takes only two pointers per node, the same as the standard leftchildrightchild representation, and allows accessing the left child, right child, or
666
D. D. SLEATOR AND R. E. TARJAN
FIG. 10. Leftmostchildrightsibling ure 6.
representation of the original tree in Fig
parent of any node in at most two steps. The drawback of this representation is loss of time efficiency. We shall assume that parent pointers are stored explicitly; however, our programs can easily be modified to use other ways of retrieving the accesspath. The following procedure is a straightforward implementation of splaying. To avoid an explicit test for p(x) = null, we assume that &null) = l&null) = right(nul1) = null. procedure splay(x); (p(nul1) = fej(nul1) = righZ(nul1) = null] do x = lej( p(x)) + if g(x) = null + rotate right (p(x)) 1p(x) = leji( g(x)) + rotate rig&( g(x)); rotate rig&( p(x)) 1p(x) = right( g(x)) + rotate right(p(x)); rotate left(p(x)) fi I x = right(p(x)) + if g(x) = null + rotate leff( p(x)) I p(x) = rig&( g(x)) + rotate lej( g(x)); rotate left(p(x)) + rotate le#(p(x)); rotate right(p(x)) i ~(4 = kftk(x)) od (p(x) = null) end splay;
The grandparent function g is defined as follows: function g(x); CT= P(PW
end g;
The procedure rotate left(y) rotates the edge joining y and its right child. The procedure rotate right(y), whose implementation we omit, is symmetric. Remark. In the following procedure, the initializing guard “x, z: x = right(y) and z = p(y),” which is always true, declares two local variables, x and z, and initializes them to right(y) and p(y), respectively.
SelfAdjusting Binary Search Trees
667
procedure rotate left(y); if x, z: x = right(y) and z = p(y) + if z = null + skip ) left(z) = y + left(z) := x 1 right(z) = y + right(z) := x ii; left(x), right(y) := y, left(x); P(X), P(Y) := z, x; if right(y) = null + skip 1right(y) # null + p(right( y)) := y fi fi end rotate left;
Inspection of the splay program raises the possibility of simplifying it by omitting the second rotation in the zigzag case. The resulting program, suggestedby M. D. McIlroy (private communication), appears below. procedure simple splay(x);
(p(nul1) = l&null)
= right(nul1) = null)
do x = left(p(x)) + if p(x) = left( g(x)) + rotate rig&( g(x)) fly
# .WWN
+ skip
Gate right(p(x)) ( x = right(p(x)) + if p(x) = right( g(x)) + rotf2te left( g(x)) fl P(X) # rk%W)
, skw
date left( p(x))
od(p(x) = null) end simple splay;
An amortized analysis of simple splaying shows that Lemma 1 holds with a constant factor of 3.16+ in place of three. Thus the method, though simpler than the original method, has a slightly higher bound on the number of rotations. The advantage of implementing splaying using rotation as a primitive is that we can encapsulate all the restructuring, including any required updating of auxiliary fields, in the rotation procedures. The disadvantage is that many unnecessary pointer assignments are done. We can achieve greater efficiency by eliminating the superfluous assignments by expanding the rotation procedures inline, simplifying, and keeping extra information in the control state. A bottomup splaying method is appropriate if we have direct accessto the node at which the splaying is to occur. We shall see an example of this in Section 6. However, splaying as used in Sections 2 and 3 only occurs immediately after an access,and it is more efficient to splay on the way down the tree during the access. We present a topdown version of splaying that works in this way. Suppose we wish to accessan item i in a tree. During the accessand concurrent splaying operation, the tree is broken into three parts: a Zefttree, a middle tree, and a right tree. The left and right trees contain all items in the original tree so far known to be less than i and greater than i, respectively. The middle tree consists of the subtree of the original tree rooted at the current node on the accesspath. Initially the current node is the tree root and the left and right trees are empty. To do the splaying, we search down from the root looking for i, two nodes at a time, breaking links along the accesspath and adding each subtree thus detached to the bottom right of the left tree or the bottom left of the right tree, with the proviso
668
D. D. SLEATOR AND R. E. TARJAN
(4
A
B B
C
(b)
(cl FIG. I 1. Topdown splaying step. Symmetric variants of cases are omitted. Initial left tree is L, right tree is R. Labeled nodes are on the accesspath. (a) Zig: Node y contains the accesseditem. (b) Zigzig. (c) Zigzag.
A
B A
B
FIG. 12. Completion of topdown splaying. Node x contains the accesseditem.
that if we take two steps down in the same direction (leftleft or rightright) we do a rotation before breaking a link. More formally, the splaying operation consists of repeatedly applying the appropriate case among those shown in Figure 11 until none applies, then reassembling the three remaining trees as shown in Figure 12. A similar topdown restructuring method, but without the rotations and consequently with poor amortized efficiency, was proposed by Stephenson [32]. The following program implements this method. The program uses three local variables, t, f, and r, denoting the current vertex, the last node in symmetric order in the left tree, and the first node in symmetric order in the right tree, respectively. The updating of these variables occurs in the primitive procedures. These are rotate
[email protected] and rotate right, which rotate the edge joining t and its right or left child, respectively; link left and link right, which break the link between t and its left or right child and attach the resulting tree to the left or right tree, respectively; and assemble,which completes the splaying by performing the assembly of Figure 12. The program contains an initializing guard that declares 1 and r and initializes
SelfAdjusting Binary Search Trees
669
them both to null. After the first left link, the right child of null is the root of the left tree; after the first right link, the left child of null is the root of the right tree. procedure topdownsplafii, t); if I, r: I= r = null + lej(nul1) := right(nul1) := null; do i < item(t) + if i = item(left(t))+ link right
1i c item(left(t))+ rotate right; link right $ i > item(leji(t)) + link right; link left
1i > item(t) + if i = item(right(t)) + link left 1 i > item(right(t)) + rotate left; link left fl i < item(right(t)) + link left; link right od(i = item(t));
ti
assemble
end topdownsplay;
Implementations of rotate left, link left, and assemble appear below; rotate right and link right, whose definitions we omit, are symmetric to rotate left and link left, respectively. procedure link left;
t, I, right(l) := right(t), t, t
end link left;
procedure rotate left;
t, right(t), left(right(t)) := right(t), leJ(right(t)), t
end rotate left;
procedure assemble;
left(r), right(l) := right(t), left(t); left(t), right(t) := right(null), left(nul1) end assemble; We can simplify topdown splaying as we did bottomup splaying, by eliminating the second linking operation in the zigzag case. The resulting program is as follows: procedure simple topdownsplay(i, t); if 1, r: I= r = null + left(null) := right(nul1) := null; do i c item(t) + if i < item(left(t)),rotate right 4. i 2 item(left(t))+ skip
l&k right 1i > item(t) + if i > item(right(t)) + rotate left d. i 5 item(right(t)) + skip l&k left
od(i = item(f));
fi
assemble
end simple topdownsplay;
670
D. D. SLEATOR
AND
R. E. TARJAN
Lemma 1 holds for both topdown splaying methods with an unchanged constant factor of three. It is easy to modify the program for either method to carry out the various tree operations discussed in Section 3. For these operations a topdown method is more efficient than a bottomup method, but the choice between the topdown methods is unclear; the simple method is easier to program but probably somewhat less efficient. Experiments are needed to discover the most practical splaying method.
5. Semiadjusting Search Trees A possible drawback of splaying is the large amount of restructuring it does. In this section we study three techniques that reduce the amount of restructuring but preserve at least some of the properties of splay trees. As in Section 4, we shall restrict our attention to successful accesses. Our first idea is to modify the restructuring rule so that it rotates only some of the edgesalong an accesspath, thus moving the accessednode only partway toward the root. Semisplaying, our restructuring heuristic based on this idea, differs from ordinary bottomup splaying only in the zigzig case:after rotating the edgejoining the parent p(x) with the grandparent g(x) of the current node x, we do not rotate the edge joining x with p(x), but instead continue the splaying from p(x) instead of x. (See Figure 13.) The effect of a semisplaying operation is to reduce the depth of every node on the accesspath to at most about half of its previous value. If we measure the cost of a semisplaying operation by the depth of the accessed node, then Lemma 1 holds for semisplaying with a reduced constant factor of two in place of three. Furthermore, only one rotation is performed in the zigzag case, but two steps are taken up the tree. Like splaying, semisplaying has many variants. We describe only one, a topdown version, related to a method suggested by C. Stephenson (private communication). Think of performing a semisplaying operation as described above, bottomup, except that if the accesspath contains an odd number of edges,perform the zig case at the bottom of the path instead of at the top. We can simulate this variant of semisplaying topdown, as follows. As in topdown splaying, we maintain a left tree, a middle tree, and a right tree. In addition we maintain a top tree and a node top in the top tree having a vacant child. The relationship among the trees is that all items in the left tree are less than the accesseditem i and also less than those in the middle tree; all items in the right tree are greater than i and also greater than those in the middle tree; and all items in the left, middle, and right trees fall between the item in top and the item in its predecessor in the top tree if the vacant child of top is its left, or between the item in top and the item in its successor in the top tree if the vacant child of top is its right. Initially the left, right, and top trees are empty and the middle tree is the entire original tree. Let i be the item to be accessed.Each splaying step requires looking down two steps in the middle tree from the root and restructuring the four trees according to whether these steps are to the left or to the right. If i is in the root of the middle tree, we combine the left, middle, and right trees as in the completion of topdown splaying (see Figure 12) and then make the root of the combined tree (which contains i) a child of top, filling its vacancy. This completes the splaying. On the other hand, if i is not in the root of the middle tree, we carry out a zig, zigzig, or zigzag step as appropriate.
SelfAdjusting Binary Search Trees
671 X
CA Y
A
B
C
Y=
A X
A
B
(b)
& B
2
AB
CD
AB
CD
A C
FIG. 13. A semisplaying step. Symmetric variants of casesare omitted. In each case, the starred node is the current node of the splaying operation. (a) Zig. (b) Zigzig. (c) Zigzag.
The zig and zigzag casesare exactly as in topdown splaying; they do not affect the top tree. The zigzig case is as illustrated in Figure 14. Suppose that the access path to i contains the root x of the middle tree, its left child y, and the left child of y, say z. We perform a right rotation on the edgejoining x and y. Then we assemble all four trees as in the terminating case, making node y (now the root of the middle tree) a child of top and making the left and right trees the left and right subtrees of y. Finally, we break the link between z and its new parent, making the subtree rooted at z the new middle tree, the rest of the tree the new top tree, and the old parent of z the new top. The left and right trees are reinitialized to be empty. A little thought will verify that topdown semisplaying indeed transforms the tree in the same way as bottomup semisplaying with the zig step, if necessary, done at the bottom of the accesspath. Lemma 1 holds for topdown semisplaying with a constant factor of two. Whether any version of semisplaying is an improvement over splaying depends on the access sequence. Semisplaying may be better when the access pattern is stable, but splaying adapts much faster to changes in usage. All the tree operations discussed in Section 3 can be implemented using semisplaying, but this requires using the alternative implementations of insertion and deletion. Also, the join and split algorithms become more complicated. The practical efficiency of the various splaying and semisplaying methods remains to be determined. Another way to reduce the amount of restructuring in splay trees is to splay only sometimes. We propose two forms of conditional splaying. For simplicity we restrict our attention to a sequence of successful accesseson a tree containing a fixed set of items. One possibility is to splay only when the accesspath is abnormally
672
D. D. SLEATOR
AND
R. E. TARJAN
TOP
n dQ X
n
Y
L
R C
2
B
A
FIG. 14. Zigzig case of a topdown
semisplaying step. The symmetric variant is omitted. The initial left, right, and top trees are L, R, and T, respectively. Nodes x, y, and z are on the accesspath. Tree A is the new middle tree, the new left and right trees are empty, and the rest of the nodes form the new top tree.
B
Z
C
A A
long. To define what we mean by “abnormally long,” consider any variant of splaying (or semisplaying) such that Lemma 1 holds with a constant factor of c. Suppose every item i has a fixed weight w(i), and let c’ be any constant greater than c. We call an accesspath for item i long if the depth of the node containing i is at least c’log( W/w(i)) + cl/c, and short otherwise. (Recall that W is the total weight of all the items.)
If we splay only when the accesspath is THEOREM 7 (LONG SPLAY THEOREM). long and do no restructuring when the accesspath is short, then the total splaying time is O(Ct,log( W/w(i)), that is, proportional to the amortized time to access each item once. The constantfactor in this estimate is proportional to c’/(c’  c). Thus the total restructuring time is independent of m, the number of accesses. PROOF. Consider a splaying operation on a node of depth d 2 c’log( W/w(i)) + cl/c. Let 4 and $‘, respectively, be the potential before and after the splaying. Since c’/c > 1, the actual time of the splaying is d, and we have by Lemma 1 that d + 4  4 5 c log( W/w(i)) + 1. Thus the splaying reduces the potential of the tree by at least d  c log( W/w(i))  1. This means that if we amortize the potential
SelfAdjusting Binary Search Trees
613
reduction over the entire splaying operation, each unit of time spent causes a potential drop proportional to at least
d  c log( W/w(i))  1 , I _ c log( W/w(i)) + 1 d c’log( W/w(i)) + c’/c cl$ C
This means in particular that such a splaying operation can only decrease the potential. Since any accessalong a short accesspath causes no restructuring and thus no change in potential, the total splaying time over the sequenceis proportional to at most c’/(c’  c) times the overall potential drop. The overall potential drop is at most CY=rlog( W/w(i)), giving the theorem. Cl We have as a simple application of Theorem 7 that if we splay only when the accesspath is of length at least c’log n + c’/c, then the total splaying time is O(n log n). This form of conditional splaying has two drawbacks: it requires two passesover each long accesspath (one passjust to determine whether to splay), and the decision whether to splay requires knowing the item weights, which must be predetermined on the basis of known or assumed properties of the access sequence. Thus the method is antithetical to the philosophy of selfadjustment. Another form of conditional splaying, which does not depend on the item weights, is to splay during the first j  1 accesses,and just before the jth accessto stop splaying completely, using the tree resulting from all the splaying operations as a static search tree. The intuition behind this idea is that if splaying is efficient in an amortized sense, then the average state of the data structure ought to be a good one. Under appropriate assumptions, we can prove this. Suppose that each item i has a fixed accessprobability pi and that all accessesare independent. For any search tree T, we define the averageaccesstime of T to be ELI pidi, where di is the depth of the node containing item i. THEOREM 8 (SNAPSHOT THEOREM). Suppose we begin with any initial tree, carry out a sequenceof m accesses,and randomly stop splaying just before access j, with probability l/m for each possible value of j. Then the search tree resultingfrom the sequenceof splaying operations has an expectedaverageaccesstime of O((n log N/m + C?=I Pi&(
l/Pi)).
PROOF. For any accesssequence (Y and any item i, let 1(Y1 be the length of CI (number of accesses),and let Lyibe the accesssequence consisting of (Yfollowed by an accessof i. Let pa be the probability of (Y,that is, the product of the probabilities of all the accesses.Let t&3) be the total time required to carry out the sequence of accessesp using splaying, assuming that we have first performed the sequence of accessesa! using splaying. (If the subscript (Yis missing, we take (Yto be the empty sequence.) Let T, be the average accesstime of the tree formed by performing the sequence of accesses(Yusing splaying. (If CYis the empty sequence, T, is the average accesstime of the initial tree.) We wish to estimate the quantity ClaI