Splaying Preorders and Postorders
Caleb C. Levy, Robert E. Tarjan

TL;DR
This paper proves that splaying preorder and postorder sequences in binary search trees can be done in linear time, leveraging pattern-avoidance and properties of balanced trees, supporting the dynamic optimality conjecture.
Contribution
It introduces new linear-time bounds for splaying preorder and postorder sequences using pattern-avoidance and balanced tree properties.
Findings
Splaying preorder/postorder sequences in an empty BST takes linear time.
Splaying these sequences from a weight-balanced BST also takes linear time.
Preorders and postorders of balanced trees have few large jumps, aiding efficient splaying.
Abstract
Let be a binary search tree. We prove two results about the behavior of the Splay algorithm (Sleator and Tarjan 1985). Our first result is that inserting keys into an empty binary search tree via splaying in the order of either 's preorder or 's postorder takes linear time. Our proof uses the fact that preorders and postorders are pattern-avoiding: i.e. they contain no subsequences that are order-isomorphic to and , respectively. Pattern-avoidance implies certain constraints on the manner in which items are inserted. We exploit this structure with a simple potential function that counts inserted nodes lying on access paths to uninserted nodes. Our methods can likely be extended to permutations that avoid more general patterns. Second, if is any other binary search tree with the same keys as and is weight-balanced (Nievergelt and Reingold 1973),…
Peer Reviews
No public reviews on file for this paper yet. If you reviewed it on a platform where reviews are public (OpenReview, ICLR, NeurIPS, ICML), you can paste yours below so the community can read it here.
Videos
No videos yet. Explain this paper in a talk, walkthrough, or lecture? Add one.
Splaying Preorders and Postorders111Research at
Princeton University partially supported by an innovation research grant from Princeton and a gift from Microsoft.
Caleb C. Levy222Baskin School of Engineering, UC Santa Cruz; [email protected].
Robert E. Tarjan333Department of Computer Science, Princeton University, and Intertrust Technologies; [email protected].
Abstract
Let be a binary search tree of nodes with root , left subtree , and right subtree . The preorder and postorder of are defined as follows: the preorder and postorder of the empty tree is the empty sequence, and
[TABLE]
where denotes sequence concatenation.444We will refer to any such sequence as a preorder or a postorder. We prove the following results about the behavior of splaying [21] preorders and postorders:
Inserting the nodes of into an empty tree via splaying costs . (Theorem 2.) 2. 2.
Inserting the nodes of into an empty tree via splaying costs . (Theorem 3.) 3. 3.
If has the same keys as and is weight-balanced [18] then splaying either or starting from costs . (Theorem 4.)
For 1 and 2, we use the fact that preorders and postorders are pattern-avoiding: i.e. they contain no subsequences that are order-isomorphic to and , respectively. Pattern-avoidance implies certain constraints on the manner in which items are inserted. We exploit this structure with a simple potential function that counts inserted nodes lying on access paths to uninserted nodes. Our methods can likely be extended to permutations that avoid more general patterns. The proof of 3 uses the fact that preorders and postorders of balanced search trees do not contain many large “jumps” in symmetric order, and exploits this fact using the dynamic finger theorem [6, 5].
Items 2 and 3 are both novel. Item 1 was originally proved by Chaudhuri and Höft [4]; our proof simplifies theirs. These results provide further evidence in favor of the elusive dynamic optimality conjecture [21].
Outline.
Section 1 discusses the mathematical preliminaries, historical background, and context for this investigation, and Section 2 samples some related work. Familiar readers may skip directly to the main results and their proofs, in Sections 3 and 4. Section 3 proves that inserting both preorders and postorders via splaying takes linear time. Section 4 establishes that splaying preorders and postorders of balanced search trees [18] takes linear time, regardless of starting tree. Section 5 provides our thoughts on how to analyze insertion splaying permutations that avoid more general patterns, particularly the class of “-increasing” sequences [3].
1 Preliminaries
Binary Search Trees
A binary tree contains of a finite set of nodes, with one node designated to be the root. All nodes have a left and a right child pointer, each leading to a different node. Either or both children may be missing, and we denote a missing child by null. Every node in , save for the root, has a single parent node of which it is a child. (The root has no parent.) The size of is the number of nodes it contains, and is denoted .
There is a unique path from to every other node in , called the access path for in . If is on the access path for then we say is an ancestor of , and is a descendent of . We refer to the subtree comprising and all of its descendants as the subtree rooted at . Nodes thus have left and right subtrees rooted respectively at their left and right children. (Subtrees are empty for null children.) The depth of the node , denoted , is the number of edges on its access path. Its right-depth is the number of right pointers followed, and its left-depth is the number of left pointers followed.
In a binary search tree, every node has a unique key, and the tree satisfies the symmetric order condition: every node’s key is greater than those in its left subtree and smaller than those in its right subtree. The binary search tree derives its name from how its structure enables finding keys. To find a key initialize the current node to be the root. While the current node is not null and does not contain the given key, replace the current node by its left or right child depending on whether is smaller or larger than the key in the current node, respectively. The search returns the last current node, which contains if is in the tree and otherwise null.
The lowest common ancestor of and in , denoted , is the deepest node shared by the access paths of both and . Since the root is a common ancestor of any pair of nodes in and is finite, exists and is well defined. Furthermore .
To insert a new key into a binary search tree , we first do a search for in . When the search reaches a missing node, we replace this node with a node containing the key . (Inserting into an empty tree makes the root key.)
Rotation
Binary search trees are the canonical data structure for maintaining an ordered set of elements, and are building blocks in countless algorithms. Perhaps the most attractive feature of binary search trees is that the number of comparisons required to find an item in an -node binary search tree is , provided that that the tree is properly arranged, which is good in theory and practice. However, without exercising care when inserting nodes, a binary search tree can easily become unbalanced (for example when inserting in order), leading to search costs as high as . Thus, binary search trees require some form of maintenance and restructuring for good performance.
We will employ a restructuring primitive called rotation. A rotation at left child with parent makes the right child of while preserving symmetric order. A rotation at a right child is symmetric, and rotation at the root is undefined. (See Figure 1). A rotation changes three child pointers in the tree.
Rotations were first employed in “balanced” search trees, which include AVL trees [1], Red-Black trees [10], weight-balanced trees [18], and more recently weak AVL trees [11]. These trees augment nodes with bits that provide rough information about how “balanced” each node’s subtree is. Whenever an item is inserted or deleted, rotations are performed to restore invariants on the balance bits that ensure all search paths have nodes. While balanced searched trees are not the focus of this work, they were progenitors for the main algorithm of interest.
Splay
The Splay algorithm [21] eschews keeping track of balance information, replacing it with an intriguing notion: instead of adjusting the search tree only after insertion and deletion, Splay modifies the tree after every search.
The algorithm begins with a binary search for a key in the tree. Let be the node returned by this search. If is not null then the algorithm repeatedly applies a “splay step” until becomes the root. A splay step applies a certain series of rotations based on the relationship between , its parent, and its grandparent, as follows. If has no grandparent (i.e. ’s parent is the root), then rotate at (this case is always terminal). Otherwise, if is a left child and its parent is a right child, or vice-versa, rotate at twice. Otherwise, rotate at ’s parent, and then rotate at . Sleator and Tarjan [21] assigned the respective names zig, zig-zag and zig-zig to these three cases. The series of splay steps that bring to the root are collectively called to as splaying at , or simply splaying . The three cases are depicted in Figure 2.
The cost of splaying a single item in is defined to be .555We absorb the search cost into the rotations. If is a sequence of requested keys in then the cost of splaying starting from is defined as , where , and for , we form by splaying in . To perform insertion splaying, insert a key into the tree and then splay the newly created node. The cost of an insertion splay is the cost splaying the new node.
While splaying individual items can cost , the total cost of splaying requested items in a tree of size is . Hence, the worst case cost of a splay operation, amortized over all the requests, is the same as any balanced binary search tree. This is perhaps surprising for an algorithm that keeps no record of balance information.
What makes Splay truly remarkable is how it takes advantage of “latent structure” in the request sequence, and provides more than simple “worst-case” guarantees. As just one example, if is the number of different items accessed before access since the last access to item (or since the beginning of the sequence if is the first access to ), then the cost to splay starting from is [21].666Note that amortized cost per splay is a corollary of this. (This is called the “working set” property.) Thus, Splay exploits “temporal locality” in the access pattern.
Splay simultaneously exploits “spatial” locality, as shown by the following theorem (originally conjectured in [21]) that we will use later on:
Theorem 1** (Dynamic Finger [6, 5]).**
Let the rank of in , denoted , be the number of nodes in whose keys are less than or equal the key in . The cost of splaying starting from is , where .
In fact, the properties of Splay inspired the authors of [21] to speculate on a much stronger possibility: that Splay’s cost is always within a constant factor of the “optimal” way of executing the requests. Formally, an execution for comprises the following. Let , and for , we perform some number of rotations starting from to form , followed by a search for . The cost of this execution is . The optimal cost \operatorname{OPT}(X,T)\equiv\min\{\operatorname{cost}(E)\mid\text{E(X,T)}\}. The following conjecture has spawned a great deal of related research (see §2):
Conjecture 1** (Dynamic Optimality [21]).**
.
The conjecture remains open. In fact, there is no sub-exponential time algorithm whatsoever that is known to compute, even to within a constant factor, the cost of an optimum binary search tree execution for an instance. There are several known lower bounds [7, 25], none known to be tight (though some conjectured to be).
Pattern-Avoidance
For simplicity, we restrict subsequent discussion to permutation request sequences (i.e. no key is requested twice). By [3], any algorithm that achieves optimal cost on all permutations can be extended to an algorithm that is optimal for all request sequences.
An auxiliary question to determining if Splay (or any other algorithm) is dynamically optimal is: “what class(es) of permutations have optimum executions with ‘low’ cost?” This issue is not a mere curiosity, as almost every permutation of length has optimal execution cost [14], a bound achieved by any balanced search tree. Thus, in the absence of insertions or deletions, adjusting the tree after every access only gives an advantage on a small subset of “structured” request sequences. In addition, these structured request sequences provide candidate counter-examples to dynamic optimality. In this work, we focus on certain pattern-avoiding permutations: those that do not contain any subsequences of a specified type. More formally:777The following definitions and theorems are taken from [13, Chapter 1.3], almost verbatim.
Two permutations and of the same length are order-isomorphic if their entries have the same relative order, i.e. . For example, is order-isomorphic to . A sequence avoids a sequence (or is called -avoiding) if it has no subsequence that is order-isomorphic with . If is -avoiding then all subsequences of are -avoiding. We use as shorthand for “an (arbitrary) permutation that avoids .” Both preorders and postorders may be characterized as pattern-avoiding permutations:
Lemma 1** (Lemma 1.4 from [13]).**
For any permutation :
- (a)
* for some binary search tree avoids .* 2. (b)
* for some binary search tree avoids .*
Sketch.
For preorders, Kozma builds a bijection between binary search trees and -avoiding sequences, and uses a simple argument by contradiction to show preorders avoid [13]. The proof for postorders is a nearly symmetric variation of this argument. ∎
2 Related Work
The first result about Splay’s behavior on pattern-avoiding request sequences was the sequential access theorem [24]: the cost of splaying the nodes of in order is . This is a special case of a corollary888A priori, the traversal conjecture follows from dynamic optimality conditioned on Splay being optimal with low “additive overhead.” The authors recently proved that this corollary is actually unconditional [15]. of dynamic optimality:
Conjecture 2** (Traversal [21]).**
There exists for which the cost of splaying starting from is at most for all pairs of binary search trees with the same keys.
Theorem 2 and [4] is another special case, when . In §3 we prove a new special case: when is -weight balanced.
Interest in the behavior of binary search tree algorithms on “structured” request sequences was revived by Seth Pettie’s analysis of the performance of Splay-based deque data structures using Davenport-Schinzel sequences [20], and his later reproof of the sequential access theorem via the theory of forbidden submatrices [19].
This analysis was later adapted to and greatly extended for another binary search tree algorithm, colloquially known as “Greedy,” that was first proposed as an off-line algorithm independently by Lucas [16] and Munro [17]. Greedy is widely conjectured to be dynamically optimal, and is known to have many of the same properties of Splay, including the working set [8] and dynamic finger [12] bounds.
Greedy was later recast as an on-line algorithm in a “geometric” view of binary search trees [7]. This geometric view of Greedy is especially amenable to forbidden submatrix analysis. In [3], Chalermsook et. al. show that Greedy has nearly-optimal run-time on a broad class of pattern-avoiding permutations. Moreover, they demonstrate that if Greedy is optimal on a certain class of “non-decomposable” permutations then it is dynamically optimal. Chalermsook et al.’s analysis was later simplified in [9].
3 Insertion Splaying Preorders and Postorders
If is a permutation then the insertion tree for , denoted , is the binary search tree obtained by starting from an empty tree and inserting keys in order of their first appearance in .
Lemma 2**.**
If is a proper ancestor of in then precedes in .
Proof.
Let denote the prefix of containing the elements preceding . By construction, is inserted as a child of some node in . Every proper ancestor of is an ancestor of , thus . Hence, precedes . ∎
Insertion splaying has the same cost as splaying starting from .999This is because the manner in which Splay restructures the access path is independent of nodes outside the path. For the purposes of analysis we will assume that, initially, every node in is marked as untouched. An insertion splay marks the node as touched, and then splays the node. The touched nodes form a connected subtree containing the root, called the touched subtree. The untouched nodes form subtrees each of which contains no touched node. Call an untouched node with a touched parent a sub-root. The subtrees rooted at sub-roots have identical structure in both the splayed tree and . By Lemma 2, the next node to be touched is always a sub-root.
For , form by touching and then splaying in , where starts with all nodes untouched. At any time we define the potential to be the twice the number of touched nodes that are ancestors of sub-roots, and we define to be the potential of . The amortized cost of splaying in is defined as , where denotes the actual cost. By a standard telescoping sum argument, the cost of insertion splaying is [23]. Since , an upper bound on amortized cost provides an upper bound on the actual cost.
Pattern-avoidance provides certain information about both and about which sub-root can be touched next. We exploit this information in the next two sections.
Preorders
There are no restrictions on the possible structure of preorder insertion trees as .101010In fact, this property is shared by any permutation for which every node in appears in before those in its left and right subtrees. However, the manner in which sub-roots are chosen is particularly simple.
Lemma 3**.**
If is a preorder then, for , is the smallest sub-root of , where all nodes begin untouched in and is formed by touching and splaying in .
Proof.
The statement is vacuously true for . We prove for by contradiction, as follows. Suppose has some sub-root that is smaller than . Since and are both sub-roots in , they are both children of respective (though not necessarily distinct) nodes and in . Let . Since and , all of , and are distinct nodes in , and furthermore . By Lemma 2, precedes both and in , and by construction precedes . We thus have is a subsequence of . But is order-isomorphic with , contradicting . ∎
Theorem 2**.**
Insertion splaying keeps each sub-root at left-depth at most and takes amortized time per splay operation.
Proof.
The theorem is trivial for the first insertion splay. The inductive hypothesis is that every sub-root has left depth [math] or . Let be the next sub-root to be splayed, and let and (either or both of which can be missing) be its left and right children. Touching makes and into sub-roots.
Suppose has left depth [math] before it is touched. Converting from untouched to touched (without splaying it) increases the potential by at most and gives the new sub-roots and left depths of and [math], respectively. (In this case they are the only two sub-roots.) Each splay step, except possibly the last, is a zig-zig in which starts as a left child with parent and grandparent . After completing the zig-zig, is no longer an ancestor of any untouched node, which decreases the potential by . The zig-zig also preserves the left depths of and . ( becomes the right child of .) No other sub-roots can increase left-depth, as is the smallest sub-root. If the last splay step is a zig, the potential does not change (although the length of the path to increases by ).
More complicated is the case in which has left depth . Converting from untouched to touched (without splaying it) makes a sub-root of left depth and a sub-root of left depth . Let be the parent of the ancestor of that is a left child. All other sub-roots are in the right subtree of , which is unaffected by splaying . The splay of consists of [math] or more left zig-zigs, followed by a zig-zag (which can either left-right or right-left), followed by zero of more left zig-zigs, followed possibly by a zig. Each zig-zig reduces the potential by and preserves the left depths of all sub-roots. The zig-zag does not increase the potential, reduces the left depth of from to , and that of from to [math], and preserves the left depth of . Now has left depth [math], and the argument above applies to the remaining splay steps.
By Lemma 3, the next node to be splayed will be if present, otherwise if present, otherwise if present. All three of these items have left-depth [math] or , hence an identical form to Figure 3. Thus the hypothesis holds.
To obtain the constant factor, we observe that converting from untouched to touched increases the potential by . Each zig-zig step pays for itself: it requires rotations, paid for by the potential decreasing by at least . The zig-zag requires rotations, and the zig requires rotation. If the cost of a splay is the number of nodes on the splay path, equal to the number of rotations plus , we have an amortized cost of per splay. ∎
Postorders
Postorder insertion trees are more restricted. A binary search tree is a (left-toothed) comb if the access path for always comprises some number of right children followed by some number of left children. The nodes of are partitioned into teeth, where every node in the tooth has right-depth . The shallowest node in a tooth is called the head. The insertion trees of postorders are combs:
Lemma 4**.**
If is a postorder then no left child in has a right child.
Proof.
By contradiction. Let be a left child in with right child , and let . As is ’s right child, . Similarly, as both and are in ’s left subtree, . By Lemma 2, can be an ancestor of only if precedes in , and similarly must precede . Thus, is a subsequence of that is order-isomorphic to . By Lemma 1(b), is not a postorder. ∎
While postorder insertion trees are less varied than for preorders, there may be many postorders with a given insertion tree. This affords some amount of freedom for choosing different sub-roots.
Lemma 5**.**
Let be a postorder with insertion tree sequence . For , is either:
- (a)
The single sub-root greater than (if present), or 2. (b)
The largest sub-root smaller than (if present).
Proof.
The result is vacuous for . If is case (a), we merely note that if is a new maximum then it must be the right child of the largest node in . There can be at most one sub-root in this position. Hence, is unique.
For the sake of contradiction, suppose is not of the form in case (a) or (b), and let be the largest sub-root smaller than . By Lemma 2, the items of each tooth are added in decreasing order. As is not the head of its tooth, its successor must be in , and furthermore precedes both and in . By construction, is a subsequence of . Yet this subsequence is isomorphic to since , contradicting Lemma 1(b). ∎
Theorem 3**.**
Insertion splaying postorders maintains the following invariants:
After each insertion splay, the path to every sub-root comprises left pointers followed by right pointers. (Furthermore, after the first insertion, .) 2. 2.
The left-depth of every sub-root decreases from smallest to largest.111111The first two invariants dictate that the ancestors of sub-roots form a right-toothed comb. 3. 3.
The splay operation takes constant amortized time.
Proof.
The base case is trivial. Lemma 5 dictates that the next splayed sub-root is either greater than all marked items, or is the largest sub-root smaller than the tree root. Let be the next node to be insertion splayed, its left child, and its right child (either or both children may be missing).
Suppose is greater than the current tree root. Marking increases the potential by and makes and new sub-roots. The splay operation brings to the root by a sequence of left zig-zigs followed possibly by a left zig (depending on whether the length of the access path is odd or even). After each one of these zigs or zig-zigs, ’s left-depth remains , and ’s left depth remains [math]. Let be the root prior to the splay operation. If the last splay step is a zig then the last splay operation increases the left depth of and everything in its left subtree by either or . Since the left-depth of was [math] and was the largest sub-root, the inductive hypothesis ensures that all sub-roots had left-depth at least before the splay operation, and therefore at least afterward. Thus, when becomes the root, the left-depths of each sub-root decrease from left to right.
Otherwise, is the largest sub-root less than the root. Marking again increases the potential by at most . By Lemma 4, has no right child (see Figure 4), so we only need to worry about its left child . Let be the last ancestor of that is a left child. Each left zig-zig prior to the splay step involving maintains the left-depth of to be one greater than the left-depth of . The splay step involving will either be a left zig-zig or a left-right zig-zag, depending on the length of the original path connecting to . Regardless, immediately after the splay step involving , the ancestor of that is the left child of is either the left child of or the left child of ’s parent. Since all the sub-roots less than are in the left subtree of , and thus have left-depth greater than the left-depth of , the invariant is restored, and remains true after each right zig-zig or zig that brings to the root.
All that remains is showing constant amortized time. As noted before, marking costs . If is greater than the root then each left zig-zig, except possibly the last, pays for itself, giving amortized cost of . In the other case, all splay steps except for the one involving and the one making the root pay for themselves, giving amortized cost at most . ∎
4 Balanced Trees
Let denote the size of the subtree rooted at . Following [18], we say is weight balanced for if for all , and write .
Theorem 4**.**
For any (fixed) , if and has the same keys as , then the cost of splaying or starting from is .
Proof.
By Theorem 1, it suffices to show that . Let
[TABLE]
Recall that , where and are the left and right subtrees of the root of , respectively. Notice that the rank differences between and the first item in , and between the last item in and the first item in , are at most by definition. Hence,
[TABLE]
Observe that since , and by definition . Hence,
[TABLE]
Akra-Bazzi’s result [2] suffices to show for fixed . The proof for postorders is identical. ∎
Remark 1**.**
In actuality, for some function of . Unfortunately, the computation appears to be messy. We have declined to do the necessary footwork, as we strongly suspect that, regardless, does not tightly bound the cost of splaying these sequences.
Remark 2**.**
This result extends to any binary search tree algorithm that satisfies the dynamic finger bound. Iacono and Langerman proved Greedy also has the dynamic finger property [12]; their analysis does not consider initial trees, however.
5 Remarks
Patterns that avoid are “symmetric” to those that avoid : if then is the preorder of the mirror image of . Similarly, patterns that avoid are symmetric to patterns that avoid . Thus, insertion splaying and takes linear time.
The only other patterns of length three are and its symmetric counterpart . The pattern was explored in [3], where it was shown that Greedy executes -avoiding permutations in linear time starting from an arbitrary tree. In fact, they showed that executing takes time proportional to ; this is linear in for fixed . These permutations are called -increasing because they can be partitioned into disjoint monotonically increasing subsequences [3]. They form the natural generalization of sequential access, which is the (unique) permutation of the tree nodes that avoids .
More general invariants can be derived about insertion tree structure and sub-root insertion order based on pattern-avoidance. As one particularly interesting example:
Theorem 5**.**
If then no node in has left-depth more than , and the next sub-root inserted (without splaying) is always the smallest sub-root with its given left-depth.
The proof is similar to Lemmas 4 and 5. In particular, the insertion trees of -avoiding permutations look like the combs of postorder insertion trees, except the teeth are rightward, instead of leftward paths.
For -increasing sequences, the potential used for Theorems 2 and 3 needs modifications. The main issue is that in both of these cases, the zig-zigs paid for themselves because the nodes knocked off the access path did not have sub-root descendants. This structure no longer holds for -avoiding sequences, since we must splay the nodes of the teeth in increasing order. The proof seems to require a generalization of the sequential access theorem. It is possible that the notion of kernel trees used by Sundar in [22] for a potential-based proof of the sequential access theorem could be useful.
The reference list from the paper itself. Each links out to its DOI / PubMed record.
- 1[1] Georgy Adel’son-Vel’skii and Evgenii Landis “An algorithm for the organization of information” In Sov. Math. Dokl. 3 , 1962, pp. 1259–1262
- 2[2] M. Akra and L. Bazzi “On the Solution of Linear Recurrence Equations” In Computational Optimization and Applications 10.2 , 1998, pp. 195–210
- 3[3] Parinya Chalermsook et al. “Pattern-Avoiding Access in Binary Search Trees” In FOCS , 2015, pp. 410–423
- 4[4] Ranjan Chaudhuri and Hartmut Höft “Splaying a search tree in preorder takes linear time” In ACM SIGACT News 24.2 , 1993, pp. 88–93
- 5[5] Richard Cole “On the Dynamic Finger Conjecture for Splay Trees. Part II: The Proof” In SICOMP 30.1 , 2000, pp. 44–85
- 6[6] Richard Cole, Bud Mishra, Jeanette Schmidt and Alan Siegel “On the Dynamic Finger Conjecture for Splay Trees. Part I: Splay Sorting log n 𝑛 \log n -Block Sequences” In SICOMP 30.1 , 2000, pp. 1–43
- 7[7] Erik Demaine et al. “The Geometry of Binary Search Trees” In SODA , 2009, pp. 496–505
- 8[8] Kyle Fox “Upper Bounds for Maximally Greedy Binary Search Trees” In WADS , 2011, pp. 411–422
