Combining Binary Search Trees
Abstract
We present a general transformation for combining a constant number of binary search tree data structures (BSTs) into a single BST whose running time is within a constant factor of the minimum of any “wellbehaved” bound on the running time of the given BSTs, for any online access sequence. (A BST has a wellbehaved bound with overhead if it spends at most time per access and its bound satisfies a weak sense of closure under subsequences.) In particular, we obtain a BST data structure that is competitive, satisfies the working set bound (and thus satisfies the static finger bound and the static optimality bound), satisfies the dynamic finger bound, satisfies the unified bound with an additive factor, and performs each access in worstcase time.
fullvReferences
1 Introduction
Binary search trees (BSTs) are one of the most fundamental and wellstudied data structures in computer science. Yet, many fundamental questions about their performance remain open. While information theory dictates the worstcase running time of a single access in an node BST to be , which is achieved by many BSTs (e.g., [2]), BSTs are generally not built to execute a single access, and there is a long line of research attempting to minimize the overall running time of executing an online access sequence. This line of work was initiated by Allen and Munro [1], and then by Sleator and Tarjan [14] who invented the splay tree. Central to splay trees and many of the data structures in the subsequent literature is the BST model. The BST model provides a precise model of computation, which is not only essential for comparing different BSTs, but also allows the obtaining of lower bounds on the optimal offline BST.
In the BST model, the elements of a totally ordered set are stored in the nodes of a binary tree and a BST data structure is allowed at unit cost to manipulate the tree by following the parent, leftchild, or rightchild pointers at each node or rotate the node with its parent. We give a formal description of the model in Section 1.1. A common theme in the literature since the invention of splay trees concerns proving various bounds on the running time of splay trees and other BST data structures [14, 6, 7, 4, 11]. Sleator and Tarjan [14] proved a number of upper bounds on the performance of splay trees. The static optimality bound requires that any access sequence is executed within a constant factor of the time it would take to execute it on the best static tree for that sequence. The static finger bound requires that each access is executed in amortized time where is the number of keys between any fixed finger and . The working set bound requires that each access is executed in amortized time where is the number of elements accessed since the last access to . Cole [6] and Cole et al. [7] later proved that splay trees also have the dynamic finger bound which requires that each access is executed in amortized time where is the previous item in the access sequence. Iacono [13] introduced the unified bound, which generalizes and implies both the dynamic finger and working set bounds. Bose et al. [4] presented layered working set trees, and showed how to achieve the unified bound with an additive cost of per access, by combining them with the skipsplay trees of Derryberry and Sleator [11].
A BST data structure satisfies the dynamic optimality bound if it is competitive with respect to the best offline BST data structure. Dynamic optimality implies all other bounds of BSTs. The existence of a dynamically optimal BST data structure is a major open problem. While splay trees were conjectured by Sleator and Tarjan to be dynamically optimal, despite decades of research, there were no online BSTs known to be competitive until Demaine et al. invented Tango trees [8] which are competitive. Later, Wang et al. [16] presented a variant of Tango trees, called multisplay trees, which are also competitive and retain some bounds of splay trees. Bose et al. [3] gave a transformation where given any BST whose amortized running time per access is , they show how to deamortize it to obtain worstcase running time per access while preserving its original bounds.
Results and Implications.
In this paper we present a structural tool to combine bounds of BSTs from a certain general class of BST bounds, which we refer to as wellbehaved bounds. Specifically, our method can be used to produce an online BST data structure which combines wellbehaved bounds of all known BST data structures. In particular, we obtain a BST data structure that is competitive, satisfies the working set bound (and thus satisfies the static finger bound and the static optimality bound), satisfies the dynamic finger bound, satisfies the unified bound with an additive , and performs each access in worstcase time. Moreover, we can add to this list any wellbehaved bound realized by a BST data structure.
Note that requiring the data structures our method produces to be in the BST model precludes the possibility of a trivial solution such as running all data structures in parallel and picking the fastest.
Our result has a number of implications. First, it could be interpreted as a weak optimality result where our method produces a BST data structure which is competitive with respect to a constant number of given BST data structures whose actual running times are wellbehaved. In comparison, a dynamically optimal BST data structure, if one exists, would be competitive with respect to all BST data structures. On the other hand, the existence of our method is a necessary condition for the existence of a dynamically optimal BST. Lastly, techniques introduced in this paper (in particular the simulation of multiple fingers in Section 2) may be of independent interest for augmenting a BST in nontrivial ways, as we do here. Indeed, they are also used in [5].
1.1 Preliminaries
The BST model.
Given a set of elements from a totally ordered universe, where , a BST data structure stores the elements of in a rooted tree, where each node in the tree stores an element of , which we refer to as the key of the node. The node also stores three pointers pointing to its parent, left child, and right child. Any key contained in the left subtree of a node is smaller than the key stored in the node; and any key contained in the right subtree of a node is greater than the key stored in the node. Each node can store data in addition to its key and the pointers.
Although BST data structures usually support insertions, deletions, and searches, in this paper we consider only successful searches, which we call accesses. To implement such searches, a BST data structure has a single pointer which we call the finger, pointed to a node in the BST . The finger initially points to the root of the tree before the first access. Whenever a finger points to a node as a result of an operation we say the node is touched, and denote the node by . An access sequence satisfies for all . A BST data structure executes each access by performing a sequence of unitcost operations on the finger—where the allowed unitcost operations are following the leftchild pointer, following the rightchild pointer, following the parent pointer, and performing a rotation on the finger and its parent—such that the node containing the search key is touched as a result of these operations. Any augmented data stored in a node can be modified when the node is touched during an access. The running time of an access is the number of unitcost operations performed during that access.
An offline BST data structure executes each operation as a function of the entire access sequence. An online BST data structure executes each operation as a function of the prefix of the access sequence ending with the current access. Furthermore, as coined by [3], a realworld BST data structure is one which can be implemented with a constant number of bit registers and bits of augmented data at each node.
The MultifingerBST model.
The MultifingerBST model is identical to the BST model with one difference: in the MultifingerBST model we have access to a constant number of fingers, all initially pointing to the root.
We now formally define what it means for a BST data structure to simulate a MultifingerBST data structure.
Definition 1
A BST data structure simulates a MultifingerBST data structure if there is a correspondence between the th operation performed by and a contiguous subsequence of the operations performed by , for , such that the touched nodes satisfy .
For any access sequence , there exists an offline BST data structure that executes it optimally. We denote the number of unitcost operations performed by this data structure by . An online BST data structure is competitive if it executes all sequences of length in time at most , where is the number of nodes in the tree. An online BST that is competitive is called dynamically optimal.
Because a BST data structure is a MultifingerBST data structure with one finger, the following definitions apply to BST data structures as well.
Definition 2
Given a MultifingerBST data structure and an initial tree , let be an upper bound on the total running time of on any access sequence starting from tree , where denotes an amortized upper bound on the running time of on the th access of access sequence , and denotes the overhead. Define where is a contiguous subsequence of access sequence and is the index of the th access of in . The bound is wellbehaved with overhead if there exists constants and such that the cost of executing any single access is at most , and for any given tree , access sequence , and any contiguous subsequence of ,
1.2 Our Results
Given online BST data structures , where is a constant, our main result is the design of an online BST data structure which takes as input an online access sequence , along with an initial tree ; and executes, for all , access sequence in time where is a wellbehaved bound on the running time of . To simplify the presentation, we let . By combining BSTs two at a time, in a balanced binary tree, we achieve an (constant) overhead.
Theorem 3
Given two online BST data structures and , let and be wellbehaved amortized upper bounds with overhead on the running time of and , respectively, on a contiguous subsequence of any online access sequence from an initial tree . Then there exists an online BST data structure, such that
If and are realworld BST data structures, so is .
Corollary 4
There exists a BST data structure that is competitive, satisfies the working set bound (and thus satisfies the static finger bound and the static optimality bound), satisfies the dynamic finger bound, satisfies the unified bound^{1}^{1}1The Cachesplay tree [10] was claimed to achieve the unified bound. However, this claim has been rescinded by one of the authors at the 5th Bertinoro Workshop on Algorithms and Data Structures. with an additive , all with additive overhead , and performs each access in worstcase time.
Proof
We apply Theorem 3 to combine the bounds of the splay tree, the multisplay tree [16], and the layered working set tree [4]. The multisplay tree is competitive. Observe that is a wellbehaved bound with overhead because any tree can be transformed to any other tree in time [15]. Therefore, competitiveness of multisplay trees is a wellbehaved bound with overhead . On the other hand, the multisplay tree also satisfies the working set bound. The working set bound is a wellbehaved bound with overhead because only the first instance of each item in a subsequence of an access sequence has a different working set number with respect to that subsequence and the log of each such difference is upper bounded by . The working set bound implies the static finger and static optimality bounds with overhead [12]. The splay tree satisfies the the dynamic finger bound [7, 6], which is a wellbehaved bound with overhead because the additive term in the dynamic finger bound is linear and only the first access in a subsequence may have an increase in the amortized bound which is at most . The layered working set tree [4] satisfies the unified bound with an additive . Similar to the working set bound, the unified bound is a wellbehaved bound with overhead because only the first instance of each item in a subsequence of an access sequence has a different unified bound value with respect to that subsequence and each such difference is at most . Therefore, because the term is additive and is dominated by , the unified bound with an additive is a wellbehaved bound with overhead. Lastly, because the multisplay tree performs each access in worstcase time and because is a wellbehaved bound with no overhead, we can apply the transformation of Bose et al. [3] to our BST data structure to satisfy all of our bounds while performing each access in worstcase time.
To achieve these results, we present , which can simulate any MultifingerBST data structure in the BST model in constant amortized time per operation. We will present our data structure as a MultifingerBST data structure in Section 4 and use to transform it into a BST data structure.
Theorem 5
Given any MultifingerBST data structure , where is the th operation performed by , is a BST data structure such that, for any , given operations online, simulates them in total time for some constant that depends on the number of fingers used by . If is a realworld BST data structure, then so is .
1.2.1 Organization and Roadmap.
The rest of the paper is organized as follows. We present a method to transform a MultifingerBST data structure to a BST data structure (Theorem 5) in Section 2. We show how to load and save the state of the tree in time in the MultifingerBST model using multiple fingers in Section 3. We present our BST data structure, the , in Section 4. We analyze and prove our Theorem 3 in Section 5.
2 Simulating Multiple Fingers
In this section, we present , which transforms any given MultifingerBST data structure into a BST data structure .
Structural Terminology.
First we present some structural terminology defined by [9], adapted here to the BST model; refer to Figure 1.
Given any MultifingerBST with a set of fingers , where , let be the be the Steiner tree with terminals , that is, the union of shortest paths in between all pairs of fingers^{2}^{2}2For convenience, we also define the root of the tree to be a finger. . We define prosthetic fingers, denoted by , to be the set of nodes with degree 3 in that are not in . Then, we define the set of pseudofingers, denoted by , to be . Note that . The hand is the compressed Steiner tree obtained from the Steiner tree by contracting every vertex not in (each of degree 2). A tendon is the shortest path in connecting two pseudofingers and (excluding nodes and ), where is an ancestor of and and are adjacent in . We refer to as the top of and as the bottom of . A knuckle is a connected component of after removing all of its pseudofingers and tendons.
To avoid confusion, we use , , and to denote the pointers of a node in the MultifingerBST , and use , , and to denote the pointers of a node in .
Our Approach.
To simulate a MultifingerBST data structure, needs to handle the movement and rotation of multiple fingers. To accomplish this, maintains the hand structure. We discuss how this is done at a high level in Section 2.2. However, a crucial part of maintaining the hand is an efficient implementation of tendons in the BST model where the distance between any two fingers connected by a tendon is at most a constant. We will implement a tendon as a pair of doubleended queues (deques). The next lemma lets us do this by showing that a tendon consists of an increasing and a decreasing subsequence. See Figure (a)a.
Lemma 6
A tendon can be partitioned into two subsets of nodes and such that the levelorder key values of nodes in are increasing and the levelorder key values of nodes in are decreasing, where the maximum key value in is smaller than the minimum key value in .
Proof
Letting to be the set of all nodes in tendon whose leftchild is also in the tendon, and to be the set of all nodes in tendon whose rightchild is also in the tendon yields the statement of the lemma.
2.0.1 DequeBST.
It is straightforward to implement double ended queues (deques) in the BST model. We implement the deques for storing and symmetrically. is a BST data structure storing the set of nodes in and is a BST data structure symmetric to storing the set of nodes in . We denote the roots of and by and respectively. Because they are symmetric structures, we only describe . Any node to be pushed into a is given as the parent of the . Similarly, whenever a node is popped from , it becomes the parent of . supports the following operations in constant amortized time: , , , .
2.1 TendonBST
We now present a BST data structure, , which supports the following operations on a given tendon , where and , , , , .
2.1.1 Implementation.
We implement the operations using and . See Figure (b)b. Nodes , , , and form a path in where node is an ancestor of nodes , , and ; and node is a descedant of nodes , , and . There are four such possible paths and the particular one formed depends on the key values of and , and the relationship between node and its parent in . These invariants imply that the distance between and is 3. When we need to insert a node into the tendon, we perform a constant number of rotations to preserve the invariants and position the node appropriately as the parent of or . We then call the appropriate or operation. Removing a node from the tendon is symmetric. Because deques (, ) can be implemented in constant amortized time per operation, and performs a constant number of unitcost operations in addition to one deque operation, it supports all of its operations in constant amortized time.
2.2 OneFingerBST
At a high level, the data structure maintains the hand , which is of constant size, where each node corresponds to a pseudofinger in . For each pseudofinger the parent pointer, , either points to another pseudofinger or the bottom of a tendon, and the child pointers, , , each point to either another pseudofinger, or the root of a knuckle, or the top of a tendon.
Intuitively, the structure allows us to compress a tendon down to constant depth. Whenever an operation is to be done at a finger, uncompresses the 3 surrounding tendons until the elements at distance up to 3 from the finger are as in the original tree, then performs the operation. then reconstructs the hand structure locally, possibly changing the set of pseudofingers if needed, and recompresses all tendons using . This is all done in amortized time because operations used for decompression and recompression take amortized time and reconfiguring any constant size local subtree into any shape takes time in the worst case.
2.2.1 Adt.
is a BST data structure supporting the following operations on a MultifingerBST with a set of fingers , where :

: Move finger to its parent in , .

: Move finger to its leftchild in , .

: Move finger to its rightchild in , .

: Rotate finger with its parent in , .
2.2.2 Implementation.
We augment each node with a bit field to store the type of the node and the fingers currently on the node.
All the operations take as input the finger they are to be performed on. We first do a brute force search using the augmented bits mentioned above to find the node pointed to by the input finger. Note that all such fingers will be within a distance from the root . We then perform the operation as well as the relevant updates to the tree to reflect the changes in . Specifically, in order to perform the operation, we extract the relevant nodes from the surrounding tendons of the finger by calling the appropriate functions. We perform the operation and update the nodes to reflect the structural changes to the hand structure. Then we insert the tendon nodes back into their corresponding tendons using the appropriate functions.
Theorem 7
Given any MultifingerBST data structure , where is the th operation performed by , is a BST data structure such that, for any , given operations online, simulates them in total time for some constant that depends on the number of fingers used by . If is a realworld BST data structure, then so is .
Proof
Note that before , all the fingers are initialized to the root of the tree and therefore the potentials associated with the deques of the tendons is zero. All finger movements and rotations are performed using operations. Each operation requires one finger movement or rotation and at most a constant number of operations, as well as the time it takes to traverse between fingers. Because the time spent traversing between a constant number of fingers is at most a constant, this implies that simulates operations in time for any .
3 MultifingerBST with Buffers
In our model, each node in the tree is allowed to store bits of augmented data. In this section, we show how to use this collective bits of data to implement a traversable “buffer” data structure. More precisely, we show how to augment any MultifingerBST data structure into a structure called supporting buffer operations.
Definition 8
A buffer is a sequence of cells, where each cell can store bits of data. The buffer can be traversed by a constant number of bufferfingers, each initially on cell , and each movable forwards or backwards one cell at a time.
3.0.1 Adt.
In addition to MultifingerBST operations, supports the following operations on any bufferfinger of a buffer: , , , .
3.0.2 Implementation.
We store the th buffer cell in the th node in the inorder traversal of the tree. and are performed by traversing to the previous or next node respectively in the inorder traversal of the tree.
Lemma 9
Given a tree , traversing it inorder (or symmetrically in reverseinorder) with a finger, interleaved with rotation operations performed by other fingers, takes time.
Proof
The cost of traversing the tree inorder is at most . Each rotation performed in between the inorder traversal operations can increase the total length of any path corresponding to a subsequence of the inorder traversal by at most one. Thus, the cost of traversing inorder, interleaved with rotation operations, is .
3.0.3 Tree state.
We present an augmentation in the MultifingerBST model, which we refer to as , such that given any MultifingerBST data structure , augments with a tree state buffer. The following operations are supported on a tree state buffer: : save the current state of the tree on the tree state buffer, : transform the current tree to the state stored in the tree state buffer. Let the encoding of the tree state be a sequence of operations performed by a linear time algorithm that transforms the tree into a left path. There are numerous folklore linear time implementations of such an algorithm. We can save the state of by calling and recording the performed operations in the tree state buffer. To load a tree state in the tree state buffer, we call then undo all the operations in the tree state buffer. Note that maintains up to cells but we may need to store more data. We can either pack more data into each cell or use multiple copies of . We can also apply to itself to allow for multiple buffers.
Lemma 10
Given any MultifingerBST data structure with fingers, is a MultifingerBST data structure with fingers such that the number of operations performed to execute or is .
Proof
Because runs in linear time, there can be only rotations, and by Lemma 9 the running time of both operations is .
4 ComboMFBST and ComboBST
Given two online BST data structures and , let be any wellbehaved upper bound with overhead on the running time of , and let be any wellbehaved upper bound with overhead on the running time of on a contiguous subsequence of , for any online access sequence and initial tree . Then is defined as follows. It uses a tree state buffer implemented as a . It stores the initial tree state in by calling before executing any accesses. Then, executes any online access sequence in rounds by alternating between emulating and . Specifically, each round consists of operations that execute the access sequence using BST data structure , for , always starting from the initial tree . When the operation limit gets reached, say in the middle of executing access , transforms the tree back to its initial state by calling on ; and toggles the active BST data structure by setting to . The next round reruns access , this time on the opposite BST data structure. By picking a suitably large , we ensure that every round completes at least one access, and thus no access gets executed by the same BST data structure in more than one round.
Lemma 11
Given two BST data structures, and , for any is a MultifingerBST data structure with fingers. Furthermore, if and are realworld BST data structures, then so is .
Proof
has one tree state buffer which has fingers by Lemma 10. Because augments each node with at most bits and uses only a constant number of registers each of size bits, the lemma follows.
Definition 12
Given two online BST data structures and ,
5 Analysis
Theorem 13
Given two online BST data structures and , let and be wellbehaved amortized upper bounds with overhead on the running time of and , respectively, on a contiguous subsequence of for any online access sequence and initial tree Then there exists an online BST data structure, such that
If and are realworld BST data structures, so is .
Proof
Let if , and otherwise. Let be the subsequence of executed by . If the terminates after at most rounds ( of them performed by ), then taking into account the traversal at every round which takes time by Lemma 10 we have
(1) 
Now we need to bound . Each round but the last one runs for steps exactly, and in particular, for all , that is, it might need more steps to complete the last access of . Summing over all , we get by wellbehavedness, the definition of and the fact that s are disjoint subsets of . Therefore, setting yields . Combining with Equation 1, we obtain the desired bound for . By Lemma 11, is a realworld MultifingerBST data structure. Applying (Theorem 5) yields our result.
References
 [1] Brian Allen and J. Ian Munro. Selforganizing binary search trees. Journal of the ACM, 25(4):526–535, 1978.
 [2] Rudolf Bayer. Symmetric binary BTrees: Data structure and maintenance algorithms. Acta Informatica, 1:290–306, 1972.
 [3] Prosenjit Bose, Sébastien Collette, Rolf Fagerberg, and Stefan Langerman. Deamortizing binary search trees. In Proceedings of the 39th International Colloquium Automata, Languages, and Programming (ICALP), pages 121–132, 2012.
 [4] Prosenjit Bose, Karim Douïeb, Vida Dujmovic, and John Howat. Layered workingset trees. Algorithmica, 63(12):476–489, 2012.
 [5] Prosenjit Bose, Karim Douïeb, John Iacono, and Stefan Langerman. The power and limitations of static binary search trees with lazy finger. arXiv:1304.6897, 2013.
 [6] Richard Cole. On the dynamic finger conjecture for splay trees. Part II: The proof. SIAM Journal on Computing, 30(1):44–85, 2000.
 [7] Richard Cole, Bud Mishra, Jeanette P. Schmidt, and Alan Siegel. On the dynamic finger conjecture for splay trees. Part I: Splay sorting log block sequences. SIAM Journal on Computing, 30(1):1–43, 2000.
 [8] Erik D. Demaine, Dion Harmon, John Iacono, and Mihai Pǎtraşcu. Dynamic optimality — almost. SIAM Journal on Computing, 37(1):240–251, 2007.
 [9] Erik D. Demaine, Stefan Langerman, and Eric Price. Confluently persistent tries for efficient version control. Algorithmica, 57(3):462–483, 2010.
 [10] Jonathan Derryberry. Adaptive Binary Search Tree. PhD thesis, CMU, 2009.
 [11] Jonathan Derryberry and Daniel Dominic Sleator. Skipsplay: Toward achieving the unified bound in the BST model. In Proceedings of the 11th International Symposium on Algorithms and Data Structures (WADS), pages 194–205, 2009.
 [12] John Iacono. Improved upper bounds for pairing heaps. In Proceedings of the 7th Scandinavian Workshop on Algorithm Theory (SWAT), pages 32–45, 2000.
 [13] John Iacono. Alternatives to splay trees with worstcase access times. In Proceedings of the 12th Annual ACMSIAM Symposium on Discrete Algorithms (SODA), pages 516–522, 2001.
 [14] Daniel Dominic Sleator and Robert Endre Tarjan. Selfadjusting binary search trees. J. ACM, 32(3):652–686, 1985.
 [15] Daniel Dominic Sleator, Robert Endre Tarjan, and William P. Thurston. Rotation distance, triangulations, and hyperbolic geometry. In Proceedings of the 18th Annual ACM Symposium on Theory of Computing (STOC), pages 122–135, 1986.
 [16] Chengwen Chris Wang, Jonathan Derryberry, and Daniel Dominic Sleator. competitive dynamic binary search trees. In Proceedings of the 17th Annual ACMSIAM Symposium on Discrete Algorithms (SODA), pages 374–383, 2006.