Weighted dynamic finger in binary search trees

10/03/2018 ∙ by John Iacono, et al. ∙ 0

It is shown that the online binary search tree data structure GreedyASS performs asymptotically as well on a sufficiently long sequence of searches as any static binary search tree where each search begins from the previous search (rather than the root). This bound is known to be equivalent to assigning each item i in the search tree a positive weight w_i and bounding the search cost of an item in the search sequence s_1,...,s_m by O(1+ ∑_(s_i-1,s_i) ≤ x ≤(s_i-1,s_i)w_x/(w_s_i,w_s_i-1)) amortized. This result is the strongest finger-type bound to be proven for binary search trees. By setting the weights to be equal, one observes that our bound implies the dynamic finger bound. Compared to the previous proof of the dynamic finger bound for Splay trees, our result is significantly shorter, stronger, simpler, and has reasonable constants.



There are no comments yet.


page 1

page 2

page 3

page 4

This week in AI

Get the week's most popular data science and artificial intelligence research sent straight to your inbox every Saturday.

1 Introduction

In spite of defining one of the most fundamental classes of data structures in Computer Science, the full power of binary search trees (BSTs) is still not fully understood. The discovery of self-adjusting trees such as Splay trees [17]

, which can perform sequences of searches faster by adapting to their distribution, prompted what is probably the most tantalizing question in the field: How well can BSTs adapt, and in particular can a generic algorithm perform as well on a sequence of searches as an algorithm that would be specifically tailored to that sequence?

BST model.

Formally, let be a sequence of searches222Here only searches are considered, not insertions or deletions, so w.l.o.g. it is assumed the BST stores the integers ., where each of . The BST model has been formalized by requiring a search to be implemented with single pointer that starts at the root, and which at unit cost can be moved to one of the children, the parent, or perform a single rotation, with the obvious requirement that to successfully execute the search the pointer must touch the node containing the searched item at some point during the execution of the search. This model encompasses what one normally thinks of as a BST-model algorithm. Let be the fastest any binary search tree can execute the search sequence in the BST model given a choice of the initial tree333The choice of initial tree does not asymptotically impact if since any tree can be converted into any other tree in time in the BST model [18].. Although is well defined, it is unknown whether it is efficiently computable.

Dynamic optimality conjecture.

What has made the field of binary search trees interesting is the dynamic optimality conjecture [17] which posits the existence of online binary search tree algorithms that execute all sufficiently long in time ; a BST algorithm that satifies this conjecture is said to be dynamically optimal. It must be emphasized that there are no distributional assumptions in this conjecture. To satisfy it a BST algorithm must execute all sequences as fast as any algorithm that has foreknowledge of the sequence of searches and infinite processing time to determine which are the best unit-cost BST operations to execute that sequence. So far there are two serious contenders to satisfy this conjecture. Most famously is Splay trees [17]. However, there is an another BST algorithm known as GreedyASS [14], which was originally stated as a simple greedy offline algorithm but has been shown to have an online equivalent [6]. Neither GreedyASS nor Splay trees are known to be dynamically optimal. A BST algorithm was proposed in [12] that is dynamically optimal if any BST algorithm is, however the algorithm is spectacularly impractical and requires a superexponential amount of non-BST-model work to determine what unit-cost BST operations to execute next. A BST-model structure called Tango trees was introduced in [7] and runs in time ; unfortunately for some classes of sequences , this is tight and the general method seems to not allow further improvement. Tango trees also lack the simplicity and elegance of Splay trees and GreedyASS.


Several non-trivial upper bounds for Splay trees and GreedyASS are known. The working-set bound, which is based on temporal locality (items searched recently are fast to search) is known for Splay trees [17] and GreedyASS [10]. It states that the amortized time to execute is proportional to the logarithm of the number of distinct items searched since was last searched. The working set bound has been shown [11] to imply several other bounds such as static finger bound, the static optimality bound, as well as amortized time which were originally presented separately. This was shown to be the best possible bound when the proximity of key values is not taken into effect [6].

The dynamic finger bound is based on spatial locality (a search is fast if its key value is close to the previous search). It states that the amortized time to execute is . This was proven for Splay trees in a two-volume work [5, 4], but remained open (until this work) for GreedyASS.

Neither the working set bound nor the dynamic finger bound imply each other, and both are easily shown to not be tight on some classes of search sequences. There has been a bound introduced, the unified bound, that does imply both dynamic finger and working set; informally it requires a search to be fast if it is close in time to something close in keyspace. However, no BST-model data structure is known to have the unified bound (one highly-engineered structure was claimed [9] but later called into question [16]).

There are also non-trivial lower bounds on the time it takes to execute deterministic search sequences in the BST model. Wilber [20] produced two bounds, neither of which are known to imply each other. Those have been improved to the bounds of [6] which imply the Wilber bounds and are also not known to not be tight.

Lazy finger.

This work presents an upper bound which is an elegant generalization of the dynamic finger bound and the static optimality bound and proves that this bound holds for GreedyASS. A BST algorithm  has the static finger bound if for any fixed tree the time to execute a sufficiently long search sequences on is asymptotically the same as on ; this bound is related to the entropy of the frequencies of searching each item and is known to hold for Splay trees [17] and GreedyASS [10]. A generalization of this bound, which we introduced in [2] and called the lazy finger bound, also assumes a fixed tree but instead of measuring the cost from the root, measures it from the previous search. This is a much stronger bound, and is not related to entropy.

One generalization of the dynamic finger bound comes from weighted random search trees [15]. The idea is to give each item a weight, and to have a search be fast if the weights of the previous and current searches are large compared to the sum of the weights between the two items. Formally, a search structure has the weighted dynamic finger bound for any set of positive weights if the cost to execute search is

amortized. Although random weighted BST have this bound for a specific set of weights, before this work, no search structure, in the BST model or elsewhere, was known to have the weighted dynamic finger bound without being provided the weights. Note that setting the weights to be equal gives the dynamic finger bound. This bound is easily seen to be stronger than the dynamic finger bound. For example, consider the sequence of searches , where we assume is a prefect square. The dynamic finger bound bounds each search as as each search is to a key value from the previous one giving a bound of . The working set bound also bounds this sequence as taking as there are generally distinct items searched between searches of an item. However, by setting the weights of the searched items to and the weights of the other items to the weighted dynamic finger would bound the search cost to be amortized.

The central result of [2] is that for any sequence , if you pick the that minimizes the lazy finger bound you get a bound asymptotically equivalent to the weighted dynamic finger bound. The main result of this paper is to show that these two equivalent bounds hold for GreedyASS. In doing so we obtain a bound for that data structure that is better than the dynamic finger bound, making it a plausible candidate for dynamic optimality. This is significant as the dynamic finger bound has been the main impediment in producing better bounds for search tree algorithms and has stood unimproved for any plausible contender for dynamic optimality until now, a span of 25 years since it first appeared. The proof of the dynamic finger bound for Splay trees spans two volumes [5, 4] and is a tour-de-force of various techniques, observations, and inverse Ackermanns. It is very complicated, lengthy (85 pages), and has enormous constants (the leading constant is 42,000 and the constant on a lower-order log-log-times-inverse-Ackermann term is ). Proving a bound that implies the dynamic finger bound, would require one to try to extend the proof there, or to take an entirely new approach. We have opted for the latter. Our proof is a completely novel approach that yields a relatively straightforward amortization with respect to how the algorithm GreedyASS runs and the reference tree which we are trying to prove lazy-finger competitiveness with. Given the lazy finger bound our results from [2] immediately give the weighted dynamic finger bound. The resulting argument is fairly simple, has low constants, and perhaps most tantalizingly leaves room for possible improvements.

2 Binary search trees, the geometric view, and GreedyASS

In [6] a simple geometric view of BST algorithms was introduced. Given a BST algorithm executing a sequence of searches on a BST storing the integers , the geometric view consists of a set of points on the grid, where a point is in the geometric view iff the node containing was touched in the th search . Consider the following property: Given a set of points on a grid, two points not on the same row or column are said to be arborally satisfied, if the rectangle defined by these two points contains a third point. Two points on the same row or column are always considered arborally satisfied as well. A set of points where every pair of points is arborally satisfied is called an Arborally Satisfied Set (ASS). It was shown that the geometric view of a BST algorithm when run on any search sequence is an ASS, and furthermore, for any ASS there is a BST execution that produces that set. Thus ASS sets completely characterize BSTs, and do so in a way that is simple and in particular exempts one from the difficult process of reasoning directly about rotations.

Clearly, in order to execute the th search the algorithm must touch at time ; in the geometric view this corresponds to including the point in the set. Thus any BST algorithm executes a sequence of searches in the geometric view if and only if (1) it contains the search points and (2) it is an ASS. Let be the size of a minimal-size ASS superset of . If one can find an ASS superset of the search points of size , then one has found an asymptotically optimal way of executing in the BST model. Tango trees [7] find a set of size ; no polynomial-time method of finding an ASS superset of size is known.

It is easy to formulate a greedy algorithm to compute an ASS superset of in the geometric view, which has been called GreedyASS. This method starts with and performs a vertical line sweep, adding points to make the set an ASS. At each step the algorithm maintains the invariant that the point set below the sweep line is an ASS. When the sweep line is at row , any ASS violations are fixed by adding the minimal number of points to row to obtain the ASS property. These violations occur when and a point below row define a rectangle that violates the ASS property; the minimum way to remove these ASS violations is by placing points on the upper corners of the rectangles that is not . See Figure 1 for a worked-out example of a GreedyASS execution in the geometric view.

This algorithm was shown in [6] to be equivalent to the following algorithm in the tree view first proposed by Lucas [14]: search for and then reconfigure the search path (only) to have the next search as high as possible and recurse on any remaining elements of the search path. This algorithm, as stated, is not online, and in fact can be seen as the natural offline greedy algorithm. However, it was shown in [6] that there is an online algorithm with equivalent asymptotic cost.

Figure 1: Illustration of the GreedyASS algorithm. The points with the black squares represent the searches . The purple shaded rectangles represent the unsatisfied rectangles which GreedyASS satisfies by placing a point at the upper non- corner.

3 Main result

3.1 Overview of method and notation

Let be a sequence of searches executed by GreedyASS and let be the sets of points in the geometric view of GreedyASS executing the first operations of ; . The reference tree is an arbitrary leaf-oriented binary search tree, that is, a tree whose leaves are labeled left-to-right and whose internal nodes are unlabeled. Our goal is to show that GreedyASS will execute as fast as the static tree would with a lazy finger, asymptotically. Let444We assume and are fixed and allow further notation (such as ) to be defined without explicitly indicating a dependence on and . be the distance in the tree from to via their LCA, measured in nodes. Thus we wish to show that GreedyASS has a cost of amortized to execute the th search, and we do so by establishing a potential function that is parameterized by .

We emphasize that the running time of GreedyASS is independent of . Our analysis is based on viewing GreedyASS exclusively in the geometric view. Thus any time we mention tree notation it refers to the reference tree and not GreedyASS viewed as a BST algorithm. A point will always refer to an element the geometric view, and a node will always refer to a node of . To avoid confusion, will be used to refer to the vertical time axis in the geometric view, and will refer to the horizontal key axis. In the geometric view, left and right are unambiguous, and we adopt the convention in our figures that up corresponds to increasing time (the future) and down decreasing time (the past).

We refer to the integers as items. We distinguish between the terms accessed and searched. Item is searched at time if . Item is accessed at time if . Node refers to the leaf node containing in the tree. The most recent access of at time , is the highest point in in column . To get the individual coordinates of a point, we use and ; e.g. .

For a node of , let be the subtree of rooted at , and let the interval of , be the range spanned by the labels of the leaves in . The reference forest at time , is obtained by removing all ancestors of from , see Figure 2. The root of item at time , is the root in that contains in its subtree.

Figure 2: The reference tree and forest . Here we illustrate a reference tree that is leaf-oriented over the integers . We could have chosen any such tree, but for simplicity we picked a balanced tree. The reference forest is obtained by noting that the most recent (top) search is to 25, and removing all of the ancestors of the leaf labeled 25 from . We use orange to illustrate those nodes that have been removed from to make . The roots of are colored dark gray.

3.2 Potential

For every time , a nonzero item potential is assigned to each of the items that have been accessed so far, and each root of the reference forest will be assigned a virgin potential denoted as . The potential of the structure at time , will simply be the sum of the item potentials of each item in and the virgin potentials of each root in . We now formally define how these constituent potentials are computed.

At time each item is assigned one of three location classes. We present the formal definitions here but note that they have a simple geometric interpretation which is illustrated in Figure 3. Let be the set of all items accessed at time . Let and be the points in to the left and right of and on the same row as the most recent access of , , if they exist. That is, and

Figure 3:

Item types. The highest point in each column is classified as internal, external, or glancing, indicated on the diagram with a single letter. Graphically, these categories are based on the yellow upper orthogonal convex hull of the points. The corners on the yellow are external, the non-corners on the yellow are glancing and the rest are internal.


An item is -left external if there are no accesses to the left of and thus is undefined. Observe that for -left external there are no points in other than in the quadrant at or above and to at or to the left of ; such an access would violate the ASS property or the definition of left external. The definition of -right external is symmetric and a node is -external if it is -left or -right external.


An item is -left glancing if it is not -external and no points are in the quadrant strictly above and at or to the left of . The definition -right glancing is symmetric and an item is -glancing if it is -left or -right glancing.


An item that is neither -external nor -glancing is -internal.

For the purpose of the analysis, at each time we will assign a color to each item and the point . We refer to the points in that are the of some as active at time , they are the ones which are colored. Each of these colors denotes a different potential function. First we describe the four potential functions, and then describe how the color of each item, and therefore its potential, is determined. For -external elements , let and be the -external items closest to the left and right of , if they exist (among the sorted order of all -external elements.) These values are well defined for all -external except the first and last one, respectively.


The red potential of at time is . This potential is only well-defined when and exist, and thus is only well-defined when is -internal or -glancing. See Figure 6.


The green potential of an -external element at time is . When is the first or the last -external element, we set . See Figure 5.


Let be the root in the reference forest that contains . The blue potential of at time is . See Figure 4.


The purple potential of at time is the highest the blue potential has been since its most recent access, .

Figure 4: The computation of the blue potential of item 12 is illustrated. This is done by simply following the leaf labeled 12 up to the root of its tree in the forest and counting that five nodes are encountered and adding one to get six. Blue numbers above external and glancing items indicate their blue potential. The purple numbers above glancing items indicate the highest the blue potential has been since that item become glancing.
Figure 5: The computation of the green potential of item 3 is illustrated. The external items to the left and right of 3 are identified, in this case they are 2 and 8. Then the LCA of 2 and 8 is computed and is drawn in green on the figure. Finally, the green potential is one plus the number of nodes from the leaf containing item 3 to the LCA, in this case .
Figure 6: The computation of the red potential of 19 is illustrated. The items and are identified, in this case the are 16 and 20. Then the LCA of 16 and 20 is computed and is drawn in red on the figure. Finally, the red potential is the number of nodes from the leaf containing 19 to the LCA, in this case 6.

The color of an item at time , denoted as , is a function . The color determines the potential of an item: (e.g. if then ). Colors are assigned to elements as follows:


If is -internal: .


If is -glancing: if then otherwise .


If is -external: if then if then , otherwise . If then ; at any time there are one or two nodes for which this case applies, the extreme leftmost and rightmost accesses at time , which are referred to as top corners.

Let , , , be the items accessed at time with the given color, i.e. . Observe that the actual cost of time is the total number of accesses at time , which is .

Virgin potential.

Suppose is a root of , and for some , was not a root of and was a root of all , (i.e., has continuously been a root from time to ). Root is deemed to be an -virgin if there have been no accesses to any items in its interval in times , that is, since it has been a root. An -virgin root has a potential of , while a non--virgin root has a virgin potential of zero. We denote the virgin potential of root at time as . Additionally, it will be useful to define to be the number of distinct roots in that have at least one item in their range accessed at time .


The potential is simply the sum of for all and for all roots in .

For the analysis we find it useful to define an intermediate potential where we have updated the reference tree but not the point set. We denote the intermediate potential by . To compute we set but have . As all other notation derives from and we define primed variants , , , , , , , -external, -internal, -glancing and based on and . As several of these that depend solely on and not , we have: , , , , , -external iff -external, -internal iff -internal, -glancing iff -glancing.

The colored sets , , , and take on a special meaning. We define , that is, the set of elements accessed in , that were of color after the tree was updated (but using the old point set to determine the colors).


The potential method is used whereby the amortized cost of operation is the actual cost of operation , , plus the change in potential . We bound the potential change from to the intermediate potential in §3.4; we call this Phase I. We then bound in §3.5, this is fairly involved and has a number of cases to consider; we call this Phase II. Finally in §3.6 we combine the two potential bounds with the actual cost to obtain the main result.

3.3 Facts

This section contains a number of observations which are used in the main proofs but are generally intuitive and are moved here to improve the flow of the main proofs. All proofs that use left and right can be reversed by symmetry.

Given leaves , , , appearing in that order in a tree , and .

This is obvious.

If is -external, then iff at least one of and is not in . Conversely, iff both and are in . To see this, observe that exactly when is no higher in than .

(Invariance of left internal if not accessed.) If has defined, and then .

Item is defined to be the access to the left of . Since ( is not accessed at time ) then which immediately gives the fact.

3.4 Phase I: From the previous potential to the intermediate potential

In this step we consider the potential changes caused by updating the reference forest. To obtain a bound on this in Lemma 7, we first analyze the potential changes caused by small changes to the reference forest. In turn to bound this we need a lemma bounding the number of items with blue and purple potential; this Lemma 3.4 will find use as well in proof of Lemma 3.6

At any time , there are at most 4 blue and 2 purple items in the range of any root of .

Suppose there are more than two purple glancing items in the range of some root of . Denote three of these items as such that . Observe that:

Since and , by Fact 3.3:
Since :

Thus since , by the definition of the color of a -glancing item, ’s color will be Red, a contradiction.

By an similar argument, we now show there are no more than two -left external items and no more than two -right external items with blue potential.

Suppose there are more than two -left-external blue items in the range of some root of . Denote three of these items as such that . Observe that cannot be a top corner, and:

Since and , by Fact 3.3:
Since :

Thus since by the definition of the color of an -external item, and because is not a top corner, ’s color will be Green, a contradiction.

We will visualize the process of transforming the forest to as a sequence of splits and merges; see Figure 7. A split removes a root from the reference forest and replaces it by the subtrees of its two children (thus adding one to the number of trees in the forest), while a merge is the complementary operation.

Figure 7: Illustration of the splitting and merging steps needed to turn into . Here is 27 and is 19.

Splits and merges in the reference tree have a potential increase of at most each.

Changes are possible in both the virgin and item potentials. With regard to the virgin potentials, new roots are virgins. There is one new root in a merge and two in a split. The potential gain is at most 12 per new virgin.

Note that whether an item is -internal, -external, or -glancing does not depend on and thus is invariant on splits and merges. The only colors that are affected by splits and merges are blue and purple, since their potential depends on the distance to their root, which may change and will only grow in a a merge. By lemma 3.4 there are at most 6 keys in the range of any root which are blue or purple. The increase in potential in each of these nodes is at most 1, as the distance to the root will increase by exactly one in a merge. Glancing items may change color from purple to red, but only if their potential was tied, and their color was therefore designated as purple, after purple increases the item becomes red and there is no potential change. External items can grange from green to blue for the same reasons, and also have no potential gain.

Thus in a merge, there is one new virgin and at most 6 blue/purple nodes that increase item potential by at most one, while in a split there are two new virgins and no increase in potential of blue/purple nodes. In either case, at most 24 units of potential are gained.

(Phase I potential change) The potential gain from the previous potential to the intermediate potential, , is at most .

Recall that the only difference in how the intermediate potential is computed as compared to is that the reference forest is used rather than . Conceptually, we can convert to by performing a series of merges starting at the leaf with up to the LCA of and , and then performing a series of splits from the LCA down to . By Lemma 3.4, performing the splits and merges needed will cause an increase in potential of at most .

3.5 Phase 2: From the intermediate potential to the new potential

In this phase of the analysis, we bound the changes by adding the points in the geometric view representing the items accessed at time . These are geometrically added to the th row in the geometric view and are located in the columns of . However, due to the fact that we are beginning with the intermediate potential we can assume that the reference forest is . Adding the accesses in the th row can change the potential in many ways. Roots that were virgins may have accesses in their range and thus be virgins no more; we bound changes in virgin potential in §3.5.1. Items that are accessed at time will become glancing or top corner external, and generally incur a potential change; these changes are bounded in §3.5.2. Finally, in §3.5.3 we examine changes in potential might that be possible even for items that are not accessed.

3.5.1 Virgin changes

If key , and all are greater than , no item smaller than will be accessed at times . (and symmetrically). Equivalently, if point , then if the rectangle is free of searches, it is free of accesses.

This is due to the way GreedyASS runs. Item is accessed at time iff there is a rectangle from to that contains only in . Suppose the lemma was false, and some item was accessed as the first violation of the lemma, and suppose this violation happens at time . Then consider the rectangle defined by and . This is not empty as it contains the point and thus is not accessed, a contradiction.

If is a root of all the reference forests from time to , , then all searches are to elements not in the interval of and on the same side of the interval of .

Consider the following stronger statement: If is a root of the reference forest , all searches are in the interval of its sibling (which is in but not ). This is because if is a root in , then the parent of must be an ancestor of the current search , but itself is not. This can only occur if the search is in the subtree of ’s sibling.

If is a non-virgin root, then there are no accesses in any tree in to the side of opposite the current search .

A non-virgin root has been accessed, and by Lemma 3.5.1 all subsequent searches are to the same side of the root as the initial search. Thus by Lemma 3.5.1 there can be no accesses to the opposite side.

At most two non-virgin roots have accesses in their range at any time.

If more than two non-virgin roots have accesses in their range, there must be two non-virgin roots with accesses on the same side of the search. This would violate Lemma 3.5.1.

At least units of virgin potential are lost in Phase II.

Out of the roots in that have accesses in their intervals at time , at most two are non-virgins by Lemma 3.5.1. Those have no virgin potential to lose. The other of them were virgins and lose 12 units of potential each. The roots that do not have accesses in their intervals do not have any change of virginity.

3.5.2 Potential changes of accessed items

We break the analysis of the potential changes of items that are accessed into cases depending on their color at the end of Phase I. Note that, by the definition of the coloring, all accessed elements are colored Red, Purple, or Blue. They cannot be Green since the only -external elements in are the two top corners.

(Accessed items that were green after Phase I) For all then

Suppose w.l.o.g. that is -left external. Since , is not a top corner in as those are Blue by definition. Also, , , and are in the same tree in ; otherwise would have by Fact 3.3. The search must be to the left of , , and ; is not in the same tree as these three and must be entirely to the left or to the right, but, if it were to the right, would block from being accessed. Since is to the left of the three external items , , and , these three will all be accessed and no items between them will be accessed. Thus and . So:

If , then and 1 potential is lost. If , then by the color assignment rules, , and and one potential is lost. Finally, we argue that , otherwise would be a top corner in and - external, but this cannot happen since is to the left of , and is to its right.

(Accessed items that were purple after Phase I) For all , .

Using the definition of Purple:
Since :
Since is accessed; :
By the definition of Purple:

If , then . If , then by definition of the coloring, . Finally, if , then by the fourth line of this proof.

(Accessed items that were blue after Phase I) For all , .

Since :
Since is accessed and :
By the definition of Purple:

If , then . If , then by definition of the coloring, . Finally, if , then by the second line of this proof.

(Accessed items that were red after Phase I) For all , if and otherwise.

Assume w.l.o.g. that .

By the definition of :
Observe that is to the right of ; otherwise would not have been accessed by GreedyASS as would lie in the rectangle formed by and . Thus, by Fact 3.3 and :
Using Fact 3.3 and :
Observe that since is not in the same tree of as , the node in is above the root in and thus .
Since and thus
By the definition of Blue: