Introduction
A binary search tree (BST) stores the keys 1 , … , n 1,\ldots,n 1 , … , n and serves a sequence of searches X = ( x 1 , … , x m ) X=(x_1,\ldots,x_m) X = ( x 1 , … , x m ) . In the standard model of dynamic BSTs [38 , 40 ] , which we state in the form of [20 ] that is equivalent up to a constant factor, each search starts at the root, walks down to the searched key, and may rearrange the nodes it has visited by rotations, and its cost is the number of nodes it touches. The cost of the optimal BST, OPT ( X ) \operatorname{OPT}(X) OPT ( X ) , is the least total cost of serving X X X when the whole sequence is known in advance. Sleator and Tarjan conjectured that splay trees, the BSTs that move each searched key to the root by rotations, serve every sequence at cost O ( OPT ( X ) ) O(\operatorname{OPT}(X)) O ( OPT ( X )) [38 ] . This dynamic optimality conjecture is still open, and much of the work around it asks which regularities of a search sequence an optimal tree can exploit. Each such regularity is expressed by a formula in X X X that bounds OPT ( X ) \operatorname{OPT}(X) OPT ( X ) from above.
Two regularities are classical. The working-set bound charges a search about the logarithm of the number of distinct keys searched since the previous search for the same key (a first search for a key is charged about log 2 n \log_2 n log 2 n ). The dynamic-finger bound charges a search log 2 ( ∣ x t − x t − 1 ∣ + 2 ) \log_2(|x_t-x_{t-1}|+2) log 2 ( ∣ x t − x t − 1 ∣ + 2 ) . Splay trees satisfy both, the second by a long proof of Cole [17 ] , which builds on a special case proved by Cole, Mishra, Schmidt and Siegel [16 ] , so the optimum satisfies both. Iacono [29 ] proposed a single bound that is at most both of them up to an additive O ( m ) O(m) O ( m ) .
To state it, write ρ ( u , t ) \rho(u,t) ρ ( u , t ) , for times u < t u<t u < t , for the number of distinct keys among x u + 1 , … , x t − 1 x_{u+1},\ldots,x_{t-1} x u + 1 , … , x t − 1 . The unified bound of X X X is
UB ( X ) : = log 2 ( n + 2 ) + ∑ t = 2 m min 1 ≤ u < t log 2 ( ∣ x t − x u ∣ + ρ ( u , t ) + 2 ) . \operatorname{UB}(X) := \log_2(n+2) + \sum_{t=2}^{m}\min_{1\le u<t}\log_2\left(|x_t-x_u|+\rho(u,t)+2\right). UB ( X ) := log 2 ( n + 2 ) + t = 2 ∑ m 1 ≤ u < t min log 2 ( ∣ x t − x u ∣ + ρ ( u , t ) + 2 ) .
Choosing u = t − 1 u=t-1 u = t − 1 recovers the dynamic-finger term, and choosing u u u to be the previous search for the key x t x_t x t recovers the working-set term. Iacono’s definition, in the form of [1 ] , minimizes over keys instead of times, and the two definitions agree up to an additive O ( m ) O(m) O ( m ) (Remark 7.4). The minimum lets each search use whichever earlier search is closest. Consider two scans that move rightwards through two distant parts of the key space, one step at a time, with their searches interleaved. Every key is searched once and consecutive searches are far apart, so the working-set and dynamic-finger bounds are both about m log n m\log n m log n . Yet each search is adjacent to the search two steps earlier, so the unified bound is O ( m ) O(m) O ( m ) .
This paper answers the following question, which underlies the conjecture of Iacono that splay trees satisfy the unified bound [29 ] and is stated as open in [9 ] : is there a BST that satisfies the unified bound, that is, serves every sequence X X X at cost O ( m + n + UB ( X ) ) O(m+n+\operatorname{UB}(X)) O ( m + n + UB ( X )) ? Iacono stated his conjecture for searches in a static set of keys, as here. Bădoiu, Cole, Demaine and Iacono later stated it for searches, insertions and deletions [1 ] ; we return to this form below. A weaker, offline form of the question, whether the optimal BST satisfies the unified bound, was also open [11 , 12 ] . Since every BST costs at least OPT ( X ) \operatorname{OPT}(X) OPT ( X ) , a negative answer to the offline question would rule out the unified bound for every BST.
Iacono [29 ] gave a static comparison-based structure that serves X X X in time O ( m + UB ( X ) ) O(m+\operatorname{UB}(X)) O ( m + UB ( X )) , and Bădoiu and Demaine [3 ] a simpler one that also supports insertions, and deletions at an additional cost of O ( log log n ) O(\log\log n) O ( log log n ) . The journal version [1 ] supports searches, insertions and deletions within the unified bound. These structures store the keys in several trees and are not BSTs. In the BST model all known bounds pay an overhead. Skip-splay trees [23 ] , the layered working-set trees of Bose, Drouïlhet, Dujmović and Howat combined with skip-splay [4 ] , the combination of trees of Demaine, Iacono, Langerman and Özkan [DILLÖ13], and the group-access trees of Charalampopoulos, Gupta, Jiamjitrak, Pareek and Yingchareonthawornchai [9 ] cost O ( UB ( X ) + m log log n ) O(\operatorname{UB}(X) + m \log\log n) O ( UB ( X ) + m log log n ) , with an additional O ( n log n ) O(n \log n) O ( n log n ) in the case of [22 ] . Derryberry’s thesis [19 ] proposed the cache-splay tree and claimed that it achieves the unified bound. Demaine, Iacono, Langerman and Özkan reported that this claim was withdrawn [22 footnote 4 of the arXiv version] , see also [28 Section 1] , and we know of no published account of the gap. Chalermsook, Goswami, Kozma, Mehlhorn and Saranurak [12 ] proved that OPT ( X ) \operatorname{OPT}(X) OPT ( X ) is at most a factor times UB ℓ ( X ) \operatorname{UB}_{\ell}(X) UB ℓ ( X ) for a time-windowed bound UB ℓ \operatorname{UB}_{\ell} UB ℓ , in which only the last ℓ \ell ℓ searches may serve as u u u , with a factor O ( ℓ ! ⋅ ℓ log ℓ ) O(\ell! \cdot\ell\log\ell) O ( ℓ ! ⋅ ℓ log ℓ ) , and [9 ] obtained O ( UB ℓ ( X ) + m log ℓ log log n ) O(\operatorname{UB}_{\ell}(X) + m \log\ell\log\log n) O ( UB ℓ ( X ) + m log ℓ log log n ) in expectation, with a randomized tree and for sufficiently long sequences.
The known BSTs lose in similar ways. Skip-splay trees cut the keys into about log log n \log\log n log log n levels of nested blocks whose sizes grow doubly exponentially, and a search can pass through all of these levels, paying a constant at each, even when it is adjacent to the previous search. The multi-finger approach of [12 ] lets a BST simulate k k k pointers that move in the tree, called fingers, at a cost of O ( 1 + log k ) O(1+\log k) O ( 1 + log k ) per finger move, and its bound for a window of the last ℓ \ell ℓ searches uses up to ℓ ! \ell! ℓ ! fingers, so covering every earlier search would require the number of fingers to grow with the sequence. In both cases the overhead comes from serving all scales, or all fingers, uniformly, although a given search needs only one of them.
Our results. We answer the question positively, with a deterministic online BST. An online BST decides how to serve each search knowing only the searches so far.
Theorem 1.1 (Main theorem; see Theorems 4.4 and 7.3). There is a deterministic online BST algorithm that, starting from a fixed tree on the keys 1 , … , n 1,\ldots,n 1 , … , n , serves every sequence X X X of m m m searches at total cost
O ( m + UB ( X ) ) , O(m+\operatorname{UB}(X)), O ( m + UB ( X )) , and every single search at cost O ( log n ) O(\log n) O ( log n ) .
The bound has no additive term n n n : the initial tree is a fixed balanced tree, and the first search is paid for by the term log 2 ( n + 2 ) \log_2(n+2) log 2 ( n + 2 ) of the unified bound. If the initial tree is prescribed, the first search can rebuild it at cost n n n , which gives O ( m + n + UB ( X ) ) O(m+n+\operatorname{UB}(X)) O ( m + n + UB ( X )) , the form of the question above. The bound on a single search is optimal up to a constant factor, since every BST on n n n keys has a key of depth at least log 2 ( n + 1 ) − 1 \log_2(n+1)-1 log 2 ( n + 1 ) − 1 . Both bounds also hold for the running time on a word RAM, after an initialization of O ( n ) O(n) O ( n ) time (Proposition 9.1 ). Since OPT ( X ) \operatorname{OPT}(X) OPT ( X ) is at most the cost of any execution on X X X , the offline question is answered as well.
Corollary 1.2. For every sequence X X X of m m m searches over the keys 1 , … , n 1,\ldots,n 1 , … , n , OPT ( X ) = O ( m + UB ( X ) ) \operatorname{OPT}(X)=O(m+\operatorname{UB}(X)) OPT ( X ) = O ( m + UB ( X )) .
The difficulty is that a single tree must keep the neighbourhood of every recent search near its root, at every scale at once. Cut the keys into blocks of 2 s 2^s 2 s consecutive keys at every scale s s s , and count a block as searched whenever one of its keys is searched. The locality scale of a search is the least scale s s s such that the block of scale s s s that contains its key was searched before and fewer than about 2 s 2^s 2 s distinct keys were searched since, and in total the unified bound charges the searches about these scales (§ 2.5 ). Say that a block of scale s s s is in use while it is among the 2 s 2^s 2 s blocks of its scale that were searched most recently. At any single moment a tree in which the keys carry weights, and heavier keys are nearer the root, can place the keys of every block of scale s s s that is in use within depth O ( s ) O(s) O ( s ) . But blocks keep coming into use and falling out of use, and moving a block of scale s s s costs about s s s . A search can bring a block into use at every scale below its own, so a tree that tracks every scale pays about the square of what the bound allows (§ 2.2 ).
The way out is that a tree need not track every scale. Our tree tracks blocks only at the scales 1 , 2 , 4 , 8 , … 1, 2, 4, 8, \ldots 1 , 2 , 4 , 8 , … : at such a scale s s s , it keeps the 2 s 2^s 2 s blocks of 2 s 2^s 2 s keys that were searched most recently, each with a weight that puts its keys near the root. The scales grow geometrically, so the blocks that a search brings in cost only about the largest of them, and a search with locality scale s s s still finds a kept block of scale less than 2 s 2s 2 s around its key, namely at the least power of two that is at least s s s (§ 2.3 ). The sizes 2 , 4 , 16 , 256 , … 2, 4, 16, 256, \ldots 2 , 4 , 16 , 256 , … of the blocks at these scales grow doubly exponentially, like those of the levels of skip-splay trees [23 ] , and keeping at each level the blocks that were searched most recently is the rule of the cache-splay tree [19 ] . What is new is the tree that holds the blocks and the analysis. Every kept block has a weight that depends only on its size: a kept block of scale s s s has the weight 4 − s 4^{-s} 4 − s , whatever larger blocks contain it. At most 2 s 2^s 2 s blocks of scale s s s are kept, so all weights together are O ( 1 ) O(1) O ( 1 ) , and a search tree for weighted items can place an item of weight w w w at depth O ( log ( 1 / w ) ) O(\log(1/w)) O ( log ( 1/ w )) . So a search reaches a kept block of scale s s s at depth O ( s ) O(s) O ( s ) . It does not pass through the levels above the block, as a search in a skip-splay tree does. The tree that holds the weighted blocks is a biased search tree of Bent, Sleator and Tarjan [7 ] , a balanced search tree for weighted items in which heavy items are near the root. In it a block of scale s s s is inserted and removed in O ( s ) O(s) O ( s ) time in the worst case, so no step is amortized over the tree. A search replaces at most one block at each scale, the least recently searched block by its own, and only at scales below its locality scale. So a search with locality scale s s s costs O ( s ) O(s) O ( s ) in the worst case, and the only amortized step of the proof is the comparison of the locality scales with the unified bound.
Pattern-avoiding sequences. A search sequence avoids a permutation σ \sigma σ of { 1 , … , k } \{1,\ldots,k\} { 1 , … , k } if no k k k of its searches have distinct keys in the relative order given by σ \sigma σ . It was conjectured that OPT ( X ) = O ( n ) \operatorname{OPT}(X)=O(n) OPT ( X ) = O ( n ) for every permutation X X X of { 1 , … , n } \{1,\ldots,n\} { 1 , … , n } that avoids a fixed pattern σ \sigma σ ; Berendsohn, Kozma and Opler [6 ] attribute the conjecture to [10 ] . Berendsohn, Kozma and Opler proved it [6 Section 3] , with the bound OPT ( X ) = O ( n c σ 2 ) \operatorname{OPT}(X)=O(nc_\sigma^2) OPT ( X ) = O ( n c σ 2 ) . Here c σ c_\sigma c σ is the Füredi–Hajnal limit of σ \sigma σ , the least constant such that every 0-1 matrix with M 1 M_1 M 1 rows, M 2 M_2 M 2 columns and more than c σ max ( M 1 , M 2 ) c_\sigma\max(M_1,M_2) c σ max ( M 1 , M 2 ) ones has k k k ones, in k k k different rows and k k k different columns, that lie relative to one another as the ones of the permutation matrix of σ \sigma σ ; it is finite by the theorem of Marcus and Tardos [33 ] . They also noted a lower bound of Ω ( n log c σ ) \Omega(n\log c_\sigma) Ω ( n log c σ ) for some σ \sigma σ -avoiding X X X , by counting the σ \sigma σ -avoiding permutations of { 1 , … , n } \{1,\ldots,n\} { 1 , … , n } , whose number grows like L n L^n L n for a constant L L L that is polynomially related to c σ c_\sigma c σ [14 , 15 ] , and they called sharpening the dependence on c σ c_\sigma c σ a challenging open question. We close the gap.
Theorem 1.3 (see Proposition 8.2 ). There is a deterministic online BST algorithm on the keys 1 , … , n 1,\ldots,n 1 , … , n that, for every permutation σ \sigma σ of length at least 2, serves every sequence X X X of m m m searches over { 1 , … , n } \{1,\ldots,n\} { 1 , … , n } that avoids σ \sigma σ at cost
O ( ( m + n ) ( 1 + log c σ ) ) . O((m+n)(1+\log c_\sigma)). O (( m + n ) ( 1 + log c σ )) . The algorithm does not depend on σ \sigma σ , and the constant in the O O O -notation does not depend on σ \sigma σ .
For a permutation X X X , where m = n m=n m = n , this gives OPT ( X ) = O ( n ( 1 + log c σ ) ) \operatorname{OPT}(X)=O(n(1+\log c_\sigma)) OPT ( X ) = O ( n ( 1 + log c σ )) , which matches the lower bound of [6 , 14 ] for the worst σ \sigma σ -avoiding permutations. Since c σ = 2 O ( k ) c_\sigma=2^{O(k)} c σ = 2 O ( k ) for patterns of length k k k [24 ] , the bound is also O ( ( m + n ) k ) O((m+n)k) O (( m + n ) k ) . Berendsohn, Kozma and Opler asked whether bounds of this kind hold for online algorithms such as splay trees or Greedy [6 ] , the algorithm of Lucas and Munro [31 , 34 ] in the online form of [20 ] , which is the other leading candidate for dynamic optimality. Before this work, the best bound for any online BST on all σ \sigma σ -avoiding permutations was n ⋅ 2 1 + o ( 1 ) α ( n ) n\cdot2^{1+o(1)\alpha(n)} n ⋅ 2 1 + o ( 1 ) α ( n ) for fixed σ \sigma σ , where α \alpha α is the inverse Ackermann function, for Greedy [18 ] . Theorem 1.3 follows from the bound in terms of scales (Theorem 4.4 ) and the
theorem of Marcus and Tardos, applied at every scale s s s to the grid of blocks of 2 s 2^s 2 s keys and intervals of 2 s 2^s 2 s consecutive searches (§ 8 ). The same count gives U B ( X ) = O ( ( m + n ) ( 1 + log c σ ) ) \mathrm{UB}(X)=O((m+n)(1+\log c_\sigma)) UB ( X ) = O (( m + n ) ( 1 + log c σ )) for every σ \sigma σ -avoiding sequence (Proposition 8.2 ). So the unified bound captures pattern avoidance, which answers, for permutations, the question of [10 ] whether some parameter subsumes both pattern avoidance and the dynamic-finger bound.
Insertions and deletions. The question above concerns searches in a fixed set of keys, and this is the setting of the earlier BSTs as well; skip-splay trees, for instance, do not support insertions and deletions [4 ] . The unified bound, however, also makes sense for a set that changes. Bădoiu, Cole, Demaine and Iacono [1 ] extended it to insertions and deletions, with the distance between two keys counted among the keys that are present. We call this the unified bound with updates . They gave a structure that satisfies it and is not a BST, and they conjectured that splay trees satisfy it for all three operations. We extend our tree to support insertions and deletions (Theorem 10.1 ): it serves every sequence X X X of m m m searches, insertions and deletions, starting from a set of n 0 n_0 n 0 keys, at total cost O ( n 0 + m + U B ( X ) ) O(n_0+m+\mathrm{UB}(X)) O ( n 0 + m + UB ( X )) , and every single operation at a cost that is logarithmic in the current number of keys. Up to the additive term n 0 + m n_0+m n 0 + m , the unified bound with updates therefore bounds the cost of the best BST for every sequence of searches, insertions and deletions (Corollary 10.2). The algorithm is the same, except that the blocks of keys are no longer those of a fixed grid. They are split and merged as the set changes, which happens rarely and changes the tree only near its root.
Consequences for dynamic optimality. By Corollary 1.2, the unified bound is a sound bound in the sense of [11 ] : it bounds the optimum. Consequently, the dynamic optimality conjecture implies the conjecture of Iacono that splay trees satisfy the unified bound, and in the same way, if Greedy is dynamically optimal [20 ] , then it satisfies the unified bound. Iacono noted that his own structure, built from several search trees rather than one, gives no such implication [29 ] , and the same is noted in [1 ] . The implication also holds with insertions and deletions: if splay trees are dynamically optimal for searches, insertions and deletions, then by Corollary 10.2 they serve every such sequence at total cost O ( n 0 + m + U B ( X ) ) O(n_0+m+\mathrm{UB}(X)) O ( n 0 + m + UB ( X )) . For sequences that start from the empty set this is the conjecture of [1 ] , stated for the total cost and up to the convention for keys that were never accessed (§ 10 ). Corollary 1.2 also transfers to every competitive BST. A BST that is c c c -competitive, that is, serves every sequence at cost O ( c ( OPT ( X ) + n ) ) O(c(\operatorname{OPT}(X)+n)) O ( c ( OPT ( X ) + n )) , therefore costs O ( c ( m + n + U B ( X ) ) ) O(c(m+n+\mathrm{UB}(X))) O ( c ( m + n + UB ( X ))) ; this applies to Tango trees [21 ] and multi-splay trees [39 ] with c = O ( log log n ) c=O(\log\log n) c = O ( log log n ) , and, by recent preprints, to splay trees with c = O ( log log n ⋅ ( log log n ) 2 ) c=O(\log\log n\cdot(\log\log n)^2) c = O ( log log n ⋅ ( log log n ) 2 ) [13 ] and to Greedy with c = 2 O ( log log n ) c=2^{O(\sqrt{\log\log n})} c = 2 O ( l o g l o g n ) [27 ] . Since U B ( X ) ≤ U B ℓ ( X ) \mathrm{UB}(X)\leq\mathrm{UB}_\ell(X) UB ( X ) ≤ UB ℓ ( X ) for every ℓ \ell ℓ , Theorem 1.1 also gives O ( m + U B ℓ ( X ) ) O(m+\mathrm{UB}_\ell(X)) O ( m + UB ℓ ( X )) for the time-windowed bound, with no dependence on ℓ \ell ℓ . For the cost of a single search, a general transformation [2 ] turns every BST algorithm into one that performs O ( log n ) O(\log n) O ( log n ) BST operations per search at the same total cost up to a constant factor.
Related work. Our structure can be seen as a multi-finger tree [12 ] with one finger for each block that it holds, kept in a weighted tree. It differs in which fingers it keeps and how they are weighted: a finger stands for a block of keys around a recent search at one of the scales 1 , 2 , 4 , … 1,2,4,\ldots 1 , 2 , 4 , … , and its weight depends only on the size of the block, which is what removes the logarithmic factor (§ 2.4 ). Storing a tree as a tree of search trees, one for each piece of a decomposition, goes back to the link-cut trees of Sleator and Tarjan [37 ] and is the basis of multi-splay trees [39 ] , where the pieces are paths of a fixed balanced tree. Our tree has two levels only: a biased search tree [7 ] whose leaves are the intervals into which the kept blocks cut the keys, and a biased search tree on the keys of each interval. The cache-splay tree of [19 ] , whose rule for keeping blocks we use, stores each of its log log n \log\log n log log n levels in splay trees, and its analysis is amortized, with a potential function and a random shift of the block boundaries. Our analysis is worst-case for each search, and § 7 compares the locality scales with the unified bound by a deterministic charging argument for the fixed grid. We do not know where the analysis of [19 ] fails; our proof uses no step of it. The lazy-finger bound of Bose, Douïeb, Iacono and Langerman [5 ] and the weighted dynamic-finger bound of Iacono and Langerman [30 ] measure the distance between consecutive searches in a static reference tree or with key weights, rather than taking the best earlier witness with a charge for recency. Outside the BST model, Rutschmann [36 ] recently gave a heap that satisfies the unified bound.
For pattern-avoiding inputs, [10 ] proved linear bounds for special classes, such as OPT ( X ) = O ( n log k ) \operatorname{OPT}(X)=O(n\log k) OPT ( X ) = O ( n log k ) for k k k -decomposable sequences, the permutations that can be reduced to a single point by repeatedly replacing a block of at most k k k consecutive values at consecutive times by one point, and Goyal and Gupta [25 ] obtained the same bound for Greedy with preprocessing. For Greedy, [10 ] proved n ⋅ 2 α ( n ) O ( k ) n\cdot2^{\alpha(n)O(k)} n ⋅ 2 α ( n ) O ( k ) on every σ \sigma σ -avoiding permutation, which Chalermsook, Pettie and Yingchareonthawornchai [18 ] improved to n ⋅ 2 ( 1 + o ( 1 ) ) α ( n ) n\cdot2^{(1+o(1))\alpha(n)} n ⋅ 2 ( 1 + o ( 1 )) α ( n ) for fixed σ \sigma σ , and [8 ] treats further classes. The proof of [6 ] builds an offline execution along a sequence of pairwise merges of rectangles that cover the permutation, provided by the twin-width decomposition of Guillemot and Marx [26 ] , and pays the square of the number of rectangles that interact in a merge. Our proof uses no decomposition of the permutation: it counts, at every scale, the cells of the grid of keys and time that contain a search, and lets an online BST pay for each search at its own scale. Opler [35 ] gave a sorting algorithm for pattern-avoiding sequences whose dependence on the pattern matches the information-theoretic lower bound, and Theorem 1.3 is the corresponding statement for online BSTs.
Open problems. The conjecture of Iacono that splay trees satisfy the unified bound [29 ] remains open, also in its form with insertions and deletions [1 ] , and so does the corresponding question for Greedy. By Proposition 8.2 , a proof for either of them would also show that it is linear on pattern-avoiding sequences, with the optimal dependence on c σ c_\sigma c σ , which would answer a question of [10 , 6 ] . The unified bound itself is not a lower bound on the optimum. As Howat, Iacono and Morin observed [28 ] , it measures distances in the whole key space and not among the keys that are in use. They propose the fresh-finger property, which measures distances among the keys of a working set, and whether a BST satisfies a bound of this kind is open.
Roadmap. § 2 gives an overview of the proof. § 3 fixes the model and states the tools we use. § 4 states the algorithm and the bound on its cost in terms of the locality scales of the searches (Theorem 4.4 ), § 5 proves the facts about blocks and regions on which the algorithm relies, and § 6 bounds its cost. § 7 derives Theorem 1.1 by comparing the locality scales with the unified bound. § 8 proves Theorem 1.3 . § 9 states that the same bounds hold for the running time on a word RAM, with the implementation and the proof in Section A, and § 10 extends the tree to insertions and deletions, with the algorithm and the proof in Section B. These two sections depend on § 4 to § 7 but not on each other or on § 8 .
Technical overview
The proof of Theorem 1.1 has two parts. The main part builds an online BST in which the cost of every search is proportional to a combinatorial quantity, the locality scale of the search, which is defined from a fixed grid of blocks of keys (Theorem 4.4 ). The second part shows that the sum of the locality scales is at most twice the unified bound plus O ( m ) O(m) O ( m ) . A reader who takes Theorem 4.4 on trust can go from § 4 directly to § 7 and § 8 .
Scales and the locality scale
We cut the keys into blocks. A block of height a a a is one of the intervals { j 2 a + 1 , … , ( j + 1 ) 2 a } \{j2^a+1,\ldots,(j+1)2^a\} { j 2 a + 1 , … , ( j + 1 ) 2 a } of 2 a 2^a 2 a consecutive keys. Blocks of different heights are nested. A block of scale s s s , in the words of the introduction, is a block of height s s s : we say height for a block, and scale for the number s s s when it also counts distinct keys, as it does next. Let K : = ⌈ log 2 n ⌉ K:=\lceil\log_2 n\rceil K := ⌈ log 2 n ⌉ , and let K ^ \widehat{K} K be the least power of two that is at least K K K . A scale is an integer s s s between 0 0 0 and K ^ \widehat{K} K ; the scales above K K K are needed only in § 2.3 . We say that a search lies in a block, and that the block is searched, if the key of the search is in the block. At scale s s s we look at the block of height s s s that contains the key x t x_t x t of a search t t t , and we ask when this block was searched last and how many distinct keys were searched since. The locality scale λ ( t ) \lambda(t) λ ( t ) of the introduction is the least scale s s s such that the block of height s s s around x t x_t x t contains the key of an earlier search u u u , and fewer than 2 s − 1 2^s-1 2 s − 1 distinct keys were searched between u u u and t t t ; in the notation of § 1 , ρ ( u , t ) + 2 ≤ 2 s \rho(u,t)+2\le2^s ρ ( u , t ) + 2 ≤ 2 s . So one number s s s measures two things, a block of 2 s 2^s 2 s keys and a set of at most 2 s 2^s 2 s distinct keys. The block of height K K K has all keys, and no search lies between t − 1 t-1 t − 1 and t t t , so λ ( t ) ≤ K \lambda(t)\le K λ ( t ) ≤ K ; for the first search we set λ ( 1 ) : = K \lambda(1):=K λ ( 1 ) := K . Draw every earlier search u u u at its key x u x_u x u and at the number ρ ( u , t ) \rho(u,t) ρ ( u , t ) of distinct keys searched after it. The scale s s s then stands for a box: the block of height s s s around x t x_t x t , of 2 s 2^s 2 s keys, times the numbers 0 , … , 2 s − 2 0,\ldots,2^s-2 0 , … , 2 s − 2 . The locality scale is the least s s s whose box contains an earlier search (Figure 1 ). We use the example of this figure, on 32 keys, throughout this section.
Figure 1. The locality scale of search 7 in the running example of this section: seven searches on 32 keys, for the keys 26, 22, 10, 30, 14, 14, 27. Each earlier search, labelled by its time, is a dot above its key, at the number of distinct keys searched after it; search 1, for instance, is followed by the four keys 22, 10, 30, 14. The keys are cut into blocks of 2 s 2^s 2 s consecutive keys for every s s s . The box of scale s s s spans, horizontally, the block of 2 s 2^s 2 s keys that contains the searched key and, vertically, the numbers 0 , … , 2 s − 2 0,\ldots,2^s-2 0 , … , 2 s − 2 . The locality scale is the least s s s whose box holds a dot (Definition 4.1 ), here λ ( 7 ) = 3 \lambda(7)=3 λ ( 7 ) = 3 .
The locality scale is a version of the unified bound on the grid: it is the term of search t t t , with the distance of the two keys replaced by the size of the smallest block that contains both. If the search u u u shows that the scale λ ( t ) \lambda(t) λ ( t ) has the property above, then ∣ x t − x u ∣ < 2 λ ( t ) \lvert x_t-x_u\rvert<2^{\lambda(t)} ∣ x t − x u ∣ < 2 λ ( t ) and ρ ( u , t ) + 2 ≤ 2 λ ( t ) \rho(u,t)+2\le2^{\lambda(t)} ρ ( u , t ) + 2 ≤ 2 λ ( t ) , so the term of t t t in UB ( X ) \operatorname{UB}(X) UB ( X ) is at most λ ( t ) + 1 \lambda(t)+1 λ ( t ) + 1 . The converse holds in total over all searches, not for each search alone (§ 2.5 ). The main part of the proof is an online BST in which every search t t t costs O ( λ ( t ) ) O(\lambda(t)) O ( λ ( t )) .
A tree for one moment, and why moving it is expensive
Call a block of height s s s recent at time t t t , or in use , in the words of the introduction, if it is among the 2 s 2^s 2 s blocks of height s s s that were searched most recently before t t t . Give one key of each recent block of height s s s the weight 4 − s 4^{-s} 4 − s , and every other key the weight 1 / n 1/n 1/ n . Summed over all heights, the total weight is O ( 1 ) O(1) O ( 1 ) , and a BST in which every key of weight w w w has depth O ( log ( 1 / w ) ) O(\log(1/w)) O ( log ( 1/ w )) exists for any weights [32 ] . Such a tree brings one key of every recent block of height s s s to depth O ( s ) O(s) O ( s ) , and the construction of § 2.4 arranges the other keys of the block within O ( s ) O(s) O ( s ) further levels. A search t t t is then served from its block of height λ ( t ) \lambda(t) λ ( t ) , at depth O ( λ ( t ) ) O(\lambda(t)) O ( λ ( t )) . This block is recent: fewer than 2 λ ( t ) 2^{\lambda(t)} 2 λ ( t ) distinct keys were searched since the last search in it, so fewer than 2 λ ( t ) 2^{\lambda(t)} 2 λ ( t ) other blocks of its height were searched since. So at any single moment a tree with the right costs exists.
Such a tree can also be maintained. In the biased search trees of Bent, Sleator and Tarjan [7 ] , an item of weight w w w has depth O ( log ( W / w ) ) O(\log(W/w)) O ( log ( W / w )) at every moment, where W W W is the total weight, and it can be inserted or removed in O ( log ( W / w ) ) O(\log(W/w)) O ( log ( W / w )) time in the worst case, by the split and join operations of these trees (§ 3.2 ). So when a search makes a block of height s s s recent, the key of this block can be put into the tree with its weight, and the key of the block that is no longer recent can be removed, at a total cost of O ( s ) O(s) O ( s ) . The trouble is the number of such changes. At a height s s s below λ ( t ) \lambda(t) λ ( t ) , the block of x t x_t x t was not searched before, or 2 s − 1 2^s-1 2 s − 1 or more distinct keys were searched since, and these keys may lie in as many different blocks. So search t t t may make a new block recent at every height below λ ( t ) \lambda(t) λ ( t ) , and paying O ( s ) O(s) O ( s ) for each gives
∑ s < λ ( t ) O ( s ) = O ( λ ( t ) 2 ) \sum_{s<\lambda(t)} O(s) = O(\lambda(t)^2) s < λ ( t ) ∑ O ( s ) = O ( λ ( t ) 2 )
per search, against the O ( λ ( t ) ) O(\lambda(t)) O ( λ ( t )) we can afford. A random permutation shows that the gap is real. All its keys are distinct and they are spread out, so for small s s s the block of height s s s around x t x_t x t is recent only if one of the last 2 s 2^s 2 s or so searches lies in it. A block of height s s s holds 2 s 2^s 2 s of the n n n keys, so this happens with probability about 4 s / n 4^s/n 4 s / n , and the height must be about 1 2 log 2 n \frac{1}{2}\log_2 n 2 1 log 2 n before the block of x t x_t x t is likely to be recent. Hence most searches make a new block recent at about 1 2 log 2 n \frac{1}{2}\log_2 n 2 1 log 2 n heights, and the tree above costs, heuristically, about n log 2 n n\log^2 n n log 2 n , whereas OPT is Θ ( n log n ) \Theta(n\log n) Θ ( n log n ) for most permutations [10 ] . For the same reason λ ( t ) \lambda(t) λ ( t ) is about 1 2 log 2 n \frac{1}{2}\log_2 n 2 1 log 2 n for most searches.
Keeping only geometrically spaced scales
The remedy is to keep fewer blocks. Our tree tracks blocks only at the promoted scales 1 , 2 , 4 , 8 , … 1,2,4,8,\ldots 1 , 2 , 4 , 8 , … , up to K ^ \hat K K ^ . They are dense enough: for every scale s s s from 1 1 1 to K K K , the least power of two s ^ ≥ s \hat s \ge s s ^ ≥ s is a promoted scale, and s ^ < 2 s \hat s < 2s s ^ < 2 s . At a promoted scale s s s , the tree keeps the recent blocks of height s s s , that is, the 2 s 2^s 2 s blocks of height s s s that were searched most recently (Figure 2 (a)). At the scale K ^ \hat K K ^ the only block is the block of all keys, so the tree always keeps it. In terms of single searches, every search promotes its blocks at all promoted scales: it makes them kept if they are not kept already. We call a promoted scale missing for a search if the block of the search at this scale is not kept when the search arrives. At a missing scale s s s the search brings in its block, and if 2 s 2^s 2 s blocks of this height are kept already, the one of them that was searched least recently is no longer kept; we say that the search retires it. At the other promoted scales nothing changes. The algorithm does this in three phases, which Figure 2 shows for the running example: it first retires blocks, then walks to the searched key, and then adds its own blocks. The missing scales of search t t t are below λ ( t ) \lambda(t) λ ( t ) , because the block of x t x_t x t at every scale from λ ( t ) \lambda(t) λ ( t ) on is recent: the count of § 2.2 for the scale λ ( t ) \lambda(t) λ ( t ) applies to every larger scale s s s as well: the block of height s s s contains the block of height λ ( t ) \lambda(t) λ ( t ) , and the number 2 s 2^s 2 s of blocks that may be kept is larger.
Figure 2. Search 7 of Figure 1 in the algorithm. A box is a kept block, drawn over its keys in the row of its height; the number in it is the time of the last search in the block. Blocks are kept only at the heights 1, 2, 4 and 8. A block of height a < 8 a < 8 a < 8 has 2 a 2^a 2 a keys, and at most 2 a 2^a 2 a blocks of this height are kept, those searched most recently; the block of height 8 consists of all keys. The right margin gives this quota for each row: at height 4 the quota of 16 exceeds the 2 blocks that exist, so a block of this height is never retired once it is searched, and only the rows of heights 1 and 2 ever retire a block. The bar below shows the regions, each shaded like the smallest kept block around it, which owns it. The darker a region, the smaller its owner and the nearer its keys are to the root (Figure 3 ). The search retires the blocks { 29 , 30 } \{29,30\} { 29 , 30 } and { 21 , … , 24 } \{21,\ldots,24\} { 21 , … , 24 } , which were searched last at the times 4 and 2, is served from the block { 17 , … , 32 } \{17,\ldots,32\} { 17 , … , 32 } , and adds the blocks { 25 , … , 28 } \{25,\ldots,28\} { 25 , … , 28 } and { 27 , 28 } \{27,28\} { 27 , 28 } .
It matters here that the tree counts blocks and not time. Had it kept, at scale s s s , the blocks in which one of the last 2 s 2^s 2 s searches lies, a search could find its block gone although only a few distinct keys were searched since the last search in it, namely when these few keys were searched many times. Its cost would then not be bounded by its locality scale, and the analysis would have to amortize over the time axis as well.
A search t t t thus replaces blocks at only O ( log λ ( t ) ) O(\log\lambda(t)) O ( log λ ( t )) scales, and the heights of these blocks grow geometrically, so adding and removing all of them costs ∑ s O ( s ) = O ( λ ( t ) ) \sum_s O(s) = O(\lambda(t)) ∑ s O ( s ) = O ( λ ( t )) over the promoted scales s < λ ( t ) s < \lambda(t) s < λ ( t ) . On a random permutation this gives O ( n log n ) O(n \log n) O ( n log n ) .
A search is still served at the right depth. Let s ^ \hat{s} s ^ be the least power of two that is at least λ ( t ) \lambda(t) λ ( t ) , so that s ^ < 2 λ ( t ) \hat{s} < 2\lambda(t) s ^ < 2 λ ( t ) ; it is a promoted scale, because λ ( t ) ≤ K \lambda(t) \le K λ ( t ) ≤ K . This scale is not missing, so the block of height s ^ \hat{s} s ^ around x t x_t x t is kept when t t t arrives, and t t t is served from a block of height less than 2 λ ( t ) 2\lambda(t) 2 λ ( t ) (Lemma 6.1 ).
From the idea to a binary search tree
What remains is to build a BST that realizes this accounting. It has to make the keys of every kept block of height a a a reachable at depth O ( a ) O(a) O ( a ) , to add the blocks of a search t t t at cost O ( λ ( t ) ) O(\lambda(t)) O ( λ ( t )) , and to remove a block at a cost proportional to its height. We describe the tree in four steps: how the kept blocks are nested, how they cut the keys into intervals, how a tree over these intervals stands for a BST on the keys, and what a change of the intervals costs. Figure 2 shows the running example.
The kept blocks form a nested family: two of them are disjoint, or one contains the other. Two facts describe this family (Lemma 5.1).
If a block is kept, then so are the blocks around it at all larger promoted scales. The reason is a count. Let a kept block g g g of height s s s lie in a block g ′ g' g ′ of a larger promoted height s ′ s' s ′ , and suppose that g ′ g' g ′ is not kept. Then at least 2 s ′ 2^{s'} 2 s ′ blocks of height s ′ s' s ′ were searched after the last search in g ′ g' g ′ , and the searches in them lie in as many different blocks of height s s s . So at least 2 s ′ 2^{s'} 2 s ′ blocks of height s s s , which is more than 2 s 2^s 2 s , were searched after the last search in g ′ g' g ′ . The last search in g g g is not later than the last search in g ′ g' g ′ , because g g g lies in g ′ g' g ′ , so all these blocks were searched after the last search in g g g , and g g g is not kept either. It follows that the next larger kept block around a kept block of height a < K ^ a < \hat K a < K ^ is the kept block of height 2 a 2a 2 a around it.
The same count shows that a block that a search retires contains no kept block of a smaller promoted scale. Let the search retire the block e e e of height s s s , and let s ′ < s s' < s s ′ < s be a promoted scale. The other 2 s − 1 2^s-1 2 s − 1 kept blocks of height s s s were searched after the last search in e e e , and the searches in them lie in as many different blocks of height s ′ s' s ′ . Since 2 s − 1 ≥ 2 s ′ 2^s-1 \ge2^{s'} 2 s − 1 ≥ 2 s ′ , a block of height s ′ s' s ′ inside e e e is not among the 2 s ′ 2^{s'} 2 s ′ most recently searched ones.
So blocks appear together at the bottom of the family, nested inside one another, as the two blocks around the key 27 do in Figure 2 (c).
The kept blocks cut the keys into intervals, which we call regions . The owner of a key is the smallest kept block that contains it, and a region is a maximal interval of consecutive keys with the same owner. In Figure 2 (a) there are nine regions. For instance, the block { 17 , … , 32 } \{17,\ldots,32\} { 17 , … , 32 } owns the keys that lie in it but in neither of the two kept blocks of height 2 inside it, and these keys form the two regions { 17 , … , 20 } \{17,\ldots,20\} { 17 , … , 20 } and { 25 , … , 28 } \{25,\ldots,28\} { 25 , … , 28 } . When a block is removed that contains no kept block, its keys form a single region. These keys pass to the next larger kept block around it, and the region is merged with the neighbouring regions of that block. Conversely, when a block is added, the region that contains it is split into at most three: the block, and what is left of the region on each side (Lemma 5.2).
Our data structure is a biased search tree whose leaves are the regions, in key order; we call it the region tree . A region whose owner has height a a a has the weight 4 − a 4^{-a} 4 − a . Every leaf carries a balanced search tree whose leaves are the keys of its region, the inner tree of the region; we use a biased search tree with equal weights. Together these trees stand for a BST on the keys (Figure 3 ). Hang every inner tree at the leaf of its region. The result is one tree whose leaves are the keys, in order, and it has exactly one internal node between any two consecutive leaves. Let this node hold the smaller of the two keys, and remove the leaves, except the last one, which holds the largest key. The internal nodes and the last leaf then form a BST on all keys. So an internal node of the region tree holds the largest key of the region on its left, and the other keys of a region sit at the internal nodes of its inner tree.
Figure 3. The BST of the algorithm before and after search 7, with every key drawn at its depth and shaded like its region. The square nodes and the thick edges below them are the region tree: its internal nodes, each of which separates two consecutive regions and holds the largest key of the region on its left, as the root 14 does for the region { 13 , 14 } \{13, 14\} { 13 , 14 } . The round nodes hold the other keys of each region, in a balanced tree of their own, the inner tree of the region. A region whose owner has height a a a has the weight 4 − a 4^{-a} 4 − a in the region tree, which is a biased search tree, so its keys have depth O ( a + 1 ) O(a + 1) O ( a + 1 ) . The orange path is the search for the key 27. In (a) it passes the keys 14, 30, 28, 20 and 24 of the region tree and then descends in the inner tree of the region { 25 , … , 28 } \{25,\ldots,28\} { 25 , … , 28 } .
This tree has two properties, which reduce the cost of the BST to that of the region tree.
Depth. At most 2 a 2^a 2 a blocks of height a a a are kept at any time. Every region begins at the first key of its owner or right after the last key of a lower kept block. Charge the region to its owner in the first case and to this lower block in the second. A kept block of height a a a is then charged at most two regions, the one at its first key and the one after its last key, and the owner of each has a height of at least a a a , so each weighs at most 4 − a 4^{-a} 4 − a . So the total weight of the regions is at most twice the sum of 4 − a 4^{-a} 4 − a over the kept blocks, which is O ( 1 ) O(1) O ( 1 ) (Lemma 6.2 ). Hence the leaf of a region whose owner has height a a a is at depth O ( a + 1 ) O(a + 1) O ( a + 1 ) . The region has at most 2 a 2^a 2 a keys, so its inner tree adds O ( a + 1 ) O(a + 1) O ( a + 1 ) levels (Lemma 6.3 ).
Changing regions. When regions are merged or split, the leaves involved, at most three, are removed from the region tree and new leaves are inserted, and their inner trees are joined or split in the same way, all in time proportional to the largest height of their owners. These operations change the BST only at the nodes that they visit, and the visited nodes contain all their ancestors. In the BST model, a search may rearrange any set of nodes that contains the root and, with each of its nodes, the parent of this node, at a cost equal to the size of the set (§ 3 ). The visited nodes are such a set, and their number is at most the running time (Lemmas 3.2 and 6.4 ).
Serving a search costs O ( λ ( t ) ) O(\lambda(t)) O ( λ ( t )) , because the region of x t x_t x t is owned by a block of height less than 2 λ ( t ) 2\lambda(t) 2 λ ( t ) . The blocks of a search are added one at a time, from the largest to the smallest. Each of them splits the region of x t x_t x t once more, at a cost proportional to the height of the block that owns this region, which is the serving block for the first of them and, for each later one, the block that was added just before it. This is O ( λ ( t ) ) O(\lambda(t)) O ( λ ( t )) in all. Removing a block of height a a a merges its region with regions of the next larger kept block around it, whose height is at most 2 a 2a 2 a , so it costs O ( a + 1 ) O(a+1) O ( a + 1 ) . A search removes at most one block at each of its missing scales, which are below λ ( t ) \lambda(t) λ ( t ) , so this is O ( λ ( t ) ) O(\lambda(t)) O ( λ ( t )) in all as well. Hence search t t t costs O ( λ ( t ) ) O(\lambda(t)) O ( λ ( t )) in the worst case, and no cost is passed to another search (§ 4 to § 6 ).
From scales to the unified bound
The grid is fixed in advance, so a search t t t can lose against its term in the unified bound: the block that contains x t x_t x t and the key of a nearby earlier search u u u may be much higher than their distance suggests, when a boundary between two high blocks falls between the two keys. The loss can be of order log n \log n log n for a single search. We show that it is small in total: a search that the grid serves badly returns to a block that no search has visited for a long time, and the searches that came in between have already paid for the loss in the unified bound.
Write τ t \tau_t τ t for the term of search t t t in the unified bound. We saw that τ t ≤ λ ( t ) + 1 \tau_t \le\lambda(t)+1 τ t ≤ λ ( t ) + 1 for every search. In the other direction we prove (Lemma 7.2 )
∑ t λ ( t ) ≤ 2 ∑ t τ t + O ( m ) . \sum_t \lambda(t) \le2\sum_t \tau_t + O(m). t ∑ λ ( t ) ≤ 2 t ∑ τ t + O ( m ) .
This gives ∑ t λ ( t ) = O ( m + UB ( X ) ) \sum_t \lambda(t)=O(m+\operatorname{UB}(X)) ∑ t λ ( t ) = O ( m + UB ( X )) , and Theorem 1.1 follows from Theorem 4.4 .
Call an earlier search that attains the term τ t \tau_t τ t the partner of t t t .
We count level by level, where a level is a height J J J of blocks. The sum ∑ t λ ( t ) \sum_t \lambda(t) ∑ t λ ( t ) is the number of pairs ( t , J ) (t,J) ( t , J ) with λ ( t ) > J \lambda(t)>J λ ( t ) > J , and we pay one unit for each such pair. If τ t > J − 2 \tau_t>J-2 τ t > J − 2 , the search t t t pays the unit itself, which happens at only τ t + O ( 1 ) \tau_t+O(1) τ t + O ( 1 ) levels. Otherwise τ t ≤ J − 2 \tau_t\le J-2 τ t ≤ J − 2 , which says that the partner u u u of t t t is close to t t t : their keys differ by less than 2 J − 2 2^{J-2} 2 J − 2 , and fewer than 2 J − 2 2^{J-2} 2 J − 2 distinct keys were searched between them. Now λ ( t ) > J \lambda(t)>J λ ( t ) > J means that the block of height J J J around x t x_t x t was never searched before, or that at least 2 J − 1 2^J-1 2 J − 1 distinct keys were searched since the last search in it. So the search u u u , which is recent, does not lie in this block. Its key is less than a quarter of a block away from x t x_t x t , so it lies in the neighbouring block, just across the boundary β \beta β between the two (Figure 4 (a)). We call such a search t t t a crossing of level J J J . Here others must pay for t t t , and they are the searches made near this boundary while the block of t t t stayed unvisited.
Figure 4. The charging in the proof of Lemma 7.2 . Keys are horizontal and time runs downwards; a dot is a search, and the dashed line is the boundary β \beta β between two blocks of height J J J . An arrow leads from a search to its partner, an earlier search that attains its term in the unified bound. A crossing of level J J J is a search t t t with λ ( t ) > J \lambda(t)>J λ ( t ) > J whose partner u u u is so near in key and in time that x u x_u x u lies just across β \beta β , and v J ( t ) v_J(t) v J ( t ) is the last search before t t t in the block B J ( x t ) B_J(x_t) B J ( x t ) . In (a), no search lies in the block of the crossing t t t between the times v J ( t ) v_J(t) v J ( t ) and t t t , and the chain of partners of t t t stays after v J ( t ) v_J(t) v J ( t ) and near β \beta β . In (b), if a search s s s were on the chains of two crossings t 1 < t 2 t_1<t_2 t 1 < t 2 of level J J J , then t 1 t_1 t 1 or its partner would be a search in the block of t 2 t_2 t 2 after v J ( t 2 ) v_J(t_2) v J ( t 2 ) .
To find them, follow partners backwards: from u u u to the partner of u u u , from there to its partner, and so on. We call these searches the chain of t t t ; they are the dots joined by arrows in Figure 4 (a). A step from a search with term τ ′ \tau' τ ′ to its partner moves by less than 2 τ ′ 2^{\tau'} 2 τ ′ in key and passes fewer than 2 τ ′ 2^{\tau'} 2 τ ′ distinct keys in time. We follow the chain until the numbers 2 τ ′ 2^{\tau'} 2 τ ′ of its searches add up to at least 2 J − 2 2^{J-2} 2 J − 2 , a quarter of the size of a block of height J J J . Each search of the chain then pays the share 2 τ ′ / 2 J − 2 2^{\tau'}/2^{J-2} 2 τ ′ / 2 J − 2 of the unit, or the whole unit if this share is more than one. The shares add up to at least one, by the rule for ending the chain, so the unit is paid. The chain cannot run out before that. Only the first search of the sequence has no partner, and its term in the unified bound is log 2 ( n + 2 ) \log_2(n+2) log 2 ( n + 2 ) , which exceeds every level J J J , since a level that we count is less than λ ( t ) ≤ ⌊ log 2 n ⌋ \lambda(t)\le\lfloor\log_2 n\rfloor λ ( t ) ≤ ⌊ log 2 n ⌋ . So if the chain reaches the first search, the number 2 τ ′ 2^{\tau'} 2 τ ′ of this search alone is more than 2 J − 2 2^{J-2} 2 J − 2 , and the chain ends there.
Two facts make this charging sound; both are proved in Lemma 7.2 . The first is that the chain stays near the boundary and inside the period in which the block of t t t was not visited. Indeed, before its last search the numbers 2 τ ′ 2^{\tau'} 2 τ ′ of the chain add up to less than 2 J − 2 2^{J-2} 2 J − 2 , and the first step, from t t t to u u u , adds less than 2 τ t ≤ 2 J − 2 2^{\tau_t}\le2^{J-2} 2 τ t ≤ 2 J − 2 . So every search s i s_i s i of the chain, in the notation of Figure 4 (a), satisfies
∣ x s i − β ∣ < 2 J − 2 + 2 J − 2 = 2 J − 1 , \lvert x_{s_i}-\beta\rvert< 2^{J-2}+2^{J-2}=2^{J-1}, ∣ x s i − β ∣ < 2 J − 2 + 2 J − 2 = 2 J − 1 ,
which is half a block. The same sum bounds the time: the distinct keys searched between s i s_i s i and t t t are those passed by the steps from t t t back to s i s_i s i , together with the keys of the chain searches in between, and a step from a search with term τ ′ \tau' τ ′ passes at most 2 τ ′ − 2 2^{\tau'}-2 2 τ ′ − 2 distinct keys, so the same sum bounds their number by 2 J − 1 2^{J-1} 2 J − 1 (Lemma 7.1 ). This is fewer than the 2 J − 1 2^J-1 2 J − 1 distinct keys that were searched since the last search in the block of t t t , so s i s_i s i is later than that search.
The second fact is that, at one level J J J , a search is on the chain of at most one such t t t (Figure 4 (b)). Suppose that a search, called s s s in the figure, is on the chains of t 1 < t 2 t_1<t_2 t 1 < t 2 . Both chains stay within half a block of their boundaries, and the boundaries between blocks of height J J J are 2 J 2^J 2 J keys apart, so the two boundaries are the same. The search t 1 t_1 t 1 and its partner have their keys in the two blocks next to this boundary, one on each side, and the block of t 2 t_2 t 2 is one of these two blocks. So t 1 t_1 t 1 or its partner is a search in the block of t 2 t_2 t 2 , and it is before t 2 t_2 t 2 . It is not earlier than s s s , because the chain of t 1 t_1 t 1 starts at the partner of t 1 t_1 t 1 and runs backwards. But by the first fact, s s s is later than the last search in the block of t 2 t_2 t 2 before t 2 t_2 t 2 . This is a contradiction.
Hence a search with term τ ′ \tau' τ ′ pays a full unit only at the levels J ≤ τ ′ + 2 J\le\tau'+2 J ≤ τ ′ + 2 , and at a higher level J J J it pays at most the share 2 τ ′ / 2 J − 2 2^{\tau'}/2^{J-2} 2 τ ′ / 2 J − 2 ; these shares sum to O ( 1 ) O(1) O ( 1 ) over the levels. Each search thus pays about its term twice, once for itself and once for others, which gives the inequality.
Pattern-avoiding sequences
For Theorem 1.3 we run the same tree. Cut the time axis into epochs of 2 s 2^s 2 s consecutive searches, and call a block of height s s s together with such an epoch a cell of scale s s s . A search with λ ( t ) > s \lambda(t)>s λ ( t ) > s is the first search in its cell of scale s s s : an earlier search u u u in the same cell would lie in the block of x t x_t x t , with at most 2 s − 2 2^s-2 2 s − 2 searches between u u u and t t t . View the grid of scale s s s as a 0-1 matrix with one row per block, one column per epoch, and a one wherever a search lies in the cell. If this matrix contained the pattern σ \sigma σ , choosing one search in each of the corresponding cells would give an occurrence of σ \sigma σ in X X X . The matrix has O ( n 2 − s ) O(n2^{-s}) O ( n 2 − s ) rows and O ( 1 + m 2 − s ) O(1+m2^{-s}) O ( 1 + m 2 − s ) columns, so by the theorem of Marcus and Tardos it has O ( c σ ( m + n ) 2 − s ) O(c_\sigma(m+n)2^{-s}) O ( c σ ( m + n ) 2 − s ) ones. Hence # { t : λ ( t ) > s } ≤ min ( m , O ( c σ ( m + n ) 2 − s ) ) \#\{t:\lambda(t)>s\}\leq\min(m,O(c_\sigma(m+n)2^{-s})) # { t : λ ( t ) > s } ≤ min ( m , O ( c σ ( m + n ) 2 − s )) , and summing over s s s gives ∑ t λ ( t ) = O ( ( m + n ) ( 1 + log c σ ) ) \sum_t\lambda(t)=O((m+n)(1+\log c_\sigma)) ∑ t λ ( t ) = O (( m + n ) ( 1 + log c σ )) .
Preliminaries
This section fixes the model of computation and states two tools: a lemma that lets a search pay for a sequence of changes of the tree one by one, and the biased search trees of Bent, Sleator and Tarjan.
The BST model
We use the model of Demaine, Harmon, Iacono, Kane and Pătraşcu [20 ] , which agrees up to a constant factor with the classical models of BSTs with rotations [38 , 40 ] . A BST stores the keys 1 , … , n 1,\ldots,n 1 , … , n . To serve a search for a key x x x , an algorithm chooses a set U U U of nodes that contains x x x and the root and is connected, and it replaces the subtree formed by U U U by any BST on the same keys; the subtrees that hang off U U U are reattached in the unique way that keeps a BST. The cost of the search is ∣ U ∣ |U| ∣ U ∣ . Only these nodes are counted: the computation that decides which nodes to touch, including the records that our algorithm keeps about past searches, is free. A node counts as an ancestor of itself, and the depth of a node is the number of its other ancestors.
For a sequence X = ( x 1 , … , x m ) X=(x_1,\ldots,x_m) X = ( x 1 , … , x m ) of keys, OPT ( X ) \operatorname{OPT}(X) OPT ( X ) is the least total cost of serving X X X , over all initial trees and all choices of the sets U U U . An algorithm is online if its choices while serving x t x_t x t depend only on x 1 , … , x t x_1,\ldots,x_t x 1 , … , x t . Our algorithms choose their initial tree, which depends only on n n n .
A set of nodes of a BST is a top set if it contains every ancestor of each of its nodes. A search for x x x may replace any top set that contains x x x , at a cost equal to its size. Our algorithm goes from the tree before a search to the tree after it through several intermediate trees, one for each of its steps, which we call operations . The next lemma lets the search pay for them one by one.
Lemma 3.1 (Composing operations). Let T 0 , T 1 , … , T k T^0,T^1,\ldots,T^k T 0 , T 1 , … , T k be BSTs on the same keys. For 1 ≤ j ≤ k 1\leq j\leq k 1 ≤ j ≤ k , let U j U_j U j be a top set of T j − 1 T^{j-1} T j − 1 such that every node outside U j U_j U j has the same children in T j − 1 T^{j-1} T j − 1 and in T j T^j T j . If some U j U_j U j contains the key x x x , then a search for x x x can transform T 0 T^0 T 0 into T k T^k T k at cost at most ∑ j = 1 k ∣ U j ∣ \sum_{j=1}^{k}|U_j| ∑ j = 1 k ∣ U j ∣ .
Proof. Let V : = U 1 ∪ ⋯ ∪ U k V:=U_1\cup\cdots\cup U_k V := U 1 ∪ ⋯ ∪ U k . A node w w w outside V V V has the same children in all the trees T 0 , … , T k T^0,\ldots,T^k T 0 , … , T k , and these children are outside V V V as well, because a child of w w w in a top set U j U_j U j of T j − 1 T^{j-1} T j − 1 would put its parent w w w into U j U_j U j . So the subtree of w w w is the same in T 0 T^0 T 0 and in T k T^k T k , and it is outside V V V . Hence V V V is a top set of both trees, with the same subtrees hanging off it, and T k T^k T k arises from T 0 T^0 T 0 when one search for x x x replaces the subtree formed by V V V , at cost ∣ V ∣ ≤ ∑ j = 1 k ∣ U j ∣ |V|\leq\sum_{j=1}^{k}|U_j| ∣ V ∣ ≤ ∑ j = 1 k ∣ U j ∣ . □
We always use the lemma in the same way. The cost of an operation that turns a tree T T T into a tree T ′ T' T ′ is the number of keys that change a child or are ancestors in T T T of such a key; these keys form a top set of T T T , which serves as U j U_j U j . The walk from the root to the searched key is one more step, which changes nothing, with the searched key and its ancestors as top set. It may be placed before, between or after the operations. So a search costs at most the sum of the costs of its operations, plus one more than the depth of the searched key in whichever of the trees is convenient.
Biased search trees
We use the globally biased binary trees of Bent, Sleator and Tarjan [7 Section 4] . Such a tree is a full binary search tree whose leaves hold items with positive weights, in order; its internal nodes hold no items. Every node stores an integer, its rank , which for the leaf of an item of weight w w w is ⌊ log 2 w ⌋ \lfloor\log_2 w\rfloor ⌊ log 2 w ⌋ , and the rules of [7 ] that relate the rank of a node to the ranks of its children keep heavy items near the root. We use these rules only through the three bounds below. We import three bounds, in which W W W is the total weight of the trees involved; the last two hold in the worst case [7 p. 563] .
Join. Two trees, the items of the first preceding those of the second, can be joined in time O ( 1 + log ( W / ( w − + w + ) ) ) O(1+\log(W/(w_-+w_+))) O ( 1 + log ( W / ( w − + w + ))) , where w − w_- w − is the weight of the last item of the first tree and w + w_+ w + that of the first item of the second [7 Theorem 8] .
Split. A tree can be split at the leaf of an item of weight w w w , into the tree of the items before it and the tree of the items after it, in time O ( 1 + log ( W / w ) ) O(1+\log(W/w)) O ( 1 + log ( W / w )) [7 Theorem 10] .
Weights below 1 are allowed, because multiplying all weights by a power of two shifts all ranks equally, and where the algorithms leave a choice we fix one, so that they are deterministic. We use these trees in two ways: with weights, for a tree whose leaves are intervals of keys, and with all weights 1, for a tree on the keys of one interval. With N N N leaves of weight 1, every leaf has depth O ( 1 + log N ) O(1+\log N) O ( 1 + log N ) , and a join or a split takes O ( 1 + log N ) O(1+\log N) O ( 1 + log N ) time.
Our algorithm changes its trees by a few joins and splits at a time. The next lemma bounds the part of a tree that they can affect, and its proof uses how the two algorithms proceed [7 pp. 560–563] , not only their running times. Every node has a record , which consists of its rank and of its pointers to its children and to its parent, and an operation visits a node when it reads or writes its record. The two algorithms proceed as follows.
Join. The paper has two forms of join, the local join [7 p. 560] and the global join [7 pp. 561–563] ; the joins that our algorithm calls are global joins. In either form, a join starts at the roots of its two trees and from then on moves only from a node to a child of this node, down the right spine of the first tree and the left spine of the second. The root of the tree that it returns is a new node or a node that the join has visited.
Split. A split at a leaf [7 pp. 560–561] starts at this leaf and moves from each node to its parent until it reaches the root, and it removes the nodes of this path. On the way it collects the subtrees that hang off the path, whose roots are children of path nodes, into two trees, one for each side of the leaf. The first subtree on a side is taken as it is, and each later one is joined, by a local join, with the tree collected so far on its side.
We use two consequences of this description.
Changes. The children of a node are stored in its record, so an operation changes the children of a node, or removes the node, only by writing its record, which is a visit.
Moves. An operation comes to a node that it has not visited before in one of three ways: from the parent of the node at that moment, in a join; from a child of the node, on the walk of a split to the root, which then goes on to the parent of the node; or as the root of a tree to which a join is applied. In the third case, if the join belongs to a split, the node is the root of a subtree that hung off the path of the split. Indeed, the join is applied to such a subtree and to the tree collected so far, which is again such a subtree, taken as it is, or was returned by an earlier join of the split, and then its root is new or was visited by that join.
Lemma 3.2 (Visited nodes). Apply a sequence of joins and splits to some trees, the starting trees, and let V V V be the set of the internal nodes of the starting trees that the operations visit.
Part 1. In every starting tree, the nodes of V V V form a top set.
Part 2. Every internal node of a starting tree outside V V V keeps its two children until the end.
Part 3. ∣ V ∣ |V| ∣ V ∣ is at most a constant times the total running time of the operations.
Proof. Part 2 is the first consequence, and Part 3 holds because every visit takes constant time. For Part 1, let z ∈ V z \in V z ∈ V have the parent y y y in its starting tree. We show that y ∈ V y \in V y ∈ V ; going up from z z z then gives Part 1. Consider the first visit to z z z . If y y y is no longer the parent of z z z at this moment, then y y y has been removed or has changed a child, so y y y has been visited. Otherwise, by the second consequence, the operation comes to z z z from y y y , or from a child of z z z on the walk of a split, which goes on to y y y , or z z z is the root of a tree to which a join is applied. In the last case z z z still has the parent y y y . A join that is called on its own is applied to whole trees, whose roots have no parents, so this join belongs to a split, and the tree hangs off the path of this split. Then y y y lies on this path, which the split visits. □
The algorithm
This section states our online BST completely and without proofs; the ideas behind it are those of § 2.3 and § 2.4 . § 5 and § 6 prove the facts that its steps rely on and the bound on its cost.
The grid and the locality scale
We first fix the blocks of keys with which the algorithm works, and the quantity that bounds its cost. Throughout § 4 to § 6 , n ≥ 2 n \ge2 n ≥ 2 , the keys are 1 , … , n 1,\ldots,n 1 , … , n , and X = ( x 1 , … , x m ) X=(x_1,\ldots,x_m) X = ( x 1 , … , x m ) is a sequence of searches. Let K : = ⌈ log 2 n ⌉ K := \lceil\log_2 n \rceil K := ⌈ log 2 n ⌉ . As in § 1 , ρ ( u , t ) \rho(u,t) ρ ( u , t ) is the number of distinct keys among x u + 1 , … , x t − 1 x_{u+1},\ldots,x_{t-1} x u + 1 , … , x t − 1 , for times u < t u < t u < t .
Blocks. Let K ^ \widehat{K} K be the least power of two with K ^ ≥ K \widehat{K} \ge K K ≥ K . For 0 ≤ a ≤ K ^ 0 \le a \le\widehat{K} 0 ≤ a ≤ K , the blocks of height a a a are the nonempty sets
{ j 2 a + 1 , … , ( j + 1 ) 2 a } ∩ { 1 , … , n } , j ≥ 0. \{j2^a+1,\ldots,(j+1)2^a\}\cap\{1,\ldots,n\},\qquad j\ge0. { j 2 a + 1 , … , ( j + 1 ) 2 a } ∩ { 1 , … , n } , j ≥ 0.
They partition the keys into intervals of 2 a 2^a 2 a consecutive keys, except that the last one may be shorter. We write B a ( y ) B_a(y) B a ( y ) for the block of height a a a that contains the key y y y and h ( g ) h(g) h ( g ) for the height of a block g g g . A block of height 0 0 0 is a single key, and for K ≤ a ≤ K ^ K \le a \le\widehat{K} K ≤ a ≤ K the only block of height a a a consists of all keys. We call the block of height K ^ \widehat{K} K the top block . Since 2 a 2^a 2 a divides 2 a ′ 2^{a'} 2 a ′ for a ≤ a ′ a \le a' a ≤ a ′ , we have B a ( y ) ⊆ B a ′ ( y ) B_a(y) \subseteq B_{a'}(y) B a ( y ) ⊆ B a ′ ( y ) , so two blocks are either disjoint or nested. Two blocks of different heights can consist of the same keys, at the right end of the key range and at the heights from K K K on; we still regard them as different blocks. The blocks of all heights form a grid on the keys that is fixed in advance and does not depend on the searches. A search u u u lies in a block g g g if x u ∈ g x_u \in g x u ∈ g , and g g g is searched before a time t t t if some search u < t u < t u < t lies in it.
Scales. A scale is an integer s ∈ { 0 , … , K ^ } s \in\{0,\ldots,\widehat{K}\} s ∈ { 0 , … , K } . A scale stands for a height of blocks and at the same time for a number of keys: at scale s s s we look at the block of 2 s 2^s 2 s keys around a key, and we count up to 2 s 2^s 2 s distinct keys in time.
Definition 4.1 (Locality scale). The locality scale λ ( t ) \lambda(t) λ ( t ) of a search t ≥ 2 t \ge2 t ≥ 2 is the least scale s s s such that some search u < t u < t u < t satisfies
x u ∈ B s ( x t ) and ρ ( u , t ) + 2 ≤ 2 s . x_u \in B_s(x_t) \qquad\text{and} \qquad\rho(u,t)+2 \le2^s. x u ∈ B s ( x t ) and ρ ( u , t ) + 2 ≤ 2 s . For the first search, λ ( 1 ) : = K \lambda(1) := K λ ( 1 ) := K .
The scale K K K has this property with u = t − 1 u=t-1 u = t − 1 , because the block of height K K K contains all keys, among them x t − 1 x_{t-1} x t − 1 , and ρ ( t − 1 , t ) = 0 \rho(t-1,t)=0 ρ ( t − 1 , t ) = 0 and 2 K ≥ 2 2^K \ge2 2 K ≥ 2 ; the scale 0 0 0 does not have it. So 1 ≤ λ ( t ) ≤ K 1 \le\lambda(t) \le K 1 ≤ λ ( t ) ≤ K for every search t t t . If a scale s s s has the property with a search u u u , then every larger scale s ′ s' s ′ has it with the same u u u , because B s ( x t ) ⊆ B s ′ ( x t ) B_s(x_t) \subseteq B_{s'}(x_t) B s ( x t ) ⊆ B s ′ ( x t ) and 2 s ≤ 2 s ′ 2^s \le2^{s'} 2 s ≤ 2 s ′ . Hence, for every search t ≥ 2 t \ge2 t ≥ 2 and every scale s s s ,
min { ρ ( u , t ) : u < t , x u ∈ B s ( x t ) } ≤ 2 s − 2 ⟺ s ≥ λ ( t ) , (1) \min\{\rho(u,t): u<t,\ x_u\in B_s(x_t)\}\le2^s-2 \quad\Longleftrightarrow\quad s\ge\lambda(t), \tag*{(1)} min { ρ ( u , t ) : u < t , x u ∈ B s ( x t )} ≤ 2 s − 2 ⟺ s ≥ λ ( t ) , (1)
where the minimum of the empty set is ∞ \infty ∞ . Finally, ρ ( u , t ) \rho(u,t) ρ ( u , t ) does not increase when u u u increases, because the searches strictly between a later u u u and t t t are among those between an earlier u u u and t t t . So the minimum in (1) is attained by the last search before t t t whose key lies in B s ( x t ) B_s(x_t) B s ( x t ) , and at a scale s s s it suffices to test this one search.
Kept blocks
The tree holds near its root the blocks that were searched most recently, at a sparse set of scales.
Promoted scales. The tree keeps blocks only at the heights in
S : = { 1 , 2 , 4 , … , K ^ } = { 2 j : j ≥ 0 , 2 j ≤ K ^ } . S := \{1,2,4,\ldots,\widehat{K}\}=\{2^j:j\ge0,\ 2^j\le\widehat{K}\}. S := { 1 , 2 , 4 , … , K } = { 2 j : j ≥ 0 , 2 j ≤ K } .
These promoted scales are spaced geometrically, so that the promoted scales below any number λ \lambda λ sum to less than 2 λ 2\lambda 2 λ . They are also dense enough: for every scale s s s with 1 ≤ s ≤ K 1\le s\le K 1 ≤ s ≤ K , the least power of two s ^ \widehat{s} s with s ^ ≥ s \widehat{s}\ge s s ≥ s is a promoted scale, and s ^ < 2 s \widehat{s}<2s s < 2 s . The largest promoted scale is K ^ \widehat{K} K , and its only block is the top block.
The most recent blocks. Fix a time t ∈ { 1 , … , m + 1 } t\in\{1,\ldots,m+1\} t ∈ { 1 , … , m + 1 } , where the time m + 1 m+1 m + 1 stands for the moment after the last search. For a block g g g that is searched before t t t , the latest search u < t u<t u < t that lies in g g g is the last search in g g g before t t t . Two blocks of the same height are disjoint, so they have different last searches. Of two blocks of the same height that are searched before t t t , the one with the later last search was searched more recently .
Definition 4.2 (Kept blocks). Let s ∈ S s\in S s ∈ S and t ∈ { 1 , … , m + 1 } t\in\{1,\ldots,m+1\} t ∈ { 1 , … , m + 1 } . The kept blocks of height s s s at time t t t are the 2 s 2^s 2 s blocks of height s s s that were searched most recently before t t t ; if fewer than 2 s 2^s 2 s blocks of height s s s are searched before t t t , all of these are kept. The top block is kept at all times. We write F t \mathcal{F}_t F t for the set of the blocks that are kept at time t t t . A promoted scale s s s is missing for search t t t if B s ( x t ) ∉ F t B_s(x_t)\notin\mathcal{F}_t B s ( x t ) ∈ / F t .
So F 1 \mathcal{F}_1 F 1 consists of the top block, and the rule is online: F t \mathcal{F}_t F t is determined by x 1 , … , x t − 1 x_1,\ldots,x_{t-1} x 1 , … , x t − 1 . After search t t t , the block B s ( x t ) B_s(x_t) B s ( x t ) is the most recently searched block of its height, so it is kept at time t + 1 t+1 t + 1 ; in this sense search t t t promotes its blocks at all promoted scales. At a missing scale s s s , if 2 s 2^s 2 s blocks of height s s s are kept, the one of them that was searched least recently is no longer kept after the search; we call it the victim of the scale.
The algorithm maintains a set F \mathcal{F} F of blocks, which is F t \mathcal{F}_t F t when search t t t begins and F t + 1 \mathcal{F}_{t+1} F t + 1 when it ends (Lemma 5.1). It retires a block when it removes the block from F \mathcal{F} F , and it adds a block when it puts the block into F \mathcal{F} F . At a moment during a search, the kept blocks are the blocks of F \mathcal{F} F at this moment.
Owners. Let F \mathcal{F} F be a set of blocks that contains the top block. The owner own F ( y ) \operatorname{own}_{\mathcal{F}}(y) own F ( y ) of a key y y y is the block of least height in F \mathcal{F} F that contains y y y . The parent of a block g ∈ F g \in\mathcal{F} g ∈ F other than the top block is the block of least height in F \mathcal{F} F among those that contain g g g and are higher than g g g . The serving block f t f_t f t of search t t t is the owner of x t x_t x t in F t \mathcal{F}_t F t , the smallest kept block around the searched key.
Regions and the tree they stand for
The kept blocks cut the keys into intervals.
Regions. Let F \mathcal{F} F be a set of blocks that contains the top block. A region of F \mathcal{F} F is a maximal interval of consecutive keys that have the same owner. We say that this block owns the region, and the height h ( Q ) h(Q) h ( Q ) of a region Q Q Q is the height of its owner. We write Reg ( F ) \operatorname{Reg}(\mathcal{F}) Reg ( F ) for the set of the regions of F \mathcal{F} F . By definition the regions partition the keys into intervals. A block owns the keys that lie in it and in no block of F \mathcal{F} F of smaller height, so it owns at most one region between any two consecutive such blocks inside it and at most one at each end. Figure 2 (a) shows the regions of the running example.
The region tree and the inner trees. A region tree is a full binary tree T \mathcal{T} T whose leaves are the regions, in key order. An inner tree of a region Q Q Q is a full binary tree whose leaves are the keys of Q Q Q , in order. Given a region tree and an inner tree for each of its regions, replace every leaf of T \mathcal{T} T by the inner tree of its region. The result is one full binary tree whose leaves are the keys 1 , … , n 1,\ldots,n 1 , … , n , in order. We call it the expanded tree . Its internal nodes are those of T \mathcal{T} T and those of the inner trees. It stands for a BST on the keys in the following way.
Definition 4.3 (Realization). The realization T ( T ) \mathcal{T}(\mathcal{T}) T ( T ) of a region tree T \mathcal{T} T with inner trees for its regions is obtained from the expanded tree in two steps:
every internal node gets the key of the last leaf in its left subtree, and
the leaves are removed, except the last leaf, which keeps the key n n n .
Every internal node of a full binary tree separates two consecutive leaves, the last leaf of its left subtree and the first leaf of its right subtree, and each pair of consecutive leaves is separated by exactly one internal node. So a key y < n y<n y < n is the key of exactly one node, the node that separates the leaves of y y y and y + 1 y+1 y + 1 , and the key n n n is at its own leaf. The node of y y y is an ancestor of the leaf of y y y , so the depth of a key in T ( T ) \mathcal{T}(\mathcal{T}) T ( T ) is at most the depth of its leaf in the expanded tree. The keys are also in search-tree order, so T ( T ) \mathcal{T}(\mathcal{T}) T ( T ) is a BST on { 1 , … , n } \{1,\ldots,n\} { 1 , … , n } (Lemma 5.3 ). An internal node of T \mathcal{T} T gets the largest key of the last region in its left subtree, and the other keys of a region are at the internal nodes of its inner tree, except the key n n n . Figure 3 in § 2.4 shows the realization in the situation of Figure 2 (a).
Algorithm 1 The online BST: members and the procedures Initialize and Search.
1: data structure OnlineBST
2: members
3: $\mathcal{F}$, a set of blocks ▷ the kept blocks
4: $\mathcal{T}$, a globally biased binary tree on the regions of $\mathcal{F}$ ▷ the region tree; the BST is $T(\mathcal{T})$
5: for every region, a globally biased binary tree on its keys, with weights 1 ▷ the inner trees
6: end members
7: procedure Initialize($n$)
8: $\mathcal{F} \leftarrow \{\text{the top block}\}$
9: $\mathcal{T} \leftarrow$ the tree with one leaf, the region $\{1,\ldots,n\}$ ▷ its inner tree holds all keys
10: end procedure
11: procedure Search($t$) ▷ search $t$, for the key $x_t$
12: $M \leftarrow \{s \in S : B_s(x_t) \notin \mathcal{F}\}$ ▷ the missing scales of search $t$
13: /* Phase 1: make room at the missing scales */
14: for each $s \in M$, by increasing $s$ do
15: if $\mathcal{F}$ has $2^s$ blocks of height $s$ then
16: $e \leftarrow$ the block of height $s$ in $\mathcal{F}$ that was searched least recently ▷ the victim
17: Retire($e$) ▷ cost $O(s+1)$ ({xref}`Lemma 6.5`, Part 1)
18: end if
19: end for
20: /* Phase 2: serve the search */
21: walk from the root of $T(\mathcal{T})$ down to $x_t$ ▷ cost $O(\lambda(t))$ ({xref}`Lemma 6.5`, Part 2)
22: /* Phase 3: promote the blocks of $x_t$ at the missing scales */
23: for each $s \in M$, by decreasing $s$ do
24: Add($B_s(x_t)$) ▷ cost $O(\lambda(t))$ for all of them ({xref}`Lemma 6.5`, Part 3)
25: end for
26: end procedure
27: end data structure
The algorithm maintains the region tree and the inner trees as globally biased binary trees (§ 3.2 ). In the region tree, a region Q Q Q has the weight
w ( Q ) : = 4 − h ( Q ) , w(Q) := 4^{-h(Q)}, w ( Q ) := 4 − h ( Q ) ,
and in an inner tree every key has the weight 1. With these weights every key of a region Q Q Q has depth O ( h ( Q ) + 1 ) O(h(Q)+1) O ( h ( Q ) + 1 ) in T ( T ) T(\mathcal{T}) T ( T ) (Lemma 6.3 ).
Processing a search
The procedure Search of Algorithm 1 processes search t t t in three phases: it makes room at the missing scales by retiring their victims, walks down to x t x_t x t , and adds the blocks of x t x_t x t at the missing scales. Algorithms 2 to 4 give the procedures Retire, Add and ChangeRegions, which change the region tree and the inner trees, and with them the BST T ( T ) T(\mathcal{T}) T ( T ) , by the splits and joins of § 3.2 . Initially the top block is the only kept block, and all keys form one region, whose inner tree is a fixed globally biased tree on all keys. Which kept block of a height was searched least recently is part of the records about past searches, which are free in the model of § 3 ; Section A maintains them. § 5.2 verifies that the regions named in Algorithms 2 and 3 exist and that after each step the leaves of T \mathcal{T} T are the regions of F \mathcal{F} F .
Make room. When a victim retires, it contains no kept block of smaller height (Lemma 5.1), so its keys form a single region. When such a block g g g is removed, its keys pass to its parent p p p , and they join the regions that p p p owns on either side of g g g , if there are any; a neighbouring region that p p p does not own stays as it is. This is the reverse of the addition of a block, described under Promote below: at most three consecutive regions become one region again.
Algorithm 2 The online BST, procedure RETIRE: remove a block that contains no kept block of smaller height.
data structure ONLINEBST ▷ continues Algorithm 1
procedure RETIRE(g g g ) ▷ the keys of g g g form one region; the parent of g g g is p p p
Q ← Q \gets Q ← the list of the region g g g and of those of its two neighbouring regions that p p p owns
F ← F ∖ { g } \mathcal{F} \gets\mathcal{F} \setminus\{g\} F ← F ∖ { g }
CHANGEREGIONS(Q Q Q , the union of the regions of Q Q Q )
end procedure
end data structure
Serve. The search only walks down; the region tree does not change. The serving block f t f_t f t owns the region of x t x_t x t , so x t x_t x t has depth O ( h ( f t ) + 1 ) O(h(f_t)+1) O ( h ( f t ) + 1 ) , and the serving block is low: h ( f t ) < 2 λ ( t ) h(f_t)<2\lambda(t) h ( f t ) < 2 λ ( t ) (Lemma 6.1 ).
Promote. The blocks of x t x_t x t at the missing scales are added one at a time, from the highest to the lowest. A block g g g that is added contains no kept block of smaller height, so all its keys have the same owner and lie in one region Q Q Q , the region of x t x_t x t at this moment (Lemma 5.2). Afterwards g g g owns its keys, and the old owner still owns what is left of Q Q Q on the two sides of g g g . So Q Q Q is cut into at most three regions: the keys Q L Q_L Q L of Q Q Q that are smaller than those of g g g , the block g g g , and the keys Q R Q_R Q R of Q Q Q that are larger, where Q L Q_L Q L and Q R Q_R Q R may be empty. The next block is added inside the region g g g .
Algorithm 3 The online BST, procedure ADD: add a block that contains no kept block of smaller height.
data structure ONLINEBST ▷ continues Algorithm 1
procedure ADD(g g g ) ▷ the keys of g g g lie in one region
Q ← Q \gets Q ← the region that contains g g g
F ← F ∪ { g } \mathcal{F} \gets\mathcal{F} \cup\{g\} F ← F ∪ { g }
CHANGEREGIONS(Q Q Q , the nonempty ones of Q L Q_L Q L , g g g , Q R Q_R Q R ) ▷ the regions of F \mathcal{F} F inside Q Q Q
end procedure
end data structure
Change of regions. RETIRE and ADD only decide which regions are replaced by which, and CHANGEREGIONS changes the trees. For the inner trees, it joins those of the old regions into one tree I I I and then cuts I I I into the inner trees of the new regions, where to cut a tree after a key y y y is to split it at the leaf of y y y and to join this leaf to the tree of the smaller keys. All joins of several trees go from left to right. A call costs O ( a + 1 ) O(a+1) O ( a + 1 ) , where a a a is the largest height of a region that it removes or creates (Lemma 6.4 ).
Algorithm 4 The online BST, procedure ChangeRegions: replace the consecutive leaves Q Q Q of T \mathcal{T} T by the regions Q ′ Q^{\prime} Q ′ , which have the same union. Each of the two lists has at most three regions. The comments give the time of each line, where a a a is the largest height of a region of Q Q Q or Q ′ Q^{\prime} Q ′ (Lemma 6.4 ).
1: data structure OnlineBST ▷ continues Algorithm 1 2: procedure ChangeRegions(Q Q Q , Q ′ Q^{\prime} Q ′ ) 3: split T \mathcal{T} T at the leaves of Q Q Q , into T 1 T_1 T 1 , Q Q Q , T 2 T_2 T 2 ▷ O ( a + 1 ) O(a+1) O ( a + 1 ) 4: I ← I \leftarrow I ← the join of the inner trees of Q Q Q ▷ O ( a + 1 ) O(a+1) O ( a + 1 ) 5: cut I I I into the inner trees of Q ′ Q^{\prime} Q ′ ▷ O ( a + 1 ) O(a+1) O ( a + 1 ) 6: T ← \mathcal{T} \leftarrow T ← the join of T 1 T_1 T 1 , the regions of Q ′ Q^{\prime} Q ′ as single leaves, and T 2 T_2 T 2 ▷ O ( a + 1 ) O(a+1) O ( a + 1 ) 7: end procedure 8: end data structure
The main technical result of the paper is that this algorithm is an online BST in which every search is paid for by its own locality scale.
Theorem 4.4 (Online bound in terms of scales). There is an absolute constant γ \gamma γ with the following property. For every n ≥ 2 n \ge2 n ≥ 2 , Algorithm 1 is a deterministic online BST algorithm on the keys 1 , … , n 1,\ldots,n 1 , … , n that serves every search t t t of every sequence X X X of searches at cost at most
γ ⋅ λ ( t ) . \gamma\cdot\lambda(t). γ ⋅ λ ( t ) . In particular, every search costs at most γ ⌈ log 2 n ⌉ \gamma\lceil\log_2 n\rceil γ ⌈ log 2 n ⌉ .
Kept blocks and regions
A BST always finds the searched key, and by Lemma 3.1 any sequence of operations on a BST is a legal execution, so Algorithm 1 needs no proof of correctness in the usual sense. Its steps, however, rely on facts about how the kept blocks and the regions change. The set F \mathcal{F} F must follow the kept blocks of Definition 4.2 , and there must be few of them. A retiring block must be a single region. A new block must lie inside one region. And the realization of a region tree must be a BST. This section proves these facts, and § 6 uses the same facts to bound the cost.
The kept blocks
The next lemma is what the rest of the paper uses about the kept blocks.
Lemma 5.1 (Kept blocks). Parts 1 and 2 hold for every time t ∈ { 1 , … , m + 1 } t \in\{1,\ldots,m+1\} t ∈ { 1 , … , m + 1 } , and Parts 3 to 5 for every search t t t .
Part 1 (count). For every s ∈ S s \in S s ∈ S , at most 2 s 2^s 2 s blocks of F t \mathcal{F}_t F t have height s s s .
Part 2 (nesting). If a block g ∈ F t g \in\mathcal{F}_t g ∈ F t has height s ∈ S s \in S s ∈ S , then for every promoted scale s ′ > s s^{\prime} > s s ′ > s the block of height s ′ s^{\prime} s ′ that contains g g g is in F t \mathcal{F}_t F t .
Part 3 (the blocks of x t x_t x t ). The missing scales of search t t t are the promoted scales below some bound, and each of them is less than λ ( t ) \lambda(t) λ ( t ) .
Part 4 (one search). The family F t + 1 \mathcal{F}_{t+1} F t + 1 arises from F t \mathcal{F}_t F t as follows. For every missing scale s s s of search t t t , the block B s ( x t ) B_s(x_t) B s ( x t ) is added, and if F t \mathcal{F}_t F t has 2 s 2^s 2 s blocks of height s s s , then the one among them that was searched least recently, the victim of the scale, is removed. Nothing else changes.
Part 5 (the steps of the algorithm). Suppose that F = F t \mathcal{F}=\mathcal{F}_t F = F t when Search ( t ) \operatorname{Search}(t) Search ( t ) begins. Then F = F t + 1 \mathcal{F}=\mathcal{F}_{t+1} F = F t + 1 when it ends. Every block that it retires or adds contains, at that moment, no block of F \mathcal{F} F of smaller height. And at every moment of the procedure, F \mathcal{F} F has the nesting property of Part 2 and at most 2 s 2^s 2 s blocks of height s s s for every s ∈ S s\in S s ∈ S .
The proof works with one number per block, its rank. Let g g g be a block of height s s s , and let ℓ t ( g ) \ell_t(g) ℓ t ( g ) be the last search in g g g before t t t . Then
R t ( g ) : = { B s ( x u ) : ℓ t ( g ) < u < t } , r t ( g ) : = ∣ R t ( g ) ∣ , R_t(g):=\{B_s(x_u):\ell_t(g)<u<t\},\qquad r_t(g):=\lvert R_t(g)\rvert, R t ( g ) := { B s ( x u ) : ℓ t ( g ) < u < t } , r t ( g ) := ∣ R t ( g )∣ ,
and r t ( g ) : = ∞ r_t(g):=\infty r t ( g ) := ∞ if g g g is not searched before t t t . So the rank r t ( g ) r_t(g) r t ( g ) is the number of blocks of height s s s that were searched since the last search in g g g . The most recently searched block of a height has the rank 0. This rank of a block has nothing to do with the ranks of the nodes of a biased search tree (§ 3.2 ), which do not occur in this section.
Proof. Each part is a short computation with ranks. At the height K ^ \widehat{K} K every claim holds trivially, because the top block is the only block there and is always kept. So let s ∈ S s\in S s ∈ S with s < K ^ s<\widehat{K} s < K throughout, and write F t s : = { g ∈ F t : h ( g ) = s } \mathcal{F}_t^s:=\{g\in\mathcal{F}_t:h(g)=s\} F t s := { g ∈ F t : h ( g ) = s } .
We first restate Definition 4.2 in terms of ranks. Let g ≠ g ′ g\ne g' g = g ′ be blocks of height s s s . Then ℓ t ( g ′ ) > ℓ t ( g ) \ell_t(g')>\ell_t(g) ℓ t ( g ′ ) > ℓ t ( g ) if and only if g ′ g' g ′ contains some x u x_u x u with ℓ t ( g ) < u < t \ell_t(g)<u<t ℓ t ( g ) < u < t , that is, if and only if g ′ ∈ R t ( g ) g'\in R_t(g) g ′ ∈ R t ( g ) . So r t ( g ) r_t(g) r t ( g ) is the number of blocks of height s s s that were searched more recently than g g g , and the blocks of height s s s that are searched before t t t have the ranks 0 , 1 , 2 , … 0,1,2,\ldots 0 , 1 , 2 , … , each rank once. Since the kept blocks are the 2 s 2^s 2 s most recently searched ones,
F t s = { g : h ( g ) = s , r t ( g ) < 2 s } . (2) \mathcal{F}_t^s=\{g:h(g)=s,\ r_t(g)<2^s\}. \tag*{(2)} F t s = { g : h ( g ) = s , r t ( g ) < 2 s } . (2)
Part 1. By (2) , the blocks of F t s \mathcal{F}_t^s F t s have distinct ranks in { 0 , … , 2 s − 1 } \{0,\ldots,2^s-1\} { 0 , … , 2 s − 1 } .
Part 2. We show that ranks do not increase upwards: if g ⊆ g ′ g\subseteq g' g ⊆ g ′ and h ( g ) = s < h ( g ′ ) = s ′ h(g)=s<h(g')=s' h ( g ) = s < h ( g ′ ) = s ′ , then
r t ( g ′ ) ≤ r t ( g ) . (3) r_t(g')\le r_t(g). \tag*{(3)} r t ( g ′ ) ≤ r t ( g ) . (3)
This is trivial if r t ( g ) = ∞ r_t(g)=\infty r t ( g ) = ∞ , so let g g g be searched before t t t . Let π \pi π map a block of height s s s to the block of height s ′ s' s ′ that contains it. Every search in g g g is a search in g ′ g' g ′ , so ℓ t ( g ) ≤ ℓ t ( g ′ ) \ell_t(g)\le\ell_t(g') ℓ t ( g ) ≤ ℓ t ( g ′ ) , and
R t ( g ′ ) = π ( { B s ( x u ) : ℓ t ( g ′ ) < u < t } ) ⊆ π ( R t ( g ) ) , so r t ( g ′ ) ≤ ∣ π ( R t ( g ) ) ∣ ≤ r t ( g ) . R_t(g')=\pi\bigl(\{B_s(x_u):\ell_t(g')<u<t\}\bigr)\subseteq\pi(R_t(g)),\qquad\text{so}\qquad r_t(g')\le\lvert\pi(R_t(g))\rvert\le r_t(g). R t ( g ′ ) = π ( { B s ( x u ) : ℓ t ( g ′ ) < u < t } ) ⊆ π ( R t ( g )) , so r t ( g ′ ) ≤ ∣ π ( R t ( g ))∣ ≤ r t ( g ) .
Now let g ∈ F t g\in\mathcal{F}_t g ∈ F t . If s ′ = K ^ s'=\widehat{K} s ′ = K , then g ′ g' g ′ is the top block, which is kept. Otherwise r t ( g ′ ) ≤ r t ( g ) < 2 s < 2 s ′ r_t(g')\le r_t(g)<2^s<2^{s'} r t ( g ′ ) ≤ r t ( g ) < 2 s < 2 s ′ by (3) and (2) , so g ′ ∈ F t g'\in\mathcal{F}_t g ′ ∈ F t by (2) .
Part 3. Write g s : = B s ( x t ) g_s:=B_s(x_t) g s := B s ( x t ) , so that s s s is missing if and only if r t ( g s ) ≥ 2 s r_t(g_s)\ge2^s r t ( g s ) ≥ 2 s . If s s s is not missing and s ′ > s s'>s s ′ > s is promoted, then s ′ s' s ′ is not missing by Part 2. So the missing scales are the promoted scales below some bound.
Let t ≥ 2 t\ge2 t ≥ 2 and s ≥ λ ( t ) s\ge\lambda(t) s ≥ λ ( t ) . By (1) , some search w < t w<t w < t lies in g s g_s g s and has ρ ( w , t ) ≤ 2 s − 2 \rho(w,t)\le2^s-2 ρ ( w , t ) ≤ 2 s − 2 , and ℓ t ( g s ) ≥ w \ell_t(g_s)\ge w ℓ t ( g s ) ≥ w . Hence
r t ( g s ) ≤ ( a ) ρ ( ℓ t ( g s ) , t ) ≤ ( b ) ρ ( w , t ) ≤ 2 s − 2 < 2 s , r_t(g_s)\underset{(a)}{\le}\rho(\ell_t(g_s),t)\underset{(b)}{\le}\rho(w,t)\le2^s-2<2^s, r t ( g s ) ( a ) ≤ ρ ( ℓ t ( g s ) , t ) ( b ) ≤ ρ ( w , t ) ≤ 2 s − 2 < 2 s ,
so s s s is not missing. Here (a) holds because the blocks of R t ( g s ) R_t(g_s) R t ( g s ) are disjoint and each contains a key searched between ℓ t ( g s ) \ell_t(g_s) ℓ t ( g s ) and t t t , and (b) because ρ ( ⋅ , t ) \rho(\cdot,t) ρ ( ⋅ , t ) does not increase. For t = 1 t=1 t = 1 , only the top block is kept, so the missing scales are the promoted scales below K ^ \widehat{K} K , and these are less than K = λ ( 1 ) K=\lambda(1) K = λ ( 1 ) : the largest of them is K ^ / 2 \widehat{K}/2 K /2 , and K ^ / 2 < K \widehat{K}/2<K K /2 < K because K ^ \widehat{K} K is the least power of two with K ^ ≥ K \widehat{K}\ge K K ≥ K .
Part 4. Let g : = g s g := g_s g := g s . Search t t t becomes the last search in g g g . For every other block b b b of height s s s , it adds the time t t t , whose block is g g g , to the range of u u u in R t ( b ) R_t(b) R t ( b ) . And g g g is already in R t ( b ) R_t(b) R t ( b ) exactly when g g g was searched more recently than b b b . So
R t + 1 ( g ) = ∅ , R t + 1 ( b ) = R t ( b ) ∪ { g } , g ∈ R t ( b ) ⟺ r t ( g ) < r t ( b ) . R_{t+1}(g) = \varnothing,\qquad R_{t+1}(b) = R_t(b) \cup\{g\},\qquad g \in R_t(b) \Longleftrightarrow r_t(g) < r_t(b). R t + 1 ( g ) = ∅ , R t + 1 ( b ) = R t ( b ) ∪ { g } , g ∈ R t ( b ) ⟺ r t ( g ) < r t ( b ) .
Taking sizes, with [ P ] ∈ { 0 , 1 } [P] \in\{0,1\} [ P ] ∈ { 0 , 1 } the truth value of P P P and ∞ + 1 = ∞ \infty+ 1 = \infty ∞ + 1 = ∞ ,
r t + 1 ( g ) = 0 , r t + 1 ( b ) = r t ( b ) + [ r t ( b ) < r t ( g ) ] for b ≠ g . (4) r_{t+1}(g) = 0,\qquad r_{t+1}(b) = r_t(b) + [r_t(b) < r_t(g)]\qquad\text{for } b \ne g. \tag*{(4)} r t + 1 ( g ) = 0 , r t + 1 ( b ) = r t ( b ) + [ r t ( b ) < r t ( g )] for b = g . (4)
So g ∈ F t + 1 g \in\mathcal{F}_{t+1} g ∈ F t + 1 , and by (2) , for b ≠ g b \ne g b = g of height s s s ,
b ∈ F t + 1 ⟺ r t ( b ) + [ r t ( b ) < r t ( g ) ] < 2 s . b \in\mathcal{F}_{t+1} \Longleftrightarrow r_t(b) + [r_t(b) < r_t(g)] < 2^s. b ∈ F t + 1 ⟺ r t ( b ) + [ r t ( b ) < r t ( g )] < 2 s .
If s s s is not missing, that is, r t ( g ) < 2 s r_t(g) < 2^s r t ( g ) < 2 s , the right side is equivalent to r t ( b ) < 2 s r_t(b) < 2^s r t ( b ) < 2 s , because r t ( b ) < r t ( g ) r_t(b) < r_t(g) r t ( b ) < r t ( g ) implies r t ( b ) + 1 ≤ r t ( g ) < 2 s r_t(b) + 1 \le r_t(g) < 2^s r t ( b ) + 1 ≤ r t ( g ) < 2 s . So F t + 1 s = F t s \mathcal{F}_{t+1}^s = \mathcal{F}_t^s F t + 1 s = F t s . If s s s is missing, that is, r t ( g ) ≥ 2 s r_t(g) \ge2^s r t ( g ) ≥ 2 s , then every b b b with r t ( b ) < 2 s r_t(b) < 2^s r t ( b ) < 2 s has r t ( b ) < r t ( g ) r_t(b) < r_t(g) r t ( b ) < r t ( g ) , and the right side is equivalent to r t ( b ) ≤ 2 s − 2 r_t(b) \le2^s - 2 r t ( b ) ≤ 2 s − 2 . So
F t + 1 s = ( F t s ∖ { e } ) ∪ { g } , e : = the block with r t ( e ) = 2 s − 1 , if there is one. \mathcal{F}_{t+1}^s = (\mathcal{F}_t^s \setminus\{e\}) \cup\{g\},\qquad e := \text{the block with }r_t(e) = 2^s - 1,\text{ if there is one.} F t + 1 s = ( F t s ∖ { e }) ∪ { g } , e := the block with r t ( e ) = 2 s − 1 , if there is one.
By Part 1, e e e exists if and only if ∣ F t s ∣ = 2 s \lvert\mathcal{F}_t^s\rvert= 2^s ∣ F t s ∣ = 2 s , and then it has the largest rank in F t s \mathcal{F}_t^s F t s : it is the kept block searched least recently, the victim.
For example, let s = 1 s = 1 s = 1 , so that two blocks of height 1 1 1 are kept, and let the blocks a , b , c a,b,c a , b , c have the ranks 0 , 1 , 2 0,1,2 0 , 1 , 2 , so that F t 1 = { a , b } \mathcal{F}_t^1 = \{a,b\} F t 1 = { a , b } . A search in c c c is missing at the scale 1 1 1 , and by (4) the ranks become c : 0 c:0 c : 0 , a : 1 a:1 a : 1 , b : 2 b:2 b : 2 . So c c c is added and the victim b b b , of rank 2 1 − 1 2^1 - 1 2 1 − 1 , is removed.
Part 5. Search t t t makes the changes of Part 4 one block at a time: the victims leave from the lowest to the highest, and then the new blocks arrive from the highest to the lowest. A victim of height 1 1 1 may lie inside the victim of height 2 2 2 , so it must leave first, and a new block of height 1 1 1 lies inside the new block of height 2 2 2 , so it must arrive second.
For a block b b b and a set G \mathcal{G} G of blocks, let
↑ b : = { B s ′ ( y ) : y ∈ b , s ′ ∈ S , s ′ > h ( b ) } , ↓ G b : = { c ∈ G : c ⊆ b , h ( c ) < h ( b ) } \uparrow b := \{B_{s'}(y): y \in b,\ s' \in S,\ s' > h(b)\},\qquad\downarrow_{\mathcal{G}} b := \{c \in\mathcal{G}: c \subseteq b,\ h(c) < h(b)\} ↑ b := { B s ′ ( y ) : y ∈ b , s ′ ∈ S , s ′ > h ( b )} , ↓ G b := { c ∈ G : c ⊆ b , h ( c ) < h ( b )}
be the blocks above b b b at the promoted scales and the blocks of G \mathcal{G} G below b b b . If h ( b ) ∈ S h(b) \in S h ( b ) ∈ S , then c ∈ ↓ G b c \in\downarrow_{\mathcal{G}} b c ∈ ↓ G b if and only if c ∈ G c \in\mathcal{G} c ∈ G and b ∈ ↑ c b \in\uparrow c b ∈↑ c . Call G \mathcal{G} G closed if ↑ b ⊆ G \uparrow b \subseteq\mathcal{G} ↑ b ⊆ G for every b ∈ G b \in\mathcal{G} b ∈ G ; this is the nesting property, so F t \mathcal{F}_t F t and F t + 1 \mathcal{F}_{t+1} F t + 1 are closed by Part 2. Part 5 claims that every intermediate family F \mathcal{F} F is closed with at most 2 s 2^s 2 s blocks of each height s s s , and that every block b b b that is retired or added has ↓ F b = ∅ \downarrow_{\mathcal{F}} b = \varnothing ↓ F b = ∅ .
The set M M M of line 12 consists of the missing scales, because F = F t \mathcal{F} = \mathcal{F}_t F = F t there. Let
V : = the victims of the scales in M , A : = { B s ( x t ) : s ∈ M } , U : = F t ∖ V V := \text{the victims of the scales in }M,\qquad A := \{B_s(x_t):s \in M\},\qquad U := \mathcal{F}_t \setminus V V := the victims of the scales in M , A := { B s ( x t ) : s ∈ M } , U := F t ∖ V
be the blocks that leave, the blocks that arrive, and the blocks that stay. Write V ≥ a V_{\ge a} V ≥ a , V < a V_{<a} V < a , A ≥ a A_{\ge a} A ≥ a and A > a A_{>a} A > a for the blocks of V V V or A A A whose height satisfies the condition. By Part 4, and since A ∩ F t = ∅ A \cap\mathcal{F}_t = \varnothing A ∩ F t = ∅ ,
F t + 1 = U ∪ A , U = F t ∩ F t + 1 is closed. (5) \mathcal{F}_{t+1} = U \cup A,\qquad U = \mathcal{F}_t \cap\mathcal{F}_{t+1}\text{ is closed.} \tag*{(5)} F t + 1 = U ∪ A , U = F t ∩ F t + 1 is closed. (5)
When the first phase reaches a scale s ∈ M s \in M s ∈ M , only lower blocks have been retired, so the blocks of height s s s in F \mathcal{F} F are those of F t \mathcal{F}_t F t , and lines 15–17 retire the victim of height s s s . The third phase adds A A A by decreasing height. So, with
F a ( 1 ) : = U ∪ V ≥ a = F t ∖ V < a , F a ( 3 ) : = U ∪ A ≥ a , \mathcal{F}_a^{(1)} := U \cup V_{\ge a} = \mathcal{F}_t \setminus V_{<a},\qquad\mathcal{F}_a^{(3)} := U \cup A_{\ge a}, F a ( 1 ) := U ∪ V ≥ a = F t ∖ V < a , F a ( 3 ) := U ∪ A ≥ a ,
the family F \mathcal{F} F runs through
first phase: F t = F 0 ( 1 ) → ⋯ → F s ( 1 ) → F s + 1 ( 1 ) → ⋯ → U , \mathcal{F}_t=\mathcal{F}^{(1)}_0\to\cdots\to\mathcal{F}^{(1)}_s\to\mathcal{F}^{(1)}_{s+1}\to\cdots\to U, F t = F 0 ( 1 ) → ⋯ → F s ( 1 ) → F s + 1 ( 1 ) → ⋯ → U ,
third phase: U → ⋯ → F s + 1 ( 3 ) → F s ( 3 ) → ⋯ → U ∪ A = F t + 1 , U\to\cdots\to\mathcal{F}^{(3)}_{s+1}\to\mathcal{F}^{(3)}_s\to\cdots\to U\cup A=\mathcal{F}_{t+1}, U → ⋯ → F s + 1 ( 3 ) → F s ( 3 ) → ⋯ → U ∪ A = F t + 1 , {#eq:6}
where the index a a a runs through all integers although the heights in M M M are powers of two: the blocks of V V V and A A A have their heights in M M M , so the family changes only when a a a passes a height s ∈ M s\in M s ∈ M . There the step F s ( 1 ) → F s + 1 ( 1 ) \mathcal{F}^{(1)}_s\to\mathcal{F}^{(1)}_{s+1} F s ( 1 ) → F s + 1 ( 1 ) retires the victim of height s s s , if the scale has one, and the step F s + 1 ( 3 ) → F s ( 3 ) \mathcal{F}^{(3)}_{s+1}\to\mathcal{F}^{(3)}_s F s + 1 ( 3 ) → F s ( 3 ) adds B s ( x t ) B_s(x_t) B s ( x t ) . Every F a ( 1 ) \mathcal{F}^{(1)}_a F a ( 1 ) lies in F t \mathcal{F}_t F t and every F a ( 3 ) \mathcal{F}^{(3)}_a F a ( 3 ) in F t + 1 \mathcal{F}_{t+1} F t + 1 , which gives the count by Part 1. The rest follows from two facts:
(V) e ∈ V ⟹ ↓ U e = ∅ , e\in V\Longrightarrow\downarrow_U e=\varnothing, e ∈ V ⟹ ↓ U e = ∅ , since c ∈ ↓ U e c\in\downarrow_U e c ∈ ↓ U e gives e ∈ ↑ c ⊆ F t + 1 e\in\uparrow c\subseteq\mathcal{F}_{t+1} e ∈↑ c ⊆ F t + 1 , but e ∉ F t + 1 e\notin\mathcal{F}_{t+1} e ∈ / F t + 1 ;
(A) b ∈ A ⟹ ↑ b ⊆ A ∪ U , b\in A\Longrightarrow\uparrow b\subseteq A\cup U, b ∈ A ⟹↑ b ⊆ A ∪ U , since B s ′ ( x t ) ∈ A B_{s'}(x_t)\in A B s ′ ( x t ) ∈ A if s ′ ∈ M s'\in M s ′ ∈ M , and B s ′ ( x t ) ∈ F t ∖ V B_{s'}(x_t)\in\mathcal{F}_t\setminus V B s ′ ( x t ) ∈ F t ∖ V if s ′ ∉ M s'\notin M s ′ ∈ / M .
In (V), ↑ c ⊆ F t + 1 \uparrow c\subseteq\mathcal{F}_{t+1} ↑ c ⊆ F t + 1 because c ∈ U ⊆ F t + 1 c\in U\subseteq\mathcal{F}_{t+1} c ∈ U ⊆ F t + 1 and F t + 1 \mathcal{F}_{t+1} F t + 1 is closed. In (A), B s ′ ( x t ) B_{s'}(x_t) B s ′ ( x t ) is not a victim because victims have heights in M M M .
The families in (6) are closed. Let b ∈ F a ( 1 ) b\in\mathcal{F}^{(1)}_a b ∈ F a ( 1 ) . Then ↑ b ⊆ F t \uparrow b\subseteq\mathcal{F}_t ↑ b ⊆ F t . If some e ∈ ↑ b e\in\uparrow b e ∈↑ b were in V < a V_{<a} V < a , then h ( b ) < h ( e ) < a h(b)<h(e)<a h ( b ) < h ( e ) < a , so b ∈ U b\in U b ∈ U and b ∈ ↓ U e b\in\downarrow_U e b ∈ ↓ U e , contrary to (V). So ↑ b ⊆ F t ∖ V < a = F a ( 1 ) \uparrow b\subseteq\mathcal{F}_t\setminus V_{<a}=\mathcal{F}^{(1)}_a ↑ b ⊆ F t ∖ V < a = F a ( 1 ) . Let b ∈ F a ( 3 ) b\in\mathcal{F}^{(3)}_a b ∈ F a ( 3 ) . If b ∈ U b\in U b ∈ U , then ↑ b ⊆ U \uparrow b\subseteq U ↑ b ⊆ U . If b ∈ A ≥ a b\in A_{\geq a} b ∈ A ≥ a , then ↑ b ⊆ U ∪ A > a ⊆ F a ( 3 ) \uparrow b\subseteq U\cup A_{>a}\subseteq\mathcal{F}^{(3)}_a ↑ b ⊆ U ∪ A > a ⊆ F a ( 3 ) by (A).
Finally, the victim e e e of height s s s is retired from F s ( 1 ) \mathcal{F}^{(1)}_s F s ( 1 ) , whose blocks of height less than s s s lie in U U U , so ↓ F s ( 1 ) e = ↓ U e = ∅ \downarrow_{\mathcal{F}^{(1)}_s}e=\downarrow_U e=\varnothing ↓ F s ( 1 ) e = ↓ U e = ∅ by (V). The block g = B s ( x t ) g=B_s(x_t) g = B s ( x t ) is added to F s + 1 ( 3 ) \mathcal{F}^{(3)}_{s+1} F s + 1 ( 3 ) , whose blocks of height less than s s s lie in U ⊆ F t U\subseteq\mathcal{F}_t U ⊆ F t . If c ∈ ↓ F t g c\in\downarrow_{\mathcal{F}_t}g c ∈ ↓ F t g , then g ∈ ↑ c ⊆ F t g\in\uparrow c\subseteq\mathcal{F}_t g ∈↑ c ⊆ F t , contrary to s ∈ M s\in M s ∈ M . So ↓ F s + 1 ( 3 ) g = ∅ \downarrow_{\mathcal{F}^{(3)}_{s+1}}g=\varnothing ↓ F s + 1 ( 3 ) g = ∅ .
Initially F \mathcal{F} F consists of the top block, which is F 1 \mathcal{F}_1 F 1 . So by Part 5 and induction over the searches, F = F t \mathcal{F}=\mathcal{F}_t F = F t when search t t t begins, for every t t t , and the conclusions of Part 5 hold throughout every execution of Algorithm 1. Three consequences are used below. First, the blocks that search t t t adds are the blocks of x t x_t x t at its missing scales, which are promoted scales below λ ( t ) \lambda(t) λ ( t ) by Part 3. Second, a victim of search t t t does not contain x t x_t x t : it is a block of height s s s in F t \mathcal{F}_t F t for a missing scale s s s , hence a block other than B s ( x t ) B_s(x_t) B s ( x t ) . So the owner of x t x_t x t is the serving block f t f_t f t from the beginning of the search to its third phase. Third, by the nesting property, the parent of a kept block g g g of height s < K ^ s<\hat K s < K ^ is the kept block of height 2 s 2s 2 s that contains g g g , at the next promoted scale. So, for every kept block g g g other than the top block, with parent p p p ,
h ( p ) = 2 h ( g ) . (6) h(p)=2h(g). \tag*{(6)} h ( p ) = 2 h ( g ) . (6)
How the regions change
The regions change only when one block is added or removed, and by Part 5 of Lemma 5.1 the block then contains no kept block of smaller height. The next lemma describes this change: read forwards it is ADD (Algorithm 3), and read backwards it is RETIRE (Algorithm 2).
Lemma 5.2 (Adding or removing a block). Let F \mathcal{F} F be a set of blocks that contains the top block, and let g g g be a block that is not in F \mathcal{F} F and contains no block of F \mathcal{F} F of smaller height. Let p p p be the block of least height in F \mathcal{F} F among those that contain g g g and are higher than g g g ; this is the parent of g g g in F ∪ { g } \mathcal{F}\cup\{g\} F ∪ { g } .
Part 1 (owners). In F \mathcal{F} F , every key of g g g has the owner p p p . In F ∪ { g } \mathcal{F}\cup\{g\} F ∪ { g } , every key of g g g has the owner g g g , and every other key has the same owner as in F \mathcal{F} F .
Part 2 (adding g g g ). The block g g g lies in one region Q Q Q of F \mathcal{F} F , which p p p owns. The regions of F ∪ { g } \mathcal{F} \cup\{g\} F ∪ { g } are those of F \mathcal{F} F , except that Q Q Q is replaced by g g g and by the at most two intervals of Q ∖ g Q \setminus g Q ∖ g , which p p p still owns.
Part 3 (removing g g g ). The keys of g g g form one region of F ∪ { g } \mathcal{F} \cup\{g\} F ∪ { g } . The regions of F \mathcal{F} F are those of F ∪ { g } \mathcal{F} \cup\{g\} F ∪ { g } , except that g g g and those of its two neighbouring regions that p p p owns are merged into one region, owned by p p p .
Part 4 (the two procedures). In every execution of Algorithm 1, the region Q Q Q in line 3 3 3 of Algorithm 3 and the list Q Q Q in line 3 3 3 of Algorithm 2 exist as stated there, every call of ChangeRegions replaces at most three consecutive regions by at most three regions with the same union, and after each call the leaves of the region tree are the regions of F \mathcal{F} F .
Proof. A region is a maximal interval on which the owner is constant, so all four parts follow from one formula for the owners, (8) below. For a set G \mathcal{G} G of blocks that contains the top block and a key z z z , the blocks that contain z z z form the chain B 0 ( z ) ⊆ B 1 ( z ) ⊆ ⋯ ⊆ B K ^ ( z ) B_0(z) \subseteq B_1(z) \subseteq\cdots\subseteq B_{\hat{K}}(z) B 0 ( z ) ⊆ B 1 ( z ) ⊆ ⋯ ⊆ B K ^ ( z ) , and the owner of z z z is its lowest block in G \mathcal{G} G :
own G ( z ) = B a ( z ) , a = min J G ( z ) , J G ( z ) : = { b : B b ( z ) ∈ G } . (7) \operatorname{own}_{\mathcal{G}}(z)=B_a(z),\qquad a=\min J_{\mathcal{G}}(z),\qquad J_{\mathcal{G}}(z):=\{b:B_b(z)\in\mathcal{G}\}. \tag*{(7)} own G ( z ) = B a ( z ) , a = min J G ( z ) , J G ( z ) := { b : B b ( z ) ∈ G } . (7)
Throughout, F ′ : = F ∪ { g } \mathcal{F}' := \mathcal{F} \cup\{g\} F ′ := F ∪ { g } , a : = h ( g ) a := h(g) a := h ( g ) , and g = { g − , … , g + } g=\{g^{-},\ldots,g^{+}\} g = { g − , … , g + } .
Part 1. Let z ∈ g z\in g z ∈ g . For b ≤ a b\leq a b ≤ a we have B b ( z ) ⊆ B a ( z ) = g B_b(z)\subseteq B_a(z)=g B b ( z ) ⊆ B a ( z ) = g . So B b ( z ) ∉ F B_b(z)\notin\mathcal{F} B b ( z ) ∈ / F for b < a b<a b < a , by the assumption that g g g contains no block of F \mathcal{F} F of smaller height, and B a ( z ) = g ∉ F B_a(z)=g\notin\mathcal{F} B a ( z ) = g ∈ / F . For b > a b>a b > a , the block B b ( z ) B_b(z) B b ( z ) is the block B b ( g ) B_b(g) B b ( g ) of height b b b that contains g g g , the same for all z ∈ g z\in g z ∈ g . Hence, for every z ∈ g z\in g z ∈ g ,
J F ( z ) = J : = { b > a : B b ( g ) ∈ F } , J F ′ ( z ) = J ∪ { a } . J_{\mathcal{F}}(z)=J:=\{b>a:B_b(g)\in\mathcal{F}\},\qquad J_{\mathcal{F}'}(z)=J\cup\{a\}. J F ( z ) = J := { b > a : B b ( g ) ∈ F } , J F ′ ( z ) = J ∪ { a } .
The set J J J contains K ^ \hat{K} K ^ , because the top block is in F \mathcal{F} F and g ∉ F g\notin\mathcal{F} g ∈ / F is not the top block. So B min J ( g ) B_{\min J}(g) B m i n J ( g ) is the block of least height in F \mathcal{F} F that contains g g g and is higher than g g g , which is p p p . For z ∉ g z\notin g z ∈ / g , the block g g g is not in the chain of z z z , so J F ′ ( z ) = J F ( z ) J_{\mathcal{F}'}(z)=J_{\mathcal{F}}(z) J F ′ ( z ) = J F ( z ) . With (7) ,
own F ( z ) = p for z ∈ g , own F ′ ( z ) = { g , z ∈ g , own F ( z ) , z ∉ g . (8) \operatorname{own}_{\mathcal{F}}(z)=p\quad\text{for }z\in g,\qquad
\operatorname{own}_{\mathcal{F}'}(z)=
\begin{cases}
g,&z\in g,\\
\operatorname{own}_{\mathcal{F}}(z),&z\notin g.
\end{cases} \tag*{(8)} own F ( z ) = p for z ∈ g , own F ′ ( z ) = { g , own F ( z ) , z ∈ g , z ∈ / g . (8)
Part 2. By (8) , own F \operatorname{own}_{\mathcal{F}} own F is constant on the interval g g g , so g g g lies in one region Q = { q − , … , q + } Q=\{q^{-},\ldots,q^{+}\} Q = { q − , … , q + } of F \mathcal{F} F , and p p p owns it. Cut Q Q Q at g g g :
Q = Q L ∪ g ∪ Q R , Q L : = { q − , … , g − − 1 } , Q R : = { g + + 1 , … , q + } . Q=Q_L\cup g\cup Q_R,\qquad Q_L:=\{q^{-},\ldots,g^{-}-1\},\qquad Q_R:=\{g^{+}+1,\ldots,q^{+}\}. Q = Q L ∪ g ∪ Q R , Q L := { q − , … , g − − 1 } , Q R := { g + + 1 , … , q + } .
By (8) , own F ′ \operatorname{own}_{\mathcal{F}'} own F ′ is p p p on Q L Q_L Q L , g g g on g g g , p p p on Q R Q_R Q R , and equal to own F \operatorname{own}_{\mathcal{F}} own F outside Q Q Q . The owner changes at each cut, because p ≠ g p\ne g p = g . It also changes at both ends of Q Q Q : the key q − − 1 q^{-}-1 q − − 1 , if it exists, has
own F ′ ( q − − 1 ) = own F ( q − − 1 ) ∉ { p , g } . \operatorname{own}_{\mathcal{F}'}(q^{-}-1)=\operatorname{own}_{\mathcal{F}}(q^{-}-1)\notin\{p,g\}. own F ′ ( q − − 1 ) = own F ( q − − 1 ) ∈ / { p , g } .
where ≠ p \ne p = p holds because Q Q Q is a maximal interval with owner p p p , and ≠ g \ne g = g because g ∉ F g\notin\mathcal{F} g ∈ / F ; and the same holds for q + + 1 q^{+}+1 q + + 1 . Hence the nonempty ones among Q L , g , Q R Q_L,g,Q_R Q L , g , Q R are regions of F ′ \mathcal{F}' F ′ . Every region of F \mathcal{F} F other than Q Q Q is a region of F ′ \mathcal{F}' F ′ as well: its keys keep their owner, and so does a key next to it, unless this key is q − q^{-} q − or q + q^{+} q + , whose owner in F ′ \mathcal{F}' F ′ is p p p or g g g and so is still different. Hence, with Reg ( G ) \operatorname{Reg}(\mathcal{G}) Reg ( G ) the set of the regions of G \mathcal{G} G ,
Reg ( F ′ ) = ( Reg ( F ) ∖ { Q } ) ∪ ( { Q L , g , Q R } ∖ { ∅ } ) . (9) \operatorname{Reg}(\mathcal{F}')=\left(\operatorname{Reg}(\mathcal{F})\setminus\{Q\}\right)\cup\left(\{Q_L,g,Q_R\}\setminus\{\varnothing\}\right). \tag*{(9)} Reg ( F ′ ) = ( Reg ( F ) ∖ { Q } ) ∪ ( { Q L , g , Q R } ∖ { ∅ } ) . (9)
Part 3. This is (9) read from right to left. By (9) , g ∈ Reg ( F ′ ) g \in\operatorname{Reg}(\mathcal{F}') g ∈ Reg ( F ′ ) . Its left neighbour in Reg ( F ′ ) \operatorname{Reg}(\mathcal{F}') Reg ( F ′ ) is Q L Q_L Q L if Q L ≠ ∅ Q_L \ne\varnothing Q L = ∅ , and otherwise the region of F \mathcal{F} F that ends at q ˉ − 1 \bar{q}-1 q ˉ − 1 , whose owner is not p p p by Part 2; similarly on the right. So
{ neighbours of g in Reg ( F ′ ) owned by p } = { Q L , Q R } ∖ { ∅ } , Q L ∪ g ∪ Q R = Q ∈ Reg ( F ) , \{\text{neighbours of }g\text{ in }\operatorname{Reg}(\mathcal{F}')\text{ owned by }p\}=\{Q_L,Q_R\}\setminus\{\varnothing\},\qquad Q_L\cup g\cup Q_R=Q\in\operatorname{Reg}(\mathcal{F}), { neighbours of g in Reg ( F ′ ) owned by p } = { Q L , Q R } ∖ { ∅ } , Q L ∪ g ∪ Q R = Q ∈ Reg ( F ) ,
and Reg ( F ) \operatorname{Reg}(\mathcal{F}) Reg ( F ) arises from Reg ( F ′ ) \operatorname{Reg}(\mathcal{F}') Reg ( F ′ ) by replacing g g g and these neighbours by their union Q Q Q , which p p p owns.
Part 4. We show by induction over the calls of ChangeRegions that the leaves of the region tree are Reg ( F ) \operatorname{Reg}(\mathcal{F}) Reg ( F ) . Initially both are { { 1 , … , n } } \{\{1,\ldots,n\}\} {{ 1 , … , n }} . Consider a call of Add( g ) (g) ( g ) on the current set F \mathcal{F} F . By Part 5 of Lemma 5.1, g g g contains no block of F \mathcal{F} F of smaller height, so Part 2 applies: the region Q ⊇ g Q\supseteq g Q ⊇ g of line 3 exists, and by (9) the regions of F ∪ { g } \mathcal{F}\cup\{g\} F ∪ { g } inside Q Q Q are the at most three sets { Q L , g , Q R } ∖ { ∅ } \{Q_L,g,Q_R\}\setminus\{\varnothing\} { Q L , g , Q R } ∖ { ∅ } , whose union is Q Q Q . This is the replacement of line 5. Now consider a call of Retire( g ) (g) ( g ) , and let F − : = F ∖ { g } \mathcal{F}^{-}:=\mathcal{F}\setminus\{g\} F − := F ∖ { g } . By Part 5 of Lemma 5.1, g g g contains no block of F \mathcal{F} F , hence of F − \mathcal{F}^{-} F − , of smaller height, so the lemma applies to F − \mathcal{F}^{-} F − and g g g , with p p p the parent of g g g in F \mathcal{F} F . By Part 3, the keys of g g g form one region of F \mathcal{F} F , the list Q Q Q of line 3 is g g g together with { Q L , Q R } ∖ { ∅ } \{Q_L,Q_R\}\setminus\{\varnothing\} { Q L , Q R } ∖ { ∅ } , and these at most three consecutive regions are replaced by their union, which is the only region of F − \mathcal{F}^{-} F − that is not a region of F \mathcal{F} F . In both cases a region that the call does not replace keeps its owner (Part 1), hence its height and its weight in the region tree.
The realized tree
It remains to check that the tree of Definition 4.3 is a search tree.
Lemma 5.3 (The realized tree is a BST). For every region tree T \mathcal{T} T with its inner trees, the realization T ( T ) \mathcal{T}(\mathcal{T}) T ( T ) is a BST on { 1 , … , n } \{1,\ldots,n\} { 1 , … , n } .
Proof. Every key occurs exactly once, as noted after Definition 4.3 . For the order of the keys, let v v v be an internal node of the expanded tree, with key y y y , so that the leaf of y y y is the last leaf in the left subtree of v v v . A node of T ( T ) \mathcal{T}(\mathcal{T}) T ( T ) in the left subtree of v v v is an internal node, because the last leaf lies in no left subtree, and its key is the key of a leaf of this subtree. This key is not y y y , which is the key of v v v , so it is smaller than y y y . A node in the right subtree of v v v is an internal node or the last leaf, and its key is the key of a leaf of the right subtree, so it is larger than y y y .
The cost of the algorithm
This section proves Theorem 4.4 . The budget is the one noted in the comments of Algorithm 1: retiring a block of height s s s costs O ( s + 1 ) O(s+1) O ( s + 1 ) , and the walk and the promotions of search t t t cost O ( λ ( t ) ) O(\lambda(t)) O ( λ ( t )) each. A search retires at most one block at each of its missing scales, which are promoted scales below λ ( t ) \lambda(t) λ ( t ) and sum to less than 2 λ ( t ) 2\lambda(t) 2 λ ( t ) , so search t t t costs O ( λ ( t ) ) O(\lambda(t)) O ( λ ( t )) . § 6.1 shows that a search is served by a block of height less than 2 λ ( t ) 2\lambda(t) 2 λ ( t ) , § 6.2 and § 6.3 that a change of regions costs a constant times the largest height of its regions, and § 6.4 adds up.
Searches are served low
The density of the promoted scales enters here: the promoted scale just above λ ( t ) \lambda(t) λ ( t ) is not missing, so the block of x t x_t x t at this scale is kept.
Lemma 6.1 (The serving block). For every search t t t , the serving block satisfies h ( f t ) ≤ 2 λ ( t ) − 1 h(f_t)\le2\lambda(t)-1 h ( f t ) ≤ 2 λ ( t ) − 1 .
Proof. Let s ^ \hat{s} s ^ be the least power of two with s ^ ≥ λ ( t ) \hat{s} \ge\lambda(t) s ^ ≥ λ ( t ) . We show that
h ( f t ) ≤ ( a ) s ^ ≤ ( b ) 2 λ ( t ) − 1. h(f_t) \overset{(a)}{\le} \hat{s} \overset{(b)}{\le} 2\lambda(t)-1. h ( f t ) ≤ ( a ) s ^ ≤ ( b ) 2 λ ( t ) − 1.
For (b), if s ^ ≥ 2 \hat{s} \ge2 s ^ ≥ 2 , then the power of two s ^ / 2 \hat{s}/2 s ^ /2 is less than λ ( t ) \lambda(t) λ ( t ) , so s ^ ≤ 2 ( λ ( t ) − 1 ) \hat{s} \le2(\lambda(t)-1) s ^ ≤ 2 ( λ ( t ) − 1 ) , and if s ^ = 1 \hat{s}=1 s ^ = 1 , then s ^ ≤ 2 λ ( t ) − 1 \hat{s} \le2\lambda(t)-1 s ^ ≤ 2 λ ( t ) − 1 because λ ( t ) ≥ 1 \lambda(t) \ge1 λ ( t ) ≥ 1 . For (a), s ^ ≤ K ^ \hat{s} \le\hat{K} s ^ ≤ K ^ because λ ( t ) ≤ K ≤ K ^ \lambda(t) \le K \le\hat{K} λ ( t ) ≤ K ≤ K ^ and K ^ \hat{K} K ^ is a power of two, so s ^ ∈ S \hat{s} \in S s ^ ∈ S , and s ^ \hat{s} s ^ is not missing for search t t t , because the missing scales are less than λ ( t ) \lambda(t) λ ( t ) (Part 3 of Lemma 5.1). So B s ^ ( x t ) ∈ F t B_{\hat{s}}(x_t) \in\mathcal{F}_t B s ^ ( x t ) ∈ F t . This block contains x t x_t x t , and f t = own F t ( x t ) f_t=\operatorname{own}_{\mathcal{F}_t}(x_t) f t = own F t ( x t ) is the lowest block of F t \mathcal{F}_t F t that does.
The missing scales of one search have a small sum, and with them the heights of the blocks that the search retires and adds. Let M t M_t M t be the set of the missing scales of search t t t , which is the set M M M of line 12 of Algorithm 1. They are promoted scales s < λ ( t ) s < \lambda(t) s < λ ( t ) , by Part 3 of Lemma 5.1. If there are promoted scales below λ ( t ) \lambda(t) λ ( t ) , let 2 i 2^i 2 i be the largest of them. Then there are i + 1 ≤ 2 i i+1 \le2^i i + 1 ≤ 2 i of them and they sum to 2 i + 1 − 1 2^{i+1}-1 2 i + 1 − 1 , so with 2 i ≤ λ ( t ) − 1 2^i \le\lambda(t)-1 2 i ≤ λ ( t ) − 1 ,
∑ s ∈ M t ( s + 1 ) ≤ ∑ s ∈ S , s < λ ( t ) ( s + 1 ) ≤ 3 ⋅ 2 i ≤ 3 λ ( t ) . (10) \sum_{s\in M_t}(s+1) \le\sum_{\substack{s\in S,\,s<\lambda(t)}}(s+1) \le3\cdot2^i \le3\lambda(t). \tag*{(10)} s ∈ M t ∑ ( s + 1 ) ≤ s ∈ S , s < λ ( t ) ∑ ( s + 1 ) ≤ 3 ⋅ 2 i ≤ 3 λ ( t ) . (10)
If no promoted scale is below λ ( t ) \lambda(t) λ ( t ) , then M t M_t M t is empty and (10) holds as well. Search t t t retires at most one block of each height s ∈ M t s \in M_t s ∈ M t and adds exactly one, and it retires and adds no other blocks (Parts 4 and 5 of Lemma 5.1).
Weights and depths
The bounds of § 3.2 are in terms of the total weight of a tree. This subsection shows that the total weight of the regions is bounded by a constant, and concludes that the keys of a region of height a a a have depth O ( a + 1 ) O(a+1) O ( a + 1 ) .
Lemma 6.2 (Total weight). At every moment of the execution, the total weight W W W of all regions of the current set F \mathcal{F} F is less than 2.
Proof. We charge every region to a kept block that is not higher than its owner, in such a way that no block is charged more than twice. Let F \mathcal{F} F be the set of the kept blocks at this moment, and write own : = own F \operatorname{own}:=\operatorname{own}_{\mathcal{F}} own := own F . A region begins where its owner begins, or just after the last key of a lower kept block. So for a region Q Q Q with first key z z z and owner f = own ( z ) f=\operatorname{own}(z) f = own ( z ) , let
φ ( Q ) : = { f , if z = min f , own ( z − 1 ) , if z > min f . \varphi(Q):=
\begin{cases}
f, & \text{if } z=\min f,\\
\operatorname{own}(z-1), & \text{if } z>\min f.
\end{cases} φ ( Q ) := { f , own ( z − 1 ) , if z = min f , if z > min f .
We show that
h ( φ ( Q ) ) ≤ h ( Q ) for every region Q , ∣ φ − 1 ( c ) ∣ ≤ 2 for every c ∈ F . (11) h(\varphi(Q))\le h(Q)\quad\text{for every region }Q,\qquad|\varphi^{-1}(c)|\le2\quad\text{for every }c\in\mathcal{F}. \tag*{(11)} h ( φ ( Q )) ≤ h ( Q ) for every region Q , ∣ φ − 1 ( c ) ∣ ≤ 2 for every c ∈ F . (11)
Let z > min f z>\min f z > min f , and let c : = own ( z − 1 ) c:=\operatorname{own}(z-1) c := own ( z − 1 ) . The interval f f f contains z − 1 z-1 z − 1 , and c ≠ f c\ne f c = f because Q Q Q is a maximal interval with the owner f f f . Both c c c and f f f are blocks of F \mathcal{F} F that contain z − 1 z-1 z − 1 , and c c c is the lowest of these, so h ( c ) < h ( f ) = h ( Q ) h(c)<h(f)=h(Q) h ( c ) < h ( f ) = h ( Q ) . Moreover z ∉ c z\notin c z ∈ / c , because f f f is the lowest block of F \mathcal{F} F that contains z z z . So
z − 1 = max c . z-1=\max c. z − 1 = max c .
Hence a block c ∈ F c \in\mathcal{F} c ∈ F is φ ( Q ) \varphi(Q) φ ( Q ) only for the region Q Q Q that contains min c \min c min c and for the region Q Q Q that begins at max c + 1 \max c + 1 max c + 1 , which proves (11) . Now
W = ∑ Q ∈ Reg ( F ) 4 − h ( Q ) ≤ ( a ) ∑ Q ∈ Reg ( F ) 4 − h ( φ ( Q ) ) ≤ ( b ) 2 ∑ c ∈ F 4 − h ( c ) ≤ ( c ) 2 ∑ s ∈ S 2 s ⋅ 4 − s ≤ 2 ∑ j ≥ 0 2 − 2 j < ( d ) 2 ⋅ 7 8 < 2. \begin{aligned}
W &= \sum_{Q \in\operatorname{Reg}(\mathcal{F})} 4^{-h(Q)}
\mathrel{\overset{(a)}{\le}} \sum_{Q \in\operatorname{Reg}(\mathcal{F})} 4^{-h(\varphi(Q))}
\mathrel{\overset{(b)}{\le}} 2\sum_{c \in\mathcal{F}} 4^{-h(c)} \\
&\mathrel{\overset{(c)}{\le}} 2\sum_{s \in S} 2^s \cdot4^{-s}
\le2\sum_{j \ge0} 2^{-2j}
\mathrel{\overset{(d)}{<}} 2 \cdot\frac{7}{8} < 2.
\end{aligned} W = Q ∈ Reg ( F ) ∑ 4 − h ( Q ) ≤ ( a ) Q ∈ Reg ( F ) ∑ 4 − h ( φ ( Q )) ≤ ( b ) 2 c ∈ F ∑ 4 − h ( c ) ≤ ( c ) 2 s ∈ S ∑ 2 s ⋅ 4 − s ≤ 2 j ≥ 0 ∑ 2 − 2 j < ( d ) 2 ⋅ 8 7 < 2.
Here (a) and (b) are the two parts of (11) , and (c) holds because at most 2 s 2^s 2 s blocks of F \mathcal{F} F have height s s s , for every s ∈ S s \in S s ∈ S (Part 5 of Lemma 5.1). For (d), the terms with j ≥ 3 j \ge3 j ≥ 3 sum to less than 2 ⋅ 2 − 8 2 \cdot2^{-8} 2 ⋅ 2 − 8 , so the sum is less than 1 2 + 1 4 + 1 16 + 1 128 < 7 8 \frac{1}{2}+\frac{1}{4}+\frac{1}{16}+\frac{1}{128}<\frac{7}{8} 2 1 + 4 1 + 16 1 + 128 1 < 8 7 .
Lemma 6.3 (Size and depth of a region). Let Q Q Q be a region of height a a a at some moment between two steps of Algorithm 1. Then Q Q Q has at most 2 a 2^a 2 a keys, and the leaf of every key of Q Q Q has depth O ( a + 1 ) O(a+1) O ( a + 1 ) in the expanded tree. In particular, the leaf of Q Q Q has depth O ( a + 1 ) O(a+1) O ( a + 1 ) in T \mathcal{T} T , and every key of Q Q Q has depth O ( a + 1 ) O(a+1) O ( a + 1 ) in T ( T ) T(\mathcal{T}) T ( T ) .
Proof. The region lies in its owner, a block of height a a a , so ∣ Q ∣ ≤ 2 a |Q| \le2^a ∣ Q ∣ ≤ 2 a . In the expanded tree, the path to the leaf of a key y ∈ Q y \in Q y ∈ Q goes through T \mathcal{T} T to the leaf of Q Q Q and then through the inner tree of Q Q Q . So by the depth bound of § 3.2 , applied to both trees, the leaf of y y y has depth
O ( 1 + log W w ( Q ) ) ⏟ in T + O ( 1 + log ∣ Q ∣ ) ⏟ in the inner tree ≤ O ( 1 + log ( 2 ⋅ 4 a ) ) + O ( 1 + log 2 a ) = O ( a + 1 ) . \underbrace{O\left(1+\log\frac{W}{w(Q)}\right)}_{\text{in }\mathcal{T}}+
\underbrace{O(1+\log|Q|)}_{\text{in the inner tree}}
\le O(1+\log(2\cdot4^a))+O(1+\log2^a)=O(a+1). in T O ( 1 + log w ( Q ) W ) + in the inner tree O ( 1 + log ∣ Q ∣ ) ≤ O ( 1 + log ( 2 ⋅ 4 a )) + O ( 1 + log 2 a ) = O ( a + 1 ) .
where W < 2 W<2 W < 2 by Lemma 6.2 and w ( Q ) = 4 − a w(Q)=4^{-a} w ( Q ) = 4 − a , and the inner tree has ∣ Q ∣ |Q| ∣ Q ∣ leaves of weight 1. The first term alone bounds the depth of the leaf of Q Q Q in T \mathcal{T} T . The statement about T ( T ) T(\mathcal{T}) T ( T ) follows, because the depth of a key in T ( T ) T(\mathcal{T}) T ( T ) is at most the depth of its leaf in the expanded tree (§ 4.3 ).
The cost of changing regions
Throughout, the tree is T ( T ) T(\mathcal{T}) T ( T ) for the current region tree, and each call of ChangeRegions is one operation in the sense of Lemma 3.1: its cost is the number of keys that change a child or are ancestors, in the tree before the call, of such a key.
Lemma 6.4 (Cost of changing regions). In every execution of Algorithm 1, consider a call of ChangeRegions (Algorithm 4) that removes the regions of the list Q Q Q and creates the regions of the list Q ′ Q' Q ′ . Let a a a be the largest height of these regions, where the height of a region of Q Q Q is taken when the procedure that makes the call begins, before it changes F \mathcal{F} F , and that of a region of Q ′ Q' Q ′ after the call. Then the call has cost O ( a + 1 ) O(a+1) O ( a + 1 ) .
Proof. The cost of the call counts keys of the BST T ( T ) T(\mathcal{T}) T ( T ) , while the bounds of § 3.2 count the nodes that the splits and joins visit in the region tree and in the inner trees. The two are related as follows. By Definition 4.3 , the key of an internal node of the expanded tree, and the children of this key in T ( T ) T(\mathcal{T}) T ( T ) , are determined by the subtree below the node: the key is that of the last leaf in the left subtree, and a child of the node in the expanded tree gives a child of this key in T ( T ) T(\mathcal{T}) T ( T ) exactly if it is an internal node or the leaf of the key n n n , which is then the last leaf of the subtree. So a node whose subtree the call leaves alone keeps its key, and this key keeps its children. For example, when ADD cuts a region Q Q Q into three, the splits and joins work near the path from the root to the leaf of Q Q Q . A node of T \mathcal{T} T away from this path has the same regions below it before and after the call, with the same inner trees, so nothing changes at its key.
Let E E E be the expanded tree when the call begins, and let V V V consist of the internal nodes of E E E that a split or a join of the call visits, together with the nodes of T \mathcal{T} T that lie above a leaf of Q Q Q . We show that
cost of the call ≤ ( a ) ∣ V ∣ ≤ ( b ) O ( time of the splits and joins ) + ∑ Q ∈ Q ( depth of the leaf of Q in T ) ≤ ( c ) O ( a + 1 ) . (12) \begin{aligned}
\text{cost of the call} &\mathrel{\overset{(a)}{\le}} |V| \mathrel{\overset{(b)}{\le}} O(\text{time of the splits and joins}) + \sum_{Q\in\mathcal{Q}} \bigl(\text{depth of the leaf of }Q\text{ in }\mathcal{T}\bigr) \\
&\mathrel{\overset{(c)}{\le}} O(a+1). \tag*{(12)}
\end{aligned} cost of the call ≤ ( a ) ∣ V ∣ ≤ ( b ) O ( time of the splits and joins ) + Q ∈ Q ∑ ( depth of the leaf of Q in T ) ≤ ( c ) O ( a + 1 ) . (12)
We prove the three steps in the order (b), (c), (a), because (a) uses the set V V V that (b) and (c) bound.
Step (b). We apply Lemma 3.2 with the starting trees T \mathcal{T} T and the inner trees of the regions of Q Q Q ; the single leaves of the new regions have no internal nodes. An operation on the region tree touches no record of an inner tree, and conversely. By Part 3 of the lemma, the first set in the definition of V V V has O ( time ) O(\text{time}) O ( time ) nodes, and the second set has at most as many nodes as the sum in (12) .
Step (c). The lists Q Q Q and Q ′ Q' Q ′ have at most three regions each. So the four lines of Algorithm 4 make at most 3 splits, 2 joins, 2 cuts and 4 joins, which are thirteen splits and joins in all, since a cut is a split and a join. Each takes O ( a + 1 ) O(a+1) O ( a + 1 ) time:
in the region tree (line 3 and line 6):
O ( 1 + log W w ) ≤ O ( 1 + log ( 2 ⋅ 4 a ) ) . O\left(1+\log\frac{W}{w}\right)\le O\left(1+\log(2\cdot4^a)\right). O ( 1 + log w W ) ≤ O ( 1 + log ( 2 ⋅ 4 a ) ) .
in the inner trees (line 4 and line 5):
O ( 1 + log N ) ≤ O ( 1 + log ( 3 ⋅ 2 a ) ) . O(1+\log N)\le O\left(1+\log(3\cdot2^a)\right). O ( 1 + log N ) ≤ O ( 1 + log ( 3 ⋅ 2 a ) ) .
In the first line, W < 2 W<2 W < 2 , because every tree in line 3 holds some of the leaves that T \mathcal{T} T has when the call begins, which are the regions at the beginning of the procedure that makes the call, and every tree in line 6 holds some of the regions after the call (Lemma 6.2 ). And w ≥ 4 − a w\ge4^{-a} w ≥ 4 − a . A split is at a leaf of Q Q Q . The joins of line 6 go from left to right: T 1 T_1 T 1 is joined with the first leaf of Q ′ Q' Q ′ , the result with the next leaf of Q ′ Q' Q ′ , and at the end with T 2 T_2 T 2 . So in every join, the last leaf of the first tree or the first leaf of the second tree is a region of Q ′ Q' Q ′ , and the bound for a join in § 3.2 has the sum of the weights of these two leaves in its denominator. If T 1 T_1 T 1 or T 2 T_2 T 2 is empty, its join is omitted. In the second line, the inner trees of the call have N ≤ 3 ⋅ 2 a N\le3\cdot2^a N ≤ 3 ⋅ 2 a leaves of weight 1 in all (Lemma 6.3 ). Finally, each of the at most three leaves of Q Q Q has depth O ( a + 1 ) O(a+1) O ( a + 1 ) in T \mathcal{T} T (Lemma 6.3 ).
Step (a). This step rests on two properties of V V V . First, V V V is a top set of E E E . Second, every internal node v ∉ V v\notin V v ∈ / V of E E E has the same subtree in the expanded tree after the call. For the first property, the nodes of V V V in T \mathcal{T} T form a top set of T \mathcal{T} T , because the visited ones do (Part 1 of Lemma 3.2) and the nodes above a leaf do. The nodes of V V V in the inner tree of a region Q ∈ Q Q\in\mathcal{Q} Q ∈ Q form a top set of this tree, by the same Part 1, and all nodes of T \mathcal{T} T above the leaf of Q Q Q are in V V V . For the second property, there are three cases.
v v v is a node of T \mathcal{T} T . Again v v v and the nodes of T \mathcal{T} T below it keep their children. The leaves of T \mathcal{T} T below v v v are regions outside Q Q Q , because v v v does not lie above a leaf of Q Q Q , and these regions keep their inner trees.
Now let y y y be a key that changes a child in T ( T ) T(\mathcal{T}) T ( T ) . It is not the key n n n , which is at a leaf before and after the call. So y y y is the key of an internal node v v v of E E E , and v ∈ V v \in V v ∈ V : otherwise v v v would keep its subtree by the second property, and with it its key y y y and the children of y y y . The ancestors of y y y in T ( T ) T(\mathcal{T}) T ( T ) are the keys of the nodes above v v v in E E E , which are in V V V by the first property. So every key that the cost counts is the key of a node of V V V , and different keys have different nodes.
The cost of a search
It remains to add up. The next lemma bounds the three kinds of steps of a search, and Theorem 4.4 follows from it.
Lemma 6.5 (Cost of the steps). In every execution of Algorithm 1,
Part 1. retiring a block of height s s s (line 17 17 17 ) costs O ( s + 1 ) O(s+1) O ( s + 1 ) ;
Part 2. the walk of search t t t (line 21 21 21 ) costs O ( λ ( t ) ) O(\lambda(t)) O ( λ ( t )) ;
Part 3. the promotions of search t t t (lines 23 23 23 –24 24 24 ) cost O ( λ ( t ) ) O(\lambda(t)) O ( λ ( t )) in all.
Proof. Each part combines Lemma 6.4 or the depth bound with the regions that the step changes. In a retirement or an addition of a block g g g with parent p p p , all regions that the call of ChangeRegions removes or creates are owned by g g g or by p p p (Parts 2 and 3 of Lemma 5.2). Their heights are at most h ( p ) h(p) h ( p ) , so by Lemma 6.4 , retiring or adding g g g costs O ( h ( p ) + 1 ) O(h(p)+1) O ( h ( p ) + 1 ) .
Retiring. Let g g g retire, with height s s s and parent p p p . Then h ( p ) = 2 s h(p)=2s h ( p ) = 2 s by (6) , so the cost is O ( 2 s + 1 ) = O ( s + 1 ) O(2s+1)=O(s+1) O ( 2 s + 1 ) = O ( s + 1 ) .
Serving. The first phase does not change the owner of x t x_t x t , as noted after Lemma 5.1. So when the search is served, the region of x t x_t x t has the height h ( f t ) h(f_t) h ( f t ) , and the walk costs one more than the depth of x t x_t x t , which is
O ( h ( f t ) + 1 ) ≤ O ( 2 λ ( t ) ) O(h(f_t)+1) \le O(2\lambda(t)) O ( h ( f t ) + 1 ) ≤ O ( 2 λ ( t ))
by Lemmas 6.1 and 6.3 .
Promoting. Let g 1 , … , g k g_1,\ldots,g_k g 1 , … , g k be the blocks that search t t t adds, in this order, with heights s 1 > ⋯ > s k s_1>\cdots>s_k s 1 > ⋯ > s k ; if the search adds no block, there is nothing to show. Let p i p_i p i be the parent of g i g_i g i when it is added. By Part 1 of Lemma 5.2, p i p_i p i is the owner that the keys of g i g_i g i , among them x t x_t x t , have just before g i g_i g i is added. Before the first addition this owner is the serving block. After the addition of g i − 1 g_{i-1} g i − 1 it is g i − 1 g_{i-1} g i − 1 , the lowest kept block that contains x t x_t x t at that moment, because the missing scales are consecutive promoted scales (Part 3 of Lemma 5.1) and the blocks of x t x_t x t at the lower ones are added later. So
p 1 = f t , p i = g i − 1 for 2 ≤ i ≤ k . p_1=f_t,\qquad p_i=g_{i-1}\quad\text{for }2\le i\le k. p 1 = f t , p i = g i − 1 for 2 ≤ i ≤ k .
The heights s 1 , … , s k s_1,\ldots,s_k s 1 , … , s k are the missing scales of the search. So the promotions cost
∑ i = 1 k O ( h ( p i ) + 1 ) = O ( h ( f t ) + 1 ) + ∑ i = 1 k − 1 O ( s i + 1 ) ≤ ( a ) O ( 2 λ ( t ) ) + ∑ s ∈ M t O ( s + 1 ) = ( b ) O ( λ ( t ) ) , \sum_{i=1}^{k} O(h(p_i)+1)
=O(h(f_t)+1)+\sum_{i=1}^{k-1}O(s_i+1)
\mathrel{\overset{(a)}{\le}} O(2\lambda(t))+\sum_{s\in M_t}O(s+1)
\mathrel{\overset{(b)}{=}}O(\lambda(t)), i = 1 ∑ k O ( h ( p i ) + 1 ) = O ( h ( f t ) + 1 ) + i = 1 ∑ k − 1 O ( s i + 1 ) ≤ ( a ) O ( 2 λ ( t )) + s ∈ M t ∑ O ( s + 1 ) = ( b ) O ( λ ( t )) ,
where (a) is Lemma 6.1 and (b) is (10) .
Proof of Theorem 4.4 . By Lemma 3.1, the cost of a search is at most the sum of the costs of its steps, where the walk is one step. The steps of search t t t are its retirements, its walk and its promotions. The search retires at most one block of each height s ∈ M t s \in M_t s ∈ M t and no other block, so by Lemma 6.5 and (10) its cost is at most
∑ s ∈ M t O ( s + 1 ) ⏟ retirements + O ( λ ( t ) ) ⏟ walk + O ( λ ( t ) ) ⏟ promotions = O ( λ ( t ) ) . \underbrace{\sum_{s\in M_t} O(s+1)}_{\text{retirements}}+\underbrace{O(\lambda(t))}_{\text{walk}}+\underbrace{O(\lambda(t))}_{\text{promotions}}=O(\lambda(t)). retirements s ∈ M t ∑ O ( s + 1 ) + walk O ( λ ( t )) + promotions O ( λ ( t )) = O ( λ ( t )) .
The constants do not depend on n n n , on X X X or on t t t , which gives the constant γ \gamma γ , and λ ( t ) ≤ K = ⌈ log 2 n ⌉ \lambda(t) \le K=\lceil\log_2 n\rceil λ ( t ) ≤ K = ⌈ log 2 n ⌉ . The algorithm is deterministic, and every step of search t t t uses only x 1 , … , x t x_1,\ldots,x_t x 1 , … , x t , so it is online.
From scales to the unified bound
This section proves Theorem 1.1 from Theorem 4.4 , by showing that ∑ t λ ( t ) ≤ 2 UB ( X ) + O ( m ) \sum_t\lambda(t)\le2\operatorname{UB}(X)+O(m) ∑ t λ ( t ) ≤ 2 UB ( X ) + O ( m ) . The locality scale measures the distance of two keys by the blocks of a grid that is fixed in advance, so it can exceed the term of a search in the unified bound by a factor of order log n \log n log n : two adjacent keys can lie on the two sides of a boundary between two high blocks. We show that in total the fixed grid loses only a factor 2 and O ( m ) O(m) O ( m ) (Lemma 7.2 ).
Throughout, X = ( x 1 , … , x m ) X=(x_1,\ldots,x_m) X = ( x 1 , … , x m ) is a sequence over { 1 , … , n } \{1,\ldots,n\} { 1 , … , n } with n ≥ 2 n\ge2 n ≥ 2 , and K K K , the blocks B a ( ⋅ ) B_a(\cdot) B a ( ⋅ ) and the locality scales λ ( t ) \lambda(t) λ ( t ) are as in § 4.1 . For t ≥ 2 t\ge2 t ≥ 2 and 0 ≤ J ≤ K 0\le J\le K 0 ≤ J ≤ K , let v J ( t ) v_J(t) v J ( t ) be the last search before t t t in the block of height J J J around x t x_t x t , that is, the latest time v < t v<t v < t with x v ∈ B J ( x t ) x_v\in B_J(x_t) x v ∈ B J ( x t ) , if there is one. By the remark after (1) , some search satisfies the condition of Definition 4.1 at the scale J J J exactly if v J ( t ) v_J(t) v J ( t ) exists and satisfies it, and by (1) this is the case exactly if J ≥ λ ( t ) J\ge\lambda(t) J ≥ λ ( t ) . So for every search t ≥ 2 t\ge2 t ≥ 2 and every J ∈ { 0 , … , K } J\in\{0,\ldots,K\} J ∈ { 0 , … , K } ,
λ ( t ) > J ⟺ v J ( t ) does not exist, or ρ ( v J ( t ) , t ) ≥ 2 J − 1. (13) \lambda(t)>J\quad\Longleftrightarrow\quad v_J(t)\text{ does not exist, or }\rho(v_J(t),t)\ge2^J-1. \tag*{(13)} λ ( t ) > J ⟺ v J ( t ) does not exist, or ρ ( v J ( t ) , t ) ≥ 2 J − 1. (13)
In words, λ ( t ) > J \lambda(t)>J λ ( t ) > J means that the block of height J J J around x t x_t x t was never searched before, or that at least 2 J − 1 2^J-1 2 J − 1 distinct keys were searched since the last search in it.
Write d ( u , t ) : = ∣ x t − x u ∣ d(u,t):=|x_t-x_u| d ( u , t ) := ∣ x t − x u ∣ for u < t u<t u < t and let
τ ( u , t ) : = log 2 ( d ( u , t ) + ρ ( u , t ) + 2 ) , τ t : = min u < t τ ( u , t ) ( t ≥ 2 ) , τ 1 : = log 2 ( n + 2 ) . \tau(u,t):=\log_2(d(u,t)+\rho(u,t)+2),\qquad\tau_t:=\min_{u<t}\tau(u,t)\ (t\ge2),\qquad\tau_1:=\log_2(n+2). τ ( u , t ) := log 2 ( d ( u , t ) + ρ ( u , t ) + 2 ) , τ t := u < t min τ ( u , t ) ( t ≥ 2 ) , τ 1 := log 2 ( n + 2 ) .
so that UB ( X ) = ∑ t ≥ 1 τ t \operatorname{UB}(X)=\sum_{t\ge1}\tau_t UB ( X ) = ∑ t ≥ 1 τ t and τ t ≥ 1 \tau_t\ge1 τ t ≥ 1 . For every t ≥ 2 t\ge2 t ≥ 2 we fix a time u t < t u_t<t u t < t that attains τ t \tau_t τ t and call it the partner of t t t ; any choice will do, and the same choice is used throughout.
The proof of the next lemma, and that of Lemma B.6 for insertions and deletions, charge a time t t t whose block was not visited for a long while to a chain of earlier times, obtained by following partners backwards from t t t . The following lemma contains what the two proofs have in common: the chain is long enough to pay for t t t , and it lies within the period in which the block of t t t was not visited. It uses only the numbers τ r \tau_r τ r , the partners, and the inequality ρ ( u , r ) ≤ 2 τ r − 2 \rho(u,r)\le2^{\tau_r}-2 ρ ( u , r ) ≤ 2 τ r − 2 between a time r r r and its partner u u u , which holds because τ r = τ ( u , r ) \tau_r=\tau(u,r) τ r = τ ( u , r ) .
Lemma 7.1 (Chains of partners). Let every time r r r have a number τ r ≥ 1 \tau_r\ge1 τ r ≥ 1 , and let some times r r r have a partner p ( r ) < r p(r)<r p ( r ) < r with ρ ( p ( r ) , r ) ≤ 2 τ r − 2 \rho(p(r),r)\le2^{\tau_r}-2 ρ ( p ( r ) , r ) ≤ 2 τ r − 2 . Let J J J and c ≥ 2 c\ge2 c ≥ 2 be integers, let t t t be a time with a partner and τ t ≤ J − c \tau_t\le J-c τ t ≤ J − c , and let t t t be a time with v = 0 v=0 v = 0 or ρ ( v , t ) ≥ 2 J − 1 \rho(v,t)\ge2^J-1 ρ ( v , t ) ≥ 2 J − 1 . Suppose that every time r r r with v < r < t v<r<t v < r < t and τ r < J − c \tau_r<J-c τ r < J − c has a partner. Let s 0 : = p ( t ) s_0:=p(t) s 0 := p ( t ) and s i + 1 : = p ( s i ) s_{i+1}:=p(s_i) s i + 1 := p ( s i ) , up to the least index i ∗ i^* i ∗ with
∑ i ≤ i ∗ 2 τ s i ≥ 2 J − c , \sum_{i\le i^*}2^{\tau_{s_i}}\ge2^{J-c}, i ≤ i ∗ ∑ 2 τ s i ≥ 2 J − c ,
and let P ( s i ) : = 2 τ t + ∑ i ′ < i 2 τ s i ′ P(s_i) := 2^{\tau_t} + \sum_{i' < i} 2^{\tau_{s_{i'}}} P ( s i ) := 2 τ t + ∑ i ′ < i 2 τ s i ′ . Then the times s 0 > s 1 > ⋯ > s i ∗ s_0 > s_1 > \cdots> s_{i^*} s 0 > s 1 > ⋯ > s i ∗ are defined and later than v v v , every s s s among them satisfies
ρ ( s , t ) < P ( s ) < 2 J − c + 1 , (14) \rho(s,t) < P(s) < 2^{J-c+1}, \tag*{(14)} ρ ( s , t ) < P ( s ) < 2 J − c + 1 , (14)
and ∑ i ≤ i ∗ min ( 1 , 2 τ s i + c − J ) ≥ 1 \sum_{i \le i^*} \min(1,2^{\tau_{s_i}+c-J}) \ge1 ∑ i ≤ i ∗ min ( 1 , 2 τ s i + c − J ) ≥ 1 .
Proof. The last claim is the choice of i ∗ i^* i ∗ . For the others we show by induction on i ≤ i ∗ i \le i^* i ≤ i ∗ that s i s_i s i is defined, later than v v v , and satisfies (14) . For i = 0 i=0 i = 0 , the partner s 0 s_0 s 0 of t t t is defined, and ρ ( s 0 , t ) ≤ 2 τ t − 2 < 2 τ t = P ( s 0 ) ≤ 2 J − c \rho(s_0,t) \le2^{\tau_t}-2 < 2^{\tau_t}=P(s_0) \le2^{J-c} ρ ( s 0 , t ) ≤ 2 τ t − 2 < 2 τ t = P ( s 0 ) ≤ 2 J − c . It is later than v v v : this is clear if v = 0 v=0 v = 0 , and if v ≥ 1 v \ge1 v ≥ 1 and s 0 ≤ v s_0 \le v s 0 ≤ v , then ρ ( v , t ) ≤ ρ ( s 0 , t ) < 2 J − c < 2 J − 1 \rho(v,t) \le\rho(s_0,t) < 2^{J-c} < 2^J-1 ρ ( v , t ) ≤ ρ ( s 0 , t ) < 2 J − c < 2 J − 1 , because ρ ( ⋅ , t ) \rho(\cdot,t) ρ ( ⋅ , t ) does not increase when its first argument increases, contrary to the choice of v v v . Let i < i ∗ i<i^* i < i ∗ . Then ∑ i ′ ≤ i 2 τ s i ′ < 2 J − c \sum_{i'\le i}2^{\tau_{s_{i'}}}<2^{J-c} ∑ i ′ ≤ i 2 τ s i ′ < 2 J − c , so τ s i < J − c \tau_{s_i}<J-c τ s i < J − c , and v < s i < t v<s_i<t v < s i < t , so s i s_i s i has a partner s i + 1 s_{i+1} s i + 1 . Every time strictly between s i + 1 s_{i+1} s i + 1 and t t t lies in one of the pieces ( s i + 1 , s i ) (s_{i+1},s_i) ( s i + 1 , s i ) , { s i } \{s_i\} { s i } , ( s i , s i − 1 ) (s_i,s_{i-1}) ( s i , s i − 1 ) , … \ldots … , { s 0 } \{s_0\} { s 0 } , ( s 0 , t ) (s_0,t) ( s 0 , t ) . The piece ( s i ′ + 1 , s i ′ ) (s_{i'+1},s_{i'}) ( s i ′ + 1 , s i ′ ) holds at most 2 τ s i ′ − 2 2^{\tau_{s_{i'}}}-2 2 τ s i ′ − 2 distinct keys, the piece ( s 0 , t ) (s_0,t) ( s 0 , t ) at most 2 τ t − 2 2^{\tau_t}-2 2 τ t − 2 , and the i + 1 i+1 i + 1 singletons hold i + 1 i+1 i + 1 keys, which the terms − 2 -2 − 2 absorb. Hence
ρ ( s i + 1 , t ) < 2 τ t + ∑ i ′ ≤ i 2 τ s i ′ = P ( s i + 1 ) < 2 J − c + 2 J − c = 2 J − c + 1 . \rho(s_{i+1},t) < 2^{\tau_t}+\sum_{i'\le i}2^{\tau_{s_{i'}}}=P(s_{i+1})<2^{J-c}+2^{J-c}=2^{J-c+1}. ρ ( s i + 1 , t ) < 2 τ t + i ′ ≤ i ∑ 2 τ s i ′ = P ( s i + 1 ) < 2 J − c + 2 J − c = 2 J − c + 1 .
If v ≥ 1 v\ge1 v ≥ 1 and s i + 1 ≤ v s_{i+1}\le v s i + 1 ≤ v , then ρ ( v , t ) ≤ ρ ( s i + 1 , t ) < 2 J − 1 ≤ 2 J − 1 \rho(v,t)\le\rho(s_{i+1},t)<2^{J-1}\le2^J-1 ρ ( v , t ) ≤ ρ ( s i + 1 , t ) < 2 J − 1 ≤ 2 J − 1 as for i = 0 i=0 i = 0 , which contradicts the choice of v v v . So s i + 1 > v s_{i+1}>v s i + 1 > v . Finally, the times s i s_i s i decrease and stay later than v ≥ 0 v\ge0 v ≥ 0 , so the sum reaches 2 J − c 2^{J-c} 2 J − c after finitely many steps, and i ∗ i^* i ∗ exists.
Lemma 7.2 (Block boundaries cost little in total). For every sequence X X X ,
∑ t = 2 m λ ( t ) ≤ 2 U B ( X ) + 4 m . \sum_{t=2}^{m}\lambda(t)\le2\mathrm{UB}(X)+4m. t = 2 ∑ m λ ( t ) ≤ 2 UB ( X ) + 4 m .
Proof. We count level by level; in this proof a level is a height J J J of blocks. Since 0 ≤ λ ( t ) ≤ K 0\le\lambda(t)\le K 0 ≤ λ ( t ) ≤ K ,
∑ t = 2 m λ ( t ) = ∑ J = 0 K − 1 # { t ≥ 2 : λ ( t ) > J } . (15) \sum_{t=2}^{m}\lambda(t)=\sum_{J=0}^{K-1}\#\{t\ge2:\lambda(t)>J\}. \tag*{(15)} t = 2 ∑ m λ ( t ) = J = 0 ∑ K − 1 # { t ≥ 2 : λ ( t ) > J } . (15)
At each level J J J , a search with λ ( t ) > J \lambda(t)>J λ ( t ) > J either has τ t > J − 2 \tau_t>J-2 τ t > J − 2 and pays for itself, or it is charged to earlier searches in such a way that every search receives at most one charge per level. The sum over the levels of what a search s s s receives is then τ s + O ( 1 ) \tau_s+O(1) τ s + O ( 1 ) . Figure 4 in § 2.5 shows the second case. Fix a level J J J and a search t ≥ 2 t\ge2 t ≥ 2 with λ ( t ) > J \lambda(t)>J λ ( t ) > J . By (13) , either v J ( t ) v_J(t) v J ( t ) does not exist or ρ ( v J ( t ) , t ) ≥ 2 J − 1 \rho(v_J(t),t)\ge2^J-1 ρ ( v J ( t ) , t ) ≥ 2 J − 1 . A search t t t has τ t > J − 2 \tau_t>J-2 τ t > J − 2 at the levels J J J with 0 ≤ J < τ t + 2 0\le J<\tau_t+2 0 ≤ J < τ t + 2 , which are at most τ t + 3 \tau_t+3 τ t + 3 levels, so these searches contribute at most ∑ t ≥ 2 ( τ t + 3 ) \sum_{t\ge2}(\tau_t+3) ∑ t ≥ 2 ( τ t + 3 ) to (15) . From now on let τ t ≤ J − 2 \tau_t\le J-2 τ t ≤ J − 2 ; we call t t t a crossing of level J J J . Its partner u : = u t u:=u_t u := u t satisfies
d ( u , t ) + ρ ( u , t ) + 2 = 2 τ t ≤ 2 J − 2 . (16) d(u,t)+\rho(u,t)+2=2^{\tau_t}\le2^{J-2}. \tag*{(16)} d ( u , t ) + ρ ( u , t ) + 2 = 2 τ t ≤ 2 J − 2 . (16)
The chain of a crossing. We apply Lemma 7.1 with c = 2 c=2 c = 2 , the partners u r u_r u r , and v : = v J ( t ) v:=v_J(t) v := v J ( t ) , or v : = 0 v:=0 v := 0 if v J ( t ) v_J(t) v J ( t ) does not exist. Its hypotheses hold: ρ ( v , t ) ≥ 2 J − 1 \rho(v,t)\ge2^J-1 ρ ( v , t ) ≥ 2 J − 1 by (13) if v ≥ 1 v\ge1 v ≥ 1 , and a time r r r with τ r < J − 2 \tau_r<J-2 τ r < J − 2 is not the first search, whose term log 2 ( n + 2 ) \log_2(n+2) log 2 ( n + 2 ) exceeds K − 1 ≥ J K-1\ge J K − 1 ≥ J , so it has a partner. We call the times s 0 = u , s 1 , … , s i ∗ s_0=u,s_1,\ldots,s_{i^*} s 0 = u , s 1 , … , s i ∗ of the lemma the chain of the crossing ; in the rest of the proof the letters s s s and s i s_i s i denote searches, not scales. The chain lies after v J ( t ) v_J(t) v J ( t ) , if that exists, and no search after v J ( t ) v_J(t) v J ( t ) and before t t t has its key in B J ( x t ) B_J(x_t) B J ( x t ) , no search before t t t at all if v J ( t ) v_J(t) v J ( t ) does not exist. So x u ∉ B J ( x t ) x_u \notin B_J(x_t) x u ∈ / B J ( x t ) , and since d ( u , t ) < 2 J − 2 d(u,t) < 2^{J-2} d ( u , t ) < 2 J − 2 , the keys x u x_u x u and x t x_t x t lie in two consecutive blocks of height J J J . Let β \beta β be the largest key of the lower one, a multiple of 2 J 2^J 2 J ; we call β \beta β the boundary of the crossing. Moreover d ( u , t ) ≥ 1 d(u,t) \ge1 d ( u , t ) ≥ 1 , so (16) forces J ≥ 4 J \ge4 J ≥ 4 . The chain stays near β \beta β , because it reaches a search s i s_i s i from β \beta β by short hops: from β \beta β to x u = x s 0 x_u = x_{s_0} x u = x s 0 , and from each search of the chain to its partner. For every s = s i s=s_i s = s i in the chain,
∣ x s − β ∣ ≤ ∣ x s 0 − β ∣ + ∑ i ′ < i d ( s i ′ + 1 , s i ′ ) < ( a ) 2 τ t + ∑ i ′ < i 2 τ s i ′ = P ( s ) ≤ ( b ) 2 J − 1 . (17) |x_s-\beta| \le|x_{s_0}-\beta|+\sum_{i'<i}d(s_{i'+1},s_{i'}) \overset{(a)}{<} 2^{\tau_t}+\sum_{i'<i}2^{\tau_{s_{i'}}}=P(s) \overset{(b)}{\le} 2^{J-1}. \tag*{(17)} ∣ x s − β ∣ ≤ ∣ x s 0 − β ∣ + i ′ < i ∑ d ( s i ′ + 1 , s i ′ ) < ( a ) 2 τ t + i ′ < i ∑ 2 τ s i ′ = P ( s ) ≤ ( b ) 2 J − 1 . (17)
Here (a) holds because β \beta β lies between x u x_u x u and x t x_t x t , so that ∣ x u − β ∣ ≤ d ( u , t ) < 2 τ t |x_u-\beta| \le d(u,t) < 2^{\tau_t} ∣ x u − β ∣ ≤ d ( u , t ) < 2 τ t by (16) , and because d ( s i ′ + 1 , s i ′ ) < 2 τ s i ′ d(s_{i'+1},s_{i'}) < 2^{\tau_{s_{i'}}} d ( s i ′ + 1 , s i ′ ) < 2 τ s i ′ for the partner s i ′ + 1 s_{i'+1} s i ′ + 1 of s i ′ s_{i'} s i ′ ; and (b) is (14) with c = 2 c=2 c = 2 .
Charging. We charge the crossing to the searches of its chain: the search s i s_i s i is charged min ( 1 , 2 τ s i − J + 2 ) \min(1,2^{\tau_{s_i}-J+2}) min ( 1 , 2 τ s i − J + 2 ) . By Lemma 7.1, these charges sum to at least 1.
A search is on one chain per level. Let t 1 < t 2 t_1<t_2 t 1 < t 2 be crossings of the same level J J J , with boundaries β 1 \beta_1 β 1 and β 2 \beta_2 β 2 , and suppose that a search s s s is on both chains. By (17) ,
∣ β 1 − β 2 ∣ ≤ ∣ β 1 − x s ∣ + ∣ x s − β 2 ∣ < 2 J − 1 + 2 J − 1 = 2 J . |\beta_1-\beta_2| \le|\beta_1-x_s|+|x_s-\beta_2| < 2^{J-1}+2^{J-1}=2^J. ∣ β 1 − β 2 ∣ ≤ ∣ β 1 − x s ∣ + ∣ x s − β 2 ∣ < 2 J − 1 + 2 J − 1 = 2 J .
and both are multiples of 2 J 2^J 2 J , so β 1 = β 2 \beta_1=\beta_2 β 1 = β 2 . The keys of t 1 t_1 t 1 and of its partner u t 1 u_{t_1} u t 1 lie in the two blocks of height J J J next to this boundary, one on each side, and B J ( x t 2 ) B_J(x_{t_2}) B J ( x t 2 ) is one of these two blocks. So one of the two searches t 1 t_1 t 1 and u t 1 u_{t_1} u t 1 has its key in B J ( x t 2 ) B_J(x_{t_2}) B J ( x t 2 ) ; let t ′ t' t ′ be its time. Then t ′ ≤ t 1 < t 2 t'\le t_1<t_2 t ′ ≤ t 1 < t 2 , so v J ( t 2 ) v_J(t_2) v J ( t 2 ) exists and is at least t ′ t' t ′ . The chain of t 1 t_1 t 1 starts at u t 1 u_{t_1} u t 1 and goes backwards, so s ≤ u t 1 ≤ t ′ s\le u_{t_1}\le t' s ≤ u t 1 ≤ t ′ , and the chain of t 2 t_2 t 2 stays after v J ( t 2 ) v_J(t_2) v J ( t 2 ) . Hence
v J ( t 2 ) < s ≤ u t 1 ≤ t ′ ≤ v J ( t 2 ) , v_J(t_2)<s\le u_{t_1}\le t'\le v_J(t_2), v J ( t 2 ) < s ≤ u t 1 ≤ t ′ ≤ v J ( t 2 ) ,
a contradiction.
Charge received. Fix a search s s s . At each level it is on the chain of at most one crossing, and crossings have level at least 4. At a level J J J it receives at most min ( 1 , 2 τ s + 2 − J ) \min(1,2^{\tau_s+2-J}) min ( 1 , 2 τ s + 2 − J ) . So in total it receives at most
∑ J ≥ 4 min ( 1 , 2 τ s + 2 − J ) ≤ # { J : 4 ≤ J ≤ τ s + 2 } ⏟ ≤ τ s − 1 + ∑ J > τ s + 2 2 τ s + 2 − J ⏟ < 2 < τ s + 1. \sum_{J\ge4}\min(1,2^{\tau_s+2-J})
\le\underbrace{\#\{J:4\le J\le\tau_s+2\}}_{\le\tau_s-1}
+\underbrace{\sum_{J>\tau_s+2}2^{\tau_s+2-J}}_{<2}
<\tau_s+1. J ≥ 4 ∑ min ( 1 , 2 τ s + 2 − J ) ≤ ≤ τ s − 1 # { J : 4 ≤ J ≤ τ s + 2 } + < 2 J > τ s + 2 ∑ 2 τ s + 2 − J < τ s + 1.
The second sum is less than 2 because its first term is less than 1 and each term is half the previous one. So the crossings contribute at most ∑ s ≥ 1 ( τ s + 1 ) \sum_{s\ge1}(\tau_s+1) ∑ s ≥ 1 ( τ s + 1 ) to (15) .
Summing up. By (15) ,
∑ t ≥ 2 λ ( t ) ≤ ∑ t ≥ 2 ( τ t + 3 ) + ∑ s ≥ 1 ( τ s + 1 ) ≤ 2 UB ( X ) + 4 m . \sum_{t\ge2}\lambda(t)\le\sum_{t\ge2}(\tau_t+3)+\sum_{s\ge1}(\tau_s+1)\le2\operatorname{UB}(X)+4m. t ≥ 2 ∑ λ ( t ) ≤ t ≥ 2 ∑ ( τ t + 3 ) + s ≥ 1 ∑ ( τ s + 1 ) ≤ 2 UB ( X ) + 4 m .
Theorem 7.3 (Locality scales and the unified bound). For every sequence X X X of m m m searches over { 1 , … , n } \{1,\ldots,n\} { 1 , … , n } , n ≥ 2 n\ge2 n ≥ 2 ,
UB ( X ) − m ≤ ∑ t = 1 m λ ( t ) ≤ 2 UB ( X ) + 4 m + K . \operatorname{UB}(X)-m\le\sum_{t=1}^{m}\lambda(t)\le2\operatorname{UB}(X)+4m+K. UB ( X ) − m ≤ t = 1 ∑ m λ ( t ) ≤ 2 UB ( X ) + 4 m + K . Consequently, Algorithm 1 serves X X X at total cost O ( m + UB ( X ) ) O(m+\operatorname{UB}(X)) O ( m + UB ( X )) .
Proof. The upper bound is Lemma 7.2 together with λ ( 1 ) = K \lambda(1)=K λ ( 1 ) = K .
For the lower bound we show that τ t ≤ λ ( t ) + 1 \tau_t\le\lambda(t)+1 τ t ≤ λ ( t ) + 1 for every t t t . Let t ≥ 2 t\ge2 t ≥ 2 , and let u < t u<t u < t be a search that satisfies the condition of Definition 4.1 at the scale λ ( t ) \lambda(t) λ ( t ) . The keys x u x_u x u and x t x_t x t lie in one block of at most 2 λ ( t ) 2^{\lambda(t)} 2 λ ( t ) keys, so d ( u , t ) ≤ 2 λ ( t ) − 1 d(u,t)\le2^{\lambda(t)}-1 d ( u , t ) ≤ 2 λ ( t ) − 1 , and ρ ( u , t ) + 2 ≤ 2 λ ( t ) \rho(u,t)+2\le2^{\lambda(t)} ρ ( u , t ) + 2 ≤ 2 λ ( t ) . Thus
τ t ≤ τ ( u , t ) ≤ log 2 ( 2 λ ( t ) − 1 + 2 λ ( t ) ) < λ ( t ) + 1. \tau_t\le\tau(u,t)\le\log_2(2^{\lambda(t)}-1+2^{\lambda(t)})<\lambda(t)+1. τ t ≤ τ ( u , t ) ≤ log 2 ( 2 λ ( t ) − 1 + 2 λ ( t ) ) < λ ( t ) + 1.
For the first search, τ 1 = log 2 ( n + 2 ) ≤ log 2 ( 2 K + 2 ) ≤ K + 1 = λ ( 1 ) + 1 \tau_1 = \log_2(n + 2) \le\log_2(2^K + 2) \le K + 1 = \lambda(1) + 1 τ 1 = log 2 ( n + 2 ) ≤ log 2 ( 2 K + 2 ) ≤ K + 1 = λ ( 1 ) + 1 , because K ≥ 1 K \ge1 K ≥ 1 .
For the total cost, Algorithm 1 serves search t t t at cost at most γ λ ( t ) \gamma\lambda(t) γ λ ( t ) by Theorem 4.4 , so by the upper bound it serves X X X at cost at most γ ( 2 UB ( X ) + 4 m + K ) \gamma(2\operatorname{UB}(X) + 4m + K) γ ( 2 UB ( X ) + 4 m + K ) . This is O ( m + UB ( X ) ) O(m + \operatorname{UB}(X)) O ( m + UB ( X )) , because K < log 2 n + 1 < UB ( X ) + 1 K < \log_2 n + 1 < \operatorname{UB}(X) + 1 K < log 2 n + 1 < UB ( X ) + 1 .
Together with the bound of γ ⌈ log 2 n ⌉ \gamma\lceil\log_2 n\rceil γ ⌈ log 2 n ⌉ on the cost of a single search in Theorem 4.4 , this is Theorem 1.1 .
The factor 2 of Lemma 7.2 cannot be improved for the fixed grid. Take 2 j 2^j 2 j pairs of adjacent keys, each pair on the two sides of a different boundary between two blocks of height j + 2 j + 2 j + 2 , and search the 2 j + 1 2^{j+1} 2 j + 1 keys round robin, pair by pair. After the first round, the first search of a pair has the term log 2 ( 2 j + 1 + 1 ) \log_2(2^{j+1} + 1) log 2 ( 2 j + 1 + 1 ) and the second the term log 2 3 \log_2 3 log 2 3 , while both have the locality scale j + 2 j + 2 j + 2 . So as j j j grows, the locality scales sum to about twice the unified bound.
Remark 7.4 (Two forms of the unified bound). Iacono’s definition of the unified bound, in the form of [1 ] , minimizes over keys: its term for search t t t is min y log 2 ( ∣ x t − y ∣ + w t ( y ) + 2 ) \min_y \log_2(|x_t - y| + w_t(y) + 2) min y log 2 ( ∣ x t − y ∣ + w t ( y ) + 2 ) , where w t ( y ) w_t(y) w t ( y ) is the number of distinct keys searched since the last search for y y y ; for a key y y y that was never searched, a case that [1 ] leaves open, we take w t ( y ) = n w_t(y) = n w t ( y ) = n . In [29 ] the question does not arise: the keys are inserted first, insertions count as accesses, and the number of distinct keys accessed since the last access to a key is at most n n n . It agrees with the definition of § 1 up to an additive O ( m ) O(m) O ( m ) . For a key y y y that was searched, taking u u u to be its last search gives a term of ours that is at most Iacono’s, and a key that was never searched gives a term of at least log 2 ( n + 2 ) \log_2(n + 2) log 2 ( n + 2 ) , which no term of ours exceeds by more than one, since ∣ x t − x u ∣ + ρ ( u , t ) + 2 ≤ 2 ( n + 2 ) |x_t - x_u| + \rho(u,t) + 2 \le2(n + 2) ∣ x t − x u ∣ + ρ ( u , t ) + 2 ≤ 2 ( n + 2 ) . Conversely, for a time u u u , taking y = x u y = x_u y = x u gives a term of Iacono’s that is at most ours plus one.
Pattern-avoiding sequences
This section proves Theorem 1.3 and shows that the unified bound of a pattern-avoiding sequence is small. Both follow from one count: a search whose locality scale exceeds s s s is the first search in its cell of scale s s s , a block of 2 s 2^s 2 s keys together with an interval of 2 s 2^s 2 s searches, and in a sequence that avoids σ \sigma σ , few cells of each scale contain a search.
We first fix the definitions. Let σ \sigma σ be a permutation of { 1 , … , k } \{1,\ldots,k\} { 1 , … , k } . A sequence X X X contains σ \sigma σ if there are times t 1 < ⋯ < t k t_1 < \cdots< t_k t 1 < ⋯ < t k such that x t i < x t j x_{t_i} < x_{t_j} x t i < x t j exactly when σ ( i ) < σ ( j ) \sigma(i) < \sigma(j) σ ( i ) < σ ( j ) , and it avoids σ \sigma σ otherwise. The k k k keys of an occurrence are distinct, so repeated searches for a key create no occurrence by themselves, and for a permutation X X X this is the usual notion.
All 0-1 matrices used as patterns are nonzero. A 0-1 matrix contains a pattern P P P if some submatrix of it, with rows and columns in order, can be turned into P P P by changing ones into zeros; the zeros of P P P impose no requirement. We identify σ \sigma σ with its permutation matrix, which has a one in row σ ( l ) \sigma(l) σ ( l ) and column l l l .
The Füredi–Hajnal limit c σ c_\sigma c σ is the supremum over M M M of 1 / M 1/M 1/ M times the largest number of ones in an M × M M \times M M × M matrix that avoids σ \sigma σ . It is finite by the theorem of Marcus and Tardos [33 ] , and c σ = 2 O ( k ) c_\sigma= 2^{O(k)} c σ = 2 O ( k ) by a result of Fox [24 ] . Padding a rectangular matrix with zero rows or columns gives the form we use [6 Lemma 2.1] : every 0-1 matrix with M 1 M_1 M 1 rows, M 2 M_2 M 2 columns and more than c σ max ( M 1 , M 2 ) c_\sigma\max(M_1,M_2) c σ max ( M 1 , M 2 ) ones contains σ \sigma σ .
Throughout, X X X is a sequence of m m m searches over { 1 , … , n } \{1,\ldots,n\} { 1 , … , n } , n ≥ 2 n \ge2 n ≥ 2 , and the blocks and the locality scales are those of § 4.1 . Time is cut in the same way as the keys. For 0 ≤ s ≤ K 0 \le s \le K 0 ≤ s ≤ K , the epochs of level s s s are the intervals { j 2 s + 1 , … , ( j + 1 ) 2 s } \{j2^s + 1,\ldots,(j+1)2^s\} { j 2 s + 1 , … , ( j + 1 ) 2 s } , j ≥ 0 j \ge0 j ≥ 0 , intersected with { 1 , … , m } \{1,\ldots,m\} { 1 , … , m } ; an epoch of level s s s consists of at most 2 s 2^s 2 s consecutive times. A cell of scale s s s is a pair ( g , E ) (g,E) ( g , E ) of a block g g g of height s s s and an epoch E E E of level s s s . A search u u u lies in the cell if x u ∈ g x_u \in g x u ∈ g and u ∈ E u \in E u ∈ E , and the cell is occupied if a search lies in it. Every search lies in exactly one cell of each scale. At scale s s s there are r s : = ⌈ n / 2 s ⌉ r_s := \lceil n/2^s\rceil r s := ⌈ n / 2 s ⌉ blocks and e s : = ⌈ m / 2 s ⌉ e_s := \lceil m/2^s\rceil e s := ⌈ m / 2 s ⌉ epochs. Since 2 s < 2 n 2^s < 2n 2 s < 2 n , we have r s ≤ 2 n / 2 s r_s \le2n/2^s r s ≤ 2 n / 2 s ; and e s < 2 m / 2 s e_s < 2m/2^s e s < 2 m / 2 s when 2 s < m 2^s < m 2 s < m , while e s = 1 e_s = 1 e s = 1 when 2 s ≥ m 2^s \ge m 2 s ≥ m .
Lemma 8.1 (Scales and occupied cells). For every scale s < K s < K s < K , the number of searches t t t with λ ( t ) > s \lambda(t) > s λ ( t ) > s is at most the number o s o_s o s of occupied cells of scale s s s , and o s ≤ min ( m , r s e s ) o_s \le\min(m, r_s e_s) o s ≤ min ( m , r s e s ) .
Proof. We show that a search t t t with λ ( t ) > s \lambda(t) > s λ ( t ) > s is the first search in its cell of scale s s s . Suppose that an earlier search u u u lies in the same cell. Then x u ∈ B s ( x t ) x_u \in B_s(x_t) x u ∈ B s ( x t ) , and u u u and t t t lie in one epoch of at most 2 s 2^s 2 s consecutive times, so
ρ ( u , t ) ≤ t − u − 1 ≤ 2 s − 2. \rho(u,t) \le t-u-1 \le2^s-2. ρ ( u , t ) ≤ t − u − 1 ≤ 2 s − 2.
So the scale s s s satisfies the condition of Definition 4.1 , and λ ( t ) ≤ s \lambda(t) \le s λ ( t ) ≤ s by (1), contrary to the assumption. For t = 1 t=1 t = 1 there is no earlier search. Every occupied cell has one first search, so the searches with λ ( t ) > s \lambda(t)>s λ ( t ) > s lie in different occupied cells. Finally, every occupied cell contains a search and there are r s e s r_s e_s r s e s cells of scale s s s , so o s ≤ min ( m , r s e s ) o_s \le\min(m,r_s e_s) o s ≤ min ( m , r s e s ) .
Since 1 ≤ λ ( t ) ≤ K 1 \le\lambda(t) \le K 1 ≤ λ ( t ) ≤ K , summing over the scales and using the lower bound of Theorem 7.3 gives
UB ( X ) − m ≤ ∑ t = 1 m λ ( t ) = ∑ s = 0 K − 1 # { t : λ ( t ) > s } ≤ ∑ s = 0 K − 1 o s . (18) \operatorname{UB}(X)-m \le\sum_{t=1}^{m}\lambda(t)=\sum_{s=0}^{K-1}\#\{t:\lambda(t)>s\}\le\sum_{s=0}^{K-1}o_s. \tag*{(18)} UB ( X ) − m ≤ t = 1 ∑ m λ ( t ) = s = 0 ∑ K − 1 # { t : λ ( t ) > s } ≤ s = 0 ∑ K − 1 o s . (18)
Proposition 8.2 (Unified bound of pattern-avoiding sequences). If X X X avoids a permutation σ \sigma σ of length k ≥ 2 k\ge2 k ≥ 2 , then ∑ t λ ( t ) ≤ m log 2 c σ + 4 m + 11 2 n \sum_t\lambda(t)\le m\log_2 c_\sigma+4m+\frac{11}{2}n ∑ t λ ( t ) ≤ m log 2 c σ + 4 m + 2 11 n and UB ( X ) ≤ m log 2 c σ + 5 m + 11 2 n = O ( ( m + n ) ( 1 + log c σ ) ) \operatorname{UB}(X)\le m\log_2 c_\sigma+5m+\frac{11}{2}n=O((m+n)(1+\log c_\sigma)) UB ( X ) ≤ m log 2 c σ + 5 m + 2 11 n = O (( m + n ) ( 1 + log c σ )) .
Proof. Fix a scale s s s and consider the 0 0 0 -1 1 1 matrix with one row for each of the r s r_s r s blocks of height s s s , in key order, one column for each of the e s e_s e s epochs of level s s s , in time order, and a one wherever the cell is occupied. Suppose that this matrix contains σ \sigma σ , with ones in rows i 1 , … , i k i_1,\ldots,i_k i 1 , … , i k and columns j 1 < ⋯ < j k j_1<\cdots<j_k j 1 < ⋯ < j k , where the rows are ordered as σ \sigma σ . Choose a search t l t_l t l in the cell of row i l i_l i l and column j l j_l j l , for every l l l . The times increase with l l l , because the epochs are disjoint intervals, and the keys are distinct and ordered as σ \sigma σ , because the blocks are disjoint intervals. So X X X contains σ \sigma σ .
Hence if X X X avoids σ \sigma σ , the matrix avoids σ \sigma σ , and by the padded form of the Marcus–Tardos bound it has at most c σ max ( r s , e s ) ≤ c σ ( r s + e s ) c_\sigma\max(r_s,e_s)\le c_\sigma(r_s+e_s) c σ max ( r s , e s ) ≤ c σ ( r s + e s ) ones, that is, o s ≤ c σ ( r s + e s ) o_s\le c_\sigma(r_s+e_s) o s ≤ c σ ( r s + e s ) . Since σ \sigma σ has length at least 2 2 2 , an M × M M\times M M × M matrix whose ones fill one row avoids it, so c σ ≥ 1 c_\sigma\ge1 c σ ≥ 1 .
It remains to add up over the scales. By (19), ∑ t λ ( t ) ≤ ∑ s = 0 K − 1 o s \sum_t\lambda(t)\le\sum_{s=0}^{K-1}o_s ∑ t λ ( t ) ≤ ∑ s = 0 K − 1 o s , where o s ≤ m o_s\le m o s ≤ m . For the scales with 2 s ≥ m 2^s\ge m 2 s ≥ m there is one epoch, so o s ≤ r s ≤ 2 n / 2 s o_s\le r_s\le2n/2^s o s ≤ r s ≤ 2 n / 2 s , and these scales contribute at most 4 n / m ≤ 4 n 4n/m\le4n 4 n / m ≤ 4 n in total. For the scales with 2 s < m 2^s<m 2 s < m , the bounds on r s r_s r s and e s e_s e s give
o s ≤ min ( m , 2 c σ ( n + m ) 2 s ) . o_s\le\min\left(m,\frac{2c_\sigma(n+m)}{2^s}\right). o s ≤ min ( m , 2 s 2 c σ ( n + m ) ) .
Let Z : = 2 c σ ( n + m ) / m ≥ 2 Z:=2c_\sigma(n+m)/m\ge2 Z := 2 c σ ( n + m ) / m ≥ 2 . The scales with 2 s < Z 2^s<Z 2 s < Z contribute at most m m m each, and there are at most log 2 Z + 1 \log_2 Z+1 log 2 Z + 1 of them; the remaining scales form a geometric series with sum at most 2 m 2m 2 m . Since log 2 Z = 1 + log 2 c σ + log 2 ( 1 + n / m ) \log_2 Z=1+\log_2 c_\sigma+\log_2(1+n/m) log 2 Z = 1 + log 2 c σ + log 2 ( 1 + n / m ) and m log 2 ( 1 + n / m ) ≤ n / ln 2 < 3 2 n m\log_2(1+n/m)\le n/\ln2<\frac{3}{2}n m log 2 ( 1 + n / m ) ≤ n / ln 2 < 2 3 n , altogether ∑ t λ ( t ) ≤ m log 2 c σ + 4 m + 11 2 n \sum_t\lambda(t)\le m\log_2 c_\sigma+4m+\frac{11}{2}n ∑ t λ ( t ) ≤ m log 2 c σ + 4 m + 2 11 n . The lower bound of Theorem 7.3 gives the bound on UB ( X ) \operatorname{UB}(X) UB ( X ) .
This proves Theorem 1.3 as well. By Theorem 4.4 , Algorithm 1, which does not depend on σ \sigma σ , serves X X X at cost O ( ∑ t λ ( t ) ) = O ( ( m + n ) ( 1 + log c σ ) ) O(\sum_t\lambda(t))=O((m+n)(1+\log c_\sigma)) O ( ∑ t λ ( t )) = O (( m + n ) ( 1 + log c σ )) . With c σ = 2 O ( k ) c_\sigma=2^{O(k)} c σ = 2 O ( k ) the cost is O ( ( m + n ) k ) O((m+n)k) O (( m + n ) k ) , and for a permutation, where m = n m=n m = n , it is O ( n ( 1 + log c σ ) ) O(n(1+\log c_\sigma)) O ( n ( 1 + log c σ )) .
Running time
The model of § 3 charges only the nodes that a search touches. The computation that decides which nodes to touch, including the records that the algorithm keeps about past searches, is free in the model. This section states that it fits in the same bounds on a word RAM and explains the one point that needs an argument. Section A gives the implementation and the proof.
Proposition 9.1 (Running time). On a word RAM with words of Ω ( log ( n + m ) ) \Omega(\log(n+m)) Ω ( log ( n + m )) bits, Algorithm 1 can be implemented deterministically with O ( n ) O(n) O ( n ) words of memory so that, after an initialization of O ( n ) O(n) O ( n ) time, search t t t takes O ( λ ( t ) ) O(\lambda(t)) O ( λ ( t )) time. Hence every single search takes O ( log n ) O(\log n) O ( log n ) time, and all searches together take O ( m + UB ( X ) ) O(m+\operatorname{UB}(X)) O ( m + UB ( X )) time.
The region tree and the inner trees are stored as they are, so a walk, a split or a join takes the time that bounds its cost in the model. Apart from these, SEARCH in Algorithm 1 asks three questions about the kept blocks. Is a given block kept? Are 2 s 2^s 2 s blocks of height s s s kept? And which kept block of height s s s was searched least recently? The first two are answered by an array and a counter for every promoted scale. The third is the point that needs an argument.
Why the third question is not as easy. The obvious answer is a list of the kept blocks of each height in the order of their last searches: a search moves its blocks to the front of their lists, and the victim of a scale is the last block of its list. This is too slow. A search has a kept block at every promoted scale, so it would touch a list at each of the about log 2 log 2 n \log_2\log_2 n log 2 log 2 n promoted scales, and a search with a small locality scale cannot pay for this.
The idea. A victim never contains a kept block of smaller height (Part 5 of Lemma 5.1). So the list of a scale needs only the kept blocks that contain no kept block of smaller height; we call such a block childless . After a search for x t x_t x t , all blocks of x t x_t x t at the promoted scales are kept, so the only childless one among them is the lowest, B 1 ( x t ) B_1(x_t) B 1 ( x t ) . Hence, apart from a constant number of list operations for every block that it retires or adds, which it pays for anyway, a search has to touch one list only, that of the scale 1. A higher block enters its list later, at the moment when the last kept block inside it retires, and it then needs to know when it was last searched. For this, a retiring block hands the time of its last search to its parent. The lists are of the same kind as those that the cache-splay tree keeps for its levels [19 Section 6.3] . What has to be proved is that a block which enters a list in this way belongs at its front, so that the last block of a list is always the victim of its scale (Lemma A.1 ).
Insertions and deletions
We now let the set of keys change. Bădoiu, Cole, Demaine and Iacono [BCD07] extended the unified bound to insertions and deletions, with the distance between two keys counted among the keys that are present; we call it the unified bound with updates . Our tree extends to it (Theorem 10.1 ).
The model and the result
We are given a set S 1 S_1 S 1 of n 0 n_0 n 0 keys from an ordered set and a sequence X X X of m m m operations . Operation t t t has a key x t x_t x t and is a search, an insertion or a deletion. Let S t S_t S t be the set of keys before it, and n t : = ∣ S t ∣ n_t := |S_t| n t := ∣ S t ∣ . A search or a deletion has x t ∈ S t x_t \in S_t x t ∈ S t , and an insertion has x t ∉ S t x_t \notin S_t x t ∈ / S t . The successor of a key in a set is the next larger key of the set, and its predecessor the next smaller one.
Before operation t t t the tree is a BST on S t S_t S t . An operation is carried out in steps . A step replaces the subtree formed by a top set U U U by another BST and reattaches the subtrees that hang off U U U , at cost ∣ U ∣ |U| ∣ U ∣ .
Search. Every step keeps the keys of U U U , and some step has x t ∈ U x_t \in U x t ∈ U . By Lemma 3.1 this is the model of § 3 .
Insertion. Exactly one step changes the keys. Its set U U U contains the search path of x t x_t x t , the nodes with which a search for x t x_t x t compares it, and its new tree has the keys of U U U and x t x_t x t . The predecessor and the successor of x t x_t x t are on the search path, so no subtree that hangs off U U U has keys on both sides of x t x_t x t .
Deletion. Exactly one step changes the keys. Its set U U U contains x t x_t x t , and its predecessor or its successor if x t x_t x t has two children, and its new tree has the keys of U U U other than x t x_t x t . Then at most one subtree hangs off U U U between the two keys of U U U next to x t x_t x t , so it can be attached between them.
So the subtrees that hang off U U U can be reattached in every step. A step is carried out by O ( ∣ U ∣ + 1 ) O(|U| + 1) O ( ∣ U ∣ + 1 ) pointer moves and rotations that start at the root, together with the insertion of a leaf or the deletion of a node with at most one child, and conversely the nodes that such an operation touches form a top set. So up to a constant factor and one unit per operation, these are the costs of a BST with rotations [38 , 20 ] .
The unified bound with updates. A time u < t u < t u < t is a witness for operation t t t if operation u u u is a search or an insertion and x u x_u x u is not deleted before t t t , that is, x u ∈ S r x_u \in S_r x u ∈ S r for u < r ≤ t u < r \le t u < r ≤ t . For y ∈ S t y \in S_t y ∈ S t and a key x x x , let d t ( y , x ) d_t(y,x) d t ( y , x ) be the number of keys z ∈ S t z \in S_t z ∈ S t with y < z ≤ x y < z \le x y < z ≤ x or x ≤ z < y x \le z < y x ≤ z < y . As before, ρ ( u , t ) \rho(u,t) ρ ( u , t ) is the number of distinct keys of the operations strictly between u u u and t t t . Let
τ ( u , t ) : = log 2 ( d t ( x u , x t ) + ρ ( u , t ) + 2 ) , \tau(u,t) := \log_2(d_t(x_u,x_t) + \rho(u,t) + 2), τ ( u , t ) := log 2 ( d t ( x u , x t ) + ρ ( u , t ) + 2 ) ,
τ t : = min ( log 2 ( n t + 2 ) , min { τ ( u , t ) : u is a witness for t } ) , \tau_t := \min\left(\log_2(n_t + 2), \min\{\tau(u,t) : u \text{ is a witness for } t\}\right), τ t := min ( log 2 ( n t + 2 ) , min { τ ( u , t ) : u is a witness for t } ) ,
and UB ( X ) : = ∑ t = 1 m τ t \operatorname{UB}(X) := \sum_{t=1}^{m} \tau_t UB ( X ) := ∑ t = 1 m τ t . For searches in a fixed set this is the definition of § 1 , up to less than 1 per term. The bound of [1 ] for operation t t t is the minimum over y ∈ S t y \in S_t y ∈ S t of log 2 ( w t ( y ) + d t ( y , x t ) + 2 ) \log_2(w_t(y) + d_t(y,x_t) + 2) log 2 ( w t ( y ) + d t ( y , x t ) + 2 ) , where w t ( y ) w_t(y) w t ( y ) is the number of distinct keys of the operations since the last operation with the key y y y . That operation is a witness u u u with ρ ( u , t ) ≤ w t ( y ) \rho(u,t) \le w_t(y) ρ ( u , t ) ≤ w t ( y ) , so τ t \tau_t τ t is at most this bound, if a key that had no operation is given w t ( y ) = n t w_t(y) = n_t w t ( y ) = n t as in Remark 7.4.
Theorem 10.1 (Insertions and deletions). There is a deterministic online BST algorithm for searches, insertions and deletions that, starting from a tree that depends only on S 1 S_1 S 1 , serves every sequence X X X of m m m operations at total cost
O ( n 0 + m + UB ( X ) ) , O(n_0 + m + \operatorname{UB}(X)), O ( n 0 + m + UB ( X )) , and operation t t t at cost O ( log ( n t + 2 ) ) O(\log(n_t + 2)) O ( log ( n t + 2 )) .
We do not know whether the term n 0 n_0 n 0 is needed, and we do not analyse the running time. Since the optimum costs at most as much as this algorithm, the theorem bounds the optimum.
Corollary 10.2 (The optimum with insertions and deletions). For a sequence X X X of m m m searches, insertions and deletions, starting from a set of n 0 n_0 n 0 keys, let OPT ( X ) \operatorname{OPT}(X) OPT ( X ) be the least total cost at which X X X can be served in the model above, over all initial trees and over all algorithms, which may know X X X in advance. Then OPT ( X ) = O ( n 0 + m + UB ( X ) ) \operatorname{OPT}(X) = O(n_0 + m + \operatorname{UB}(X)) OPT ( X ) = O ( n 0 + m + UB ( X )) .
The idea
The algorithm is that of § 4 . Two things in it must change, the blocks and the time at which a block was last used, and the analysis needs one new argument. Section B gives the algorithm and the complete proof.
Blocks that grow and shrink. In § 4 a block is an interval of a grid that is fixed in advance. With insertions this is impossible. If a key smaller than all others is inserted, the rank of every key goes up by one, so every border of the grid would move by one key, and the tree would have to change everywhere. So the blocks are now intervals of the present keys, and an inserted key joins the block of the next larger key. A block of level k k k , where k k k is a power of two, has between 8 k 8^k 8 k and 4 ⋅ 8 k 4 \cdot8^k 4 ⋅ 8 k keys, about 2 3 k 2^{3k} 2 3 k , so it plays the part of a block of height 3 k 3k 3 k : its keys lie at depth O ( k ) O(k) O ( k ) when the block is kept. When it leaves this range, it is split into two blocks or merged with a neighbour, as the nodes of a B-tree are. For example, a block of level 1 has 8 to 32 keys. Its 33rd key splits it into blocks of 16 and 17 keys, and neither of them is split or merged again before it has gained at least 16 keys or lost at least 9. In general a new block of level k k k must gain or lose a quarter of 8 k 8^k 8 k keys before it is repaired, so there are O ( m / 8 k ) O(m/8^k) O ( m / 8 k ) repairs at level k k k (Lemma B.1 ). A repair is a split, a merge, or a merge followed by a split of the merged block. Each split or merge changes the regions at one place and retires or adds at most one block of level k k k , so a repair costs O ( k + 1 ) O(k+1) O ( k + 1 ) , and all repairs together cost O ( m ) O(m) O ( m ) .
The time of a block. As before, the tree keeps at every level k k k the 3 ⋅ 8 k 3 \cdot8^k 3 ⋅ 8 k blocks that were used most recently. A search or an insertion uses its own key. A deletion should count as a use of the blocks of the deleted key, but the key is gone afterwards, and the time of the deletion would be gone with it: if the last operations in a block were deletions, the block would look as if it had not been used for a long time, and the tree would stop keeping it. So a deletion uses a neighbour of the deleted key in the same block of level 1, its heir . Then the time of a block, the last time that one of its keys was used, does not decrease while the block exists.
The analysis. An operation costs as much as a search, plus O ( k + 1 ) O(k+1) O ( k + 1 ) for every repair at a level k k k (Lemma B.5 ). The comparison with the unified bound follows Lemma 7.2 (Lemma B.6 ): an operation whose block of level k k k was not used for a long time is charged to its chain of partners (Lemma 7.1 ), which lies near a border of this block. Two things are new. A block that a split or a merge has just created has no history, so an operation in it may have no chain to be charged to; but each block is in this situation for at most one operation, and these operations are paid for by the count of the repairs. And the borders of the blocks move as keys are inserted and deleted; but a present key never crosses the border of a block that survives, because an inserted key joins the block of the next larger key, and this is all that the charging needs.
Splay trees. The insertions and deletions of splay trees [38 ] , § 3 consist of steps of the model. A splay is a step whose set U U U is the path that it follows. Adding a key, as a leaf or as the new root after a splay, and removing a node and joining its two subtrees are steps whose sets U U U consist of nodes on the paths of the splays of the operation. So the cost in the model is at most a constant times the number of nodes that the splay tree visits. Call an algorithm dynamically optimal for searches, insertions and deletions if it serves every sequence from every initial tree at cost O ( OPT ( X ) + n 0 ) O(\operatorname{OPT}(X)+n_0) O ( OPT ( X ) + n 0 ) . By Corollary 10.2, such an algorithm costs O ( n 0 + m + UB ( X ) ) O(n_0+m+\operatorname{UB}(X)) O ( n 0 + m + UB ( X )) . Bădoiu, Cole, Demaine and Iacono conjecture that splay trees satisfy their bound in the amortized sense [BCD107]. So if splay trees are dynamically optimal for all three operations, this conjecture holds for the total cost of every sequence that starts from the empty set, and for other sequences up to an additive O ( n 0 ) O(n_0) O ( n 0 ) and the convention for keys that were never accessed.
Running time: the implementation and the proof
This appendix proves Proposition 9.1 . Section A.1 gives the records and the lists that find the victims and proves that they are correct (Lemma A.1 ), and Section A.2 describes the rest of the implementation and adds up the time. The budget for search t t t , with M t M_t M t the set of its missing scales, is
O ( ∣ M t ∣ + 1 ) ⏟ records and lists + ∑ s ∈ M t O ( s + 1 ) ⏟ retirements + O ( λ ( t ) ) ⏟ walk + O ( λ ( t ) ) ⏟ promotions = O ( λ ( t ) ) , (19) \underbrace{O(|M_t|+1)}_{\text{records and lists}}+\sum_{s\in M_t} \underbrace{O(s+1)}_{\text{retirements}}+\underbrace{O(\lambda(t))}_{\text{walk}}+\underbrace{O(\lambda(t))}_{\text{promotions}}=O(\lambda(t)), \tag*{(19)} records and lists O ( ∣ M t ∣ + 1 ) + s ∈ M t ∑ retirements O ( s + 1 ) + walk O ( λ ( t )) + promotions O ( λ ( t )) = O ( λ ( t )) , (19)
where the first two terms are O ( λ ( t ) ) O(\lambda(t)) O ( λ ( t )) by (10) .
Finding the victims
The victim of a scale is the kept block of its height that was searched least recently (§ 4.2 ). This subsection describes the arrays, records and lists with which the implementation finds it in constant time.
Arrays and counters. For every promoted scale s s s , an array has one entry for each block of height s s s , where the block B s ( y ) B_s(y) B s ( y ) of a key y y y has the number ⌈ y / 2 s ⌉ \lceil y/2^s\rceil ⌈ y / 2 s ⌉ ; the entry points to the record of the block if the block is kept. A counter holds the number of kept blocks of each promoted height. The arrays have ∑ s ∈ S ⌈ n / 2 s ⌉ ≤ 2 n \sum_{s\in S}\lceil n/2^s\rceil\le2n ∑ s ∈ S ⌈ n / 2 s ⌉ ≤ 2 n entries in all. They tell in constant time whether a block is kept and whether 2 s 2^s 2 s blocks of height s s s are kept.
Children. Let F \mathcal{F} F be the set of the kept blocks at some moment. For g ∈ F g\in\mathcal{F} g ∈ F , let
↓ g : = { c ∈ F : c ⊆ g , h ( c ) ≤ h ( g ) } , kids ( g ) : = { c ∈ F : the parent of c is g } \downarrow g:=\{c\in\mathcal{F}:c\subseteq g,\ h(c)\le h(g)\},\qquad\operatorname{kids}(g):=\{c\in\mathcal{F}:\text{the parent of }c\text{ is }g\} ↓ g := { c ∈ F : c ⊆ g , h ( c ) ≤ h ( g )} , kids ( g ) := { c ∈ F : the parent of c is g }
be the kept blocks inside g g g and the kept children of g g g . Unlike the set ↓ G b \downarrow G_b ↓ G b in the proof of Lemma 5.1, the set ↓ g \downarrow g ↓ g contains g g g itself. The block g g g is childless if it contains no kept block of smaller height, and
g is childless ⟺ ↓ g = { g } ⟺ kids ( g ) = ∅ . (20) g\text{ is childless}\quad\Longleftrightarrow\quad\downarrow g=\{g\}\quad\Longleftrightarrow\quad\operatorname{kids}(g)=\varnothing. \tag*{(20)} g is childless ⟺ ↓ g = { g } ⟺ kids ( g ) = ∅ . (20)
The first equivalence holds because two blocks of the same height are disjoint. For the second, a kept child of g g g is a kept block of smaller height inside g g g , and conversely, if g g g contains kept blocks of smaller height, the highest of them has no kept block between itself and g g g , so its parent is g g g .
The records. The record of a kept block g g g holds the index of the block and two numbers: its date date ( g ) \operatorname{date}(g) date ( g ) , which is the time of a search in the block, and the number ∣ kids ( g ) ∣ |\operatorname{kids}(g)| ∣ kids ( g ) ∣ of its kept children. For every promoted scale s s s , a doubly linked list, the list of the scale , holds the records of the childless kept blocks of height s s s . Initially the only record is that of the top block, with the date 0, and it is in its list. Three rules maintain the records and the lists, each in constant time.
A block is added. When search t t t adds a block, its record gets the date t t t and no kept children, and it is put at the front of the list of its scale. Its parent has one more kept child, and leaves its list if this is its first. So during the promotions of a search, an added block leaves its list again when the next lower block is added.
date ( p ) ← max ( date ( p ) , date ( e ) ) . (21) \operatorname{date}(p) \mathrel{\leftarrow} \max(\operatorname{date}(p),\operatorname{date}(e)). \tag*{(21)} date ( p ) ← max ( date ( p ) , date ( e )) . (21)
If e e e was the last kept child of p p p , the record of p p p is put at the front of the list of its scale.
The parent of a kept block of height s s s is the kept block of height 2 s 2s 2 s around it, by (6) , so its record is found through the array of that scale. By (20) , the rules keep in the list of a scale s s s exactly the childless kept blocks of height s s s .
For example, let n = 16 n=16 n = 16 , so that the promoted scales are 1 1 1 , 2 2 2 and 4 4 4 , and let the searches be 1 , 5 , 9 , 13 1,5,9,13 1 , 5 , 9 , 13 . Two blocks of height 1 1 1 are kept at a time. Search 3, for the key 9, retires the block { 1 , 2 } \{1,2\} { 1 , 2 } , whose date is 1. Its parent { 1 , … , 4 } \{1,\ldots,4\} { 1 , … , 4 } , whose date is 1 as well, enters the list of the scale 2, which was empty. Search 4, for the key 13, retires { 5 , 6 } \{5,6\} { 5 , 6 } in the same way, and its parent { 5 , … , 8 } \{5,\ldots,8\} { 5 , … , 8 } enters the same list at the front, with the date 2. The list of the scale 2 is now { 5 , … , 8 } , { 1 , … , 4 } \{5,\ldots,8\},\{1,\ldots,4\} { 5 , … , 8 } , { 1 , … , 4 } , which is the order of the last searches in these blocks. If a fifth search is for the key 2, it retires { 9 , 10 } \{9,10\} { 9 , 10 } , whose parent enters the list at the front, and it adds { 1 , 2 } \{1,2\} { 1 , 2 } , so that { 1 , … , 4 } \{1,\ldots,4\} { 1 , … , 4 } has a kept child again and leaves the list.
The rules always put a record at the front. The next lemma shows that this is the right place: a parent that becomes childless was searched later than every block that is already in its list. As in § 5.1 , ℓ t ( g ) \ell_t(g) ℓ t ( g ) is the last search in a block g g g before t t t , and we let ℓ t ( g ) : = 0 \ell_t(g):=0 ℓ t ( g ) := 0 if g g g is not searched before t t t . Recall that the victim of a scale s s s in search t t t is the block of height s s s in F t \mathcal{F}_t F t with the least ℓ t \ell_t ℓ t .
Lemma A.1 (The lists of the scales). Let t ∈ { 1 , … , m + 1 } t\in\{1,\ldots,m+1\} t ∈ { 1 , … , m + 1 } . When search t t t begins and during its first phase, or after the last search if t = m + 1 t=m+1 t = m + 1 ,
max { date ( c ) : c ∈ ↓ g } = ℓ t ( g ) , (22) \max\{\operatorname{date}(c):c\in\downarrow g\}=\ell_t(g), \tag*{(22)} max { date ( c ) : c ∈↓ g } = ℓ t ( g ) , (22) so a childless kept block g g g has date ( g ) = ℓ t ( g ) \operatorname{date}(g)=\ell_t(g) date ( g ) = ℓ t ( g ) ;
Hence, whenever the first phase of a search retires the victim of a scale, the victim is the last block of the list of this scale.
Proof. Part 1 is an induction over the changes of the kept blocks, in which each change leaves both sides of (22) as they are or sets both to t t t . Part 2 holds at all times; it reduces to one inequality between a block that enters a list and a block that is already there, and this inequality compares the last searches of two blocks through Part 1.
Part 1. Write D ( g ) D(g) D ( g ) for the left side of (22) . When the first search begins, the top block is the only kept block, and D ( g ) = 0 = ℓ 1 ( g ) D(g)=0=\ell_1(g) D ( g ) = 0 = ℓ 1 ( g ) for it. There are two kinds of changes.
A retirement in the first phase of search t t t . Let e e e retire, with parent p p p , and let g ≠ e g\ne e g = e be a kept block. If e ∉ ↓ g e\notin\downarrow g e ∈ / ↓ g , then p ∉ ↓ g p\notin\downarrow g p ∈ / ↓ g either, because e ⊂ p e\subset p e ⊂ p , so neither ↓ g \downarrow g ↓ g nor a date in it changes. If e ∈ ↓ g e\in\downarrow g e ∈↓ g , then g g g is a kept block that contains e e e and is higher, and p p p is the lowest such block, so p ∈ ↓ g p\in\downarrow g p ∈↓ g . By (21) , the retirement replaces the two dates of e e e and p p p in ↓ g \downarrow g ↓ g by their maximum. So in both cases D ( g ) D(g) D ( g ) does not change, and ℓ t ( g ) \ell_t(g) ℓ t ( g ) does not depend on the moment.
From the first phase of search t t t to the beginning of search t + 1 t+1 t + 1 . In between, the kept blocks and the dates change only by the first two rules: blocks of x t x_t x t are added, and blocks of x t x_t x t get the date t t t . Let g ∈ F t + 1 g\in\mathcal{F}_{t+1} g ∈ F t + 1 . Then
x t ∉ g : D ( g ) does not change, and ℓ t + 1 ( g ) = ℓ t ( g ) ; x_t\notin g:\quad D(g)\text{ does not change, and }\ell_{t+1}(g)=\ell_t(g); x t ∈ / g : D ( g ) does not change, and ℓ t + 1 ( g ) = ℓ t ( g ) ;
x t ∈ g : D ( g ) = t = ℓ t + 1 ( g ) . x_t \in g:\quad D(g)=t=\ell_{t+1}(g). x t ∈ g : D ( g ) = t = ℓ t + 1 ( g ) .
In the first line, no block of x t x_t x t lies in g g g , so ↓ g \downarrow g ↓ g and its dates do not change, and g g g was kept during the first phase because search t t t adds only blocks of x t x_t x t . In the second line, B 1 ( x t ) B_1(x_t) B 1 ( x t ) is a kept block inside g g g with the date t t t at the end of the search, and no date exceeds t t t .
For a childless block, ↓ g = { g } \downarrow g=\{g\} ↓ g = { g } by (20) , so D ( g ) = date ( g ) D(g)=\operatorname{date}(g) D ( g ) = date ( g ) .
Part 2. A record changes its date in two ways. It gets the date t t t of the current search, and then it is put or moved to the front of its list. Or its block is a parent that takes the date of a retiring child by (21) , and then it is not in a list, because it has a kept child. So it suffices to show that a record that is put at the front has a later date than every other record of its list. There are two cases.
The record gets the date t t t in search t t t . Its block contains x t x_t x t . Another block of the same height does not contain x t x_t x t , so it did not get the date t t t , and all other dates are earlier than t t t .
A parent becomes childless. Let the first phase of search t t t retire e e e , the last kept child of p p p . Let s : = h ( e ) s:=h(e) s := h ( e ) , so that h ( p ) = 2 s h(p)=2s h ( p ) = 2 s , and let q ≠ p q\ne p q = p be a block in the list of the scale 2 s 2s 2 s . The idea is that q q q lost its last child at an earlier time, when this child was the least recently searched kept block of its height, while e e e was searched more recently or was not kept yet. The block q q q is not the top block, so it is searched before t t t . Let c c c be the block of height s s s inside q q q that contains the key of the search ℓ t ( q ) \ell_t(q) ℓ t ( q ) , so that ℓ t ( c ) = ℓ t ( q ) \ell_t(c)=\ell_t(q) ℓ t ( c ) = ℓ t ( q ) . We show that
date ( q ) = ( a ) ℓ t ( q ) = ℓ t ( c ) < ( b ) ℓ t ( e ) ≤ ( c ) date ( e ) ≤ ( d ) date ( p ) , (23) \operatorname{date}(q) \mathrel{\overset{(a)}{=}} \ell_t(q)=\ell_t(c) \mathrel{\overset{(b)}{<}} \ell_t(e) \mathrel{\overset{(c)}{\le}} \operatorname{date}(e) \mathrel{\overset{(d)}{\le}} \operatorname{date}(p), \tag*{(23)} date ( q ) = ( a ) ℓ t ( q ) = ℓ t ( c ) < ( b ) ℓ t ( e ) ≤ ( c ) date ( e ) ≤ ( d ) date ( p ) , (23)
where date ( p ) \operatorname{date}(p) date ( p ) is the date of p p p after the retirement of e e e . Steps (a) and (c) are Part 1: the block q q q is childless because it is in a list, and e e e is childless when it retires (Part 5 of Lemma 5.1). Step (d) is (21) .
For (b), let u : = ℓ t ( c ) u:=\ell_t(c) u := ℓ t ( c ) . Search u u u promoted c c c , so c ∈ F u + 1 c\in\mathcal{F}_{u+1} c ∈ F u + 1 . Now c c c is not kept, because q q q is childless, and a block leaves the kept blocks only as a victim. So c c c was the victim of the scale s s s in a search t ′ t' t ′ with u < t ′ ≤ t u<t'\le t u < t ′ ≤ t . Here t ′ ≠ t t'\ne t t ′ = t , because the victim of the scale s s s in search t t t is e e e , and e ≠ c e\ne c e = c since e ⊆ p e\subseteq p e ⊆ p and c ⊆ q c\subseteq q c ⊆ q are disjoint. No search between u u u and t t t lies in c c c , so ℓ t ′ ( c ) = u \ell_{t'}(c)=u ℓ t ′ ( c ) = u , and the victim c c c has the least ℓ t ′ \ell_{t'} ℓ t ′ among the blocks of height s s s in F t ′ \mathcal{F}_{t'} F t ′ . The block e e e is kept at time t t t . So
ℓ t ( e ) ≥ { ℓ t ′ ( e ) > ℓ t ′ ( c ) = u , if e ∈ F t ′ , t ′ > u , if e ∉ F t ′ , \ell_t(e)\ge
\begin{cases}
\ell_{t'}(e)>\ell_{t'}(c)=u, & \text{if } e\in\mathcal{F}_{t'},\\
t'>u, & \text{if } e\notin\mathcal{F}_{t'},
\end{cases} ℓ t ( e ) ≥ { ℓ t ′ ( e ) > ℓ t ′ ( c ) = u , t ′ > u , if e ∈ F t ′ , if e ∈ / F t ′ ,
where in the second case e e e was added by a search at the time t ′ t' t ′ or later, and this search lies in e e e .
The victim. Let the first phase of search t t t retire the victim e e e of a scale s s s . Then e e e is childless (Part 5 of Lemma 5.1), so its record is in the list of the scale s s s . Every block b b b of this list is a kept block of height s s s with date ( b ) = ℓ t ( b ) \operatorname{date}(b)=\ell_t(b) date ( b ) = ℓ t ( b ) , by Part 1, and e e e has the least ℓ t \ell_t ℓ t among the kept blocks of height s s s . So e e e has the earliest date of its list, and by Part 2 its record is the last one. □ \square □
The rest of the implementation
The proof of Proposition 9.1 goes through Algorithm 1 line by line. For each line it says what the line needs besides the splits and joins of Changeregions, where the implementation finds it, and how long this takes. Table 1 collects the answers, and its last column adds up to the budget (19) .
Table 1. The implementation of Algorithm 1: what every step needs besides its splits and joins, and the time to find it. The times of ChangeRegions and of the walk are their costs in the model (Lemma 6.5 ).
What is stored. The implementation stores the following.
The trees. The region tree and the inner trees are stored as globally biased trees with parent pointers and ranks. Every internal node stores its key, the key that it has in T ( T ) T(\mathcal{T}) T ( T ) by Definition 4.3 . A leaf of the region tree stores the endpoints and the owner of its region and a pointer to its inner tree. The tree T ( T ) T(\mathcal{T}) T ( T ) is not stored separately: its nodes are the internal nodes of these trees and the last leaf, so a walk in it is a walk in them.
Three arrays on the keys. For a key y y y , first[ y ] [y] [ y ] and last[ y ] [y] [ y ] are the leaves of the regions whose smallest key, or largest key, is y y y , if there are such regions, and leaf[ y ] [y] [ y ] is the leaf of y y y in its inner tree.
Proof of Proposition 9.1 . We show that search t t t takes the time (19) , by checking the rows of Table 1 . Their times add up to
O ( ∣ M t ∣ + 1 ) + ∑ s ∈ M t O ( s + 1 ) + O ( λ ( t ) ) + O ( λ ( t ) ) = O ( λ ( t ) ) , O(|M_t|+1)+\sum_{s\in M_t}O(s+1)+O(\lambda(t))+O(\lambda(t))=O(\lambda(t)), O ( ∣ M t ∣ + 1 ) + s ∈ M t ∑ O ( s + 1 ) + O ( λ ( t )) + O ( λ ( t )) = O ( λ ( t )) ,
where the second term bounds the retirements, the third the walk and the fourth the promotions, as in the proof of Theorem 4.4 , and the first covers everything that takes constant time for each missing scale. Splits and joins run in the time bounded in the proof of Lemma 6.4 , because the rank of a leaf is computed in constant time: the weights of the regions are powers of 4 4 4 with exponents of size O ( K ) O(K) O ( K ) , and the keys have weight 1 1 1 . The rows are checked as follows.
The missing scales and the victims. The missing scales are the promoted scales below some bound (Part 3 of Lemma 5.1). So the implementation looks up B s ( x t ) B_s(x_t) B s ( x t ) for s = 1 , 2 , 4 , … s=1,2,4,\ldots s = 1 , 2 , 4 , … until it meets a kept block, and the scales that it has passed are M t M_t M t . At each of them whose counter is 2 s 2^s 2 s , the victim is the last block of the list of the scale (Lemma A.1 ).
Retire (e e e ). The keys of e e e form one region, whose leaf is first[min e e e ], and its two neighbours are last[min e − 1 ] a n d f i r s t [ m a x e-1] and first[max e − 1 ] an df i r s t [ ma x e+1]. The list Q Q Q of line 3 consists of this region and of those neighbours that the parent p p p of e e e owns. Let z : = min e − 1 z := \min e - 1 z := min e − 1 , and let F \mathcal{F} F be the set of the kept blocks at this moment. Then
p owns the left neighbour of e ⟺ z ∈ p and B h ( e ) ( z ) ∉ F , (24) p\text{ owns the left neighbour of }e \Longleftrightarrow z \in p\text{ and }B_{h(e)}(z) \notin\mathcal{F}, \tag*{(24)} p owns the left neighbour of e ⟺ z ∈ p and B h ( e ) ( z ) ∈ / F , (24)
and the right neighbour is tested in the same way, with max e + 1 \max e + 1 max e + 1 . Indeed, if z ∈ p z \in p z ∈ p , the owner of z z z is p p p or a lower kept block that contains z z z . Such a block has a height of at most h ( e ) h(e) h ( e ) , since h ( p ) = 2 h ( e ) h(p) = 2h(e) h ( p ) = 2 h ( e ) is the next promoted height, and by the nesting of Part 5 of Lemma 5.1 there is one exactly if B h ( e ) ( z ) B_{h(e)}(z) B h ( e ) ( z ) is kept.
The walk. The walk compares x t x_t x t with the keys that the nodes store. It then goes on from the node of x t x_t x t to the leaf of x t x_t x t in the expanded tree, and so passes the leaf of the region of x t x_t x t . Both parts take O ( h ( f t ) + 1 ) = O ( λ ( t ) ) O(h(f_t)+1)=O(\lambda(t)) O ( h ( f t ) + 1 ) = O ( λ ( t )) time (Lemmas 6.1 and 6.3 ).
Add (g g g ). The blocks of x t x_t x t at the missing scales are added from the highest to the lowest. The region Q Q Q of line 3 is the region of x t x_t x t , which the walk has passed, for the first of them, and the region of the block added just before for the others. The regions Q L Q_L Q L and Q R Q_R Q R of line 5 follow from the endpoints of Q Q Q and of g g g .
ChangeRegions. Besides its splits and joins, a call must keep the stored keys and the three arrays right. The arrays first and last change at the endpoints of the at most three old and three new regions, and leaf never changes, because splits and joins neither create nor destroy leaves; a cut starts at leaf[y y y ]. For the keys, recall that an internal node has the key of the last leaf in its left subtree: it separates two consecutive leaves and has the key of the first. Three facts keep the keys right in constant time per node.
A split at the leaf of a key y y y . It rebuilds its two trees by joins up its path. The new node at a path node separates the same two leaves, so it takes the key of the path node. Two path nodes are not needed for this, the lowest one at which the path turns right and the lowest one at which it turns left, with the key before y y y and the key y y y : they separate the leaf of y y y from its two neighbours. Their keys are the keys of the new nodes when a cut joins the leaf of y y y back.
The total and the initialization. Since λ ( t ) ≤ K \lambda(t) \le K λ ( t ) ≤ K , every search takes O ( log n ) O(\log n) O ( log n ) time, and all searches together take O ( m + U B ( X ) ) O(m+\mathrm{UB}(X)) O ( m + UB ( X )) time by Theorem 7.3. The initial inner tree is built by joining the leaves in pairs, the resulting trees in pairs, and so on; for every j ≥ 1 j \ge1 j ≥ 1 there are at most ⌈ n / 2 j ⌉ \lceil n/2^j\rceil ⌈ n / 2 j ⌉ joins of trees with at most 2 j 2^j 2 j keys, each in O ( j ) O(j) O ( j ) time, so this takes
∑ j ≥ 1 ⌈ n 2 j ⌉ ⋅ O ( j ) = O ( n ) \sum_{j\ge1}\left\lceil\frac{n}{2^j}\right\rceil\cdot O(j)=O(n) j ≥ 1 ∑ ⌈ 2 j n ⌉ ⋅ O ( j ) = O ( n )
time, where the sum ends at j = K j=K j = K . The arrays, the counters and the lists take O ( n ) O(n) O ( n ) time and words.
Insertions and deletions: the algorithm and the proof
This appendix proves Theorem 10.1 . The algorithm is that of § 4 , with the blocks and the time of a block changed as in § 10.2 , and the proof has the same three parts as for searches. First, the repairs keep every block at the right size, and they are rare (Section B.1). Second, the set F \mathcal{F} F that the algorithm maintains is always the set of the kept blocks, both after an access and after a repair, so that the procedures of § 4 can be applied (Sections B.2 and B.3). Third, the cost is bounded, and compared with the unified bound (Section B.4). With N k N_k N k the number of blocks of level k k k that the repairs create, the total cost will be
∑ t = 1 m O ( λ ( t ) ) ⏟ Lemma B.5 + ∑ k O ( k + 1 ) N k ⏟ repairs, Lemma B.5 = O ( m + UB ( X ) ) . \underbrace{\sum_{t=1}^{m} O(\lambda(t))}_{\text{Lemma B.5}}+\underbrace{\sum_k O(k+1)N_k}_{\text{repairs, Lemma B.5}}=O(m+\operatorname{UB}(X)). Lemma B.5 t = 1 ∑ m O ( λ ( t )) + repairs, Lemma B.5 k ∑ O ( k + 1 ) N k = O ( m + UB ( X )) .
where the first sum is O ( m + UB ( X ) ) O(m+\operatorname{UB}(X)) O ( m + UB ( X )) by Lemma B.6 and the second is O ( m ) O(m) O ( m ) by Lemma B.1 . Here λ ( t ) \lambda(t) λ ( t ) is the locality scale of operation t t t , defined in Definition B.3 as for searches.
Until the proof of Theorem 10.1 we assume that S 1 S_1 S 1 is empty; the proof reduces the general case to this one.
What we use from the earlier sections. The tree is built from the kept blocks as in § 4.3 : the owner of a key is the lowest kept block that contains it, a region is a maximal interval of keys with one owner, the regions are the leaves of the region tree, where a region of height a a a has the weight 4 − a 4^{-a} 4 − a , and the keys of each region form an inner tree. Four facts are used.
Changing regions (Lemma 6.4 ). Under the same conditions, a call of ChangeRegions \operatorname{ChangeRegions} ChangeRegions that replaces at most three consecutive regions by at most three regions with the same union costs O ( a + 1 ) O(a+1) O ( a + 1 ) , where a a a is the largest height of these regions.
Retiring and adding (Lemma 5.2). Retire \operatorname{Retire} Retire removes a kept block g g g , and Add \operatorname{Add} Add adds a block g g g to the kept blocks. Both require that g g g contain no kept block of a lower height. Then each makes one call of ChangeRegions \operatorname{ChangeRegions} ChangeRegions , for regions owned by g g g and by its parent p p p , the lowest kept block above g g g , so each costs O ( h ( p ) + 1 ) O(h(p)+1) O ( h ( p ) + 1 ) .
Chains (Lemma 7.1 ). Let c ≥ 2 c\ge2 c ≥ 2 , let t t t be a time with a partner and τ t ≤ J − c \tau_t\le J-c τ t ≤ J − c , and let v < t v<t v < t be a time with v = 0 v=0 v = 0 or ρ ( v , t ) ≥ 2 J − 1 \rho(v,t)\ge2^{J-1} ρ ( v , t ) ≥ 2 J − 1 , such that every time r r r between v v v and t t t with τ r < J − c \tau_r<J-c τ r < J − c has a partner p ( r ) < r p(r)<r p ( r ) < r with ρ ( p ( r ) , r ) ≤ 2 τ r − 2 \rho(p(r),r)\le2^{\tau_r-2} ρ ( p ( r ) , r ) ≤ 2 τ r − 2 . The chain of t t t follows partners backwards, s 0 : = p ( t ) s_0:=p(t) s 0 := p ( t ) and s i + 1 : = p ( s i ) s_{i+1}:=p(s_i) s i + 1 := p ( s i ) , until the numbers 2 τ s i 2^{\tau_{s_i}} 2 τ s i of its times sum to at least 2 J − c 2^{J-c} 2 J − c . Then the chain is defined and lies after v v v , and
ρ ( s , t ) < P ( s ) < 2 J − c + 1 for every time s of the chain , ∑ i min ( 1 , 2 τ s i + c − J ) ≥ 1 , \rho(s,t)<P(s)<2^{J-c+1}\quad\text{for every time }s\text{ of the chain},\qquad\sum_i\min(1,2^{\tau_{s_i}+c-J})\ge1, ρ ( s , t ) < P ( s ) < 2 J − c + 1 for every time s of the chain , i ∑ min ( 1 , 2 τ s i + c − J ) ≥ 1 ,
where P ( s ) P(s) P ( s ) is the sum of 2 τ 2^{\tau} 2 τ and of the numbers 2 τ 2^\tau 2 τ of the times of the chain later than s s s .
Blocks
This subsection defines the blocks and the repairs that keep their sizes in range, and shows that the repairs are rare. A level is a power of two, and a k : = 8 k a_k := 8^k a k := 8 k . The levels take the place of the promoted scales of § 4 : a block of level k k k will have about 8 k = 2 3 k 8^k = 2^{3k} 8 k = 2 3 k keys, so it plays the part of a block of height 3 k 3k 3 k . The base 8 leaves room for a factor 4 in the sizes of the blocks and a factor 3 in the count of Lemma B.2 . For every level k k k , the blocks of level k k k partition the current set S S S into intervals of consecutive keys of S S S , and every block of level 2 k 2k 2 k is a union of blocks of level k k k , its children ; the children of a block of level 1 are its keys. A level is trivial if it has a single block, which then consists of all keys; like the root of a B-tree, this block may have fewer keys than the others. The levels above a trivial level are trivial, and the lowest level that is trivial is the top level K ^ \hat K K ^ . Every block g g g of a level k k k satisfies
a k ≤ ∣ g ∣ ≤ 4 a k if level k is not trivial , ∣ g ∣ ≤ 4 a k if it is. (25) a_k \le|g| \le4a_k \quad\text{if level } k \text{ is not trivial}, \qquad|g| \le4a_k \quad\text{if it is.} \tag*{(25)} a k ≤ ∣ g ∣ ≤ 4 a k if level k is not trivial , ∣ g ∣ ≤ 4 a k if it is. (25)
Two blocks of different levels can consist of the same keys; we still regard them as different blocks. A block of level k k k has the height 3 k 3k 3 k . If K ^ ≥ 2 \hat K \ge2 K ^ ≥ 2 , the level K ^ / 2 \hat K/2 K ^ /2 has two blocks of at least a K ^ / 2 a_{\hat K/2} a K ^ /2 keys, so K ^ = O ( log ( ∣ S ∣ + 2 ) ) \hat K = O(\log(|S| + 2)) K ^ = O ( log ( ∣ S ∣ + 2 )) .
Initially every level has one empty block; only the levels up to the top level ever matter. An inserted key x x x joins the block of level 1 of its anchor , which is its successor, or the largest key if x x x has no successor, and x x x has the blocks of its anchor at all levels. If there is no key, x x x joins the empty blocks. A deleted key leaves its blocks. After an insertion or a deletion of x x x , the levels are repaired in increasing order; at level k k k only the block g g g that contains or contained x x x can violate (25) .
Split. If ∣ g ∣ > 4 a k |g| > 4a_k ∣ g ∣ > 4 a k , then g g g is split into two blocks, between two of its children and as evenly as possible, at the left one of two such places.
Merge. If ∣ g ∣ < a k |g| < a_k ∣ g ∣ < a k and level k k k is not trivial, then g g g is merged with a neighbouring block of level k k k in the same block of level 2 k 2k 2 k , the left one if there are two, and the merged block is split if it has more than 3 a k 3a_k 3 a k keys.
A split or a merge destroys the blocks that it is applied to and creates new blocks; all other blocks remain the same blocks.
For example, a block of level 1 has between 8 and 32 keys, unless it is the only one. Its 33rd key splits it into blocks of 16 and 17 keys, and neither of them becomes too large or too small before it has gained 16 keys or lost 9. This is why splits and merges are rare.
The next lemma is what the analysis needs from the repairs: the sizes stay in range, and the number of blocks that the repairs create at level k k k falls like 8 − k 8^{-k} 8 − k , so that repairs costing O ( k + 1 ) O(k+1) O ( k + 1 ) each cost O ( m ) O(m) O ( m ) in all.
Lemma B.1 (Blocks). After every operation, the blocks satisfy (25) , and every block of level 2 k 2k 2 k is a union of blocks of level k k k . If N k N_k N k is the number of blocks of level k k k that the splits and merges create, then ∑ k ( k + 1 ) N k = O ( m ) \sum_k (k+1)N_k = O(m) ∑ k ( k + 1 ) N k = O ( m ) .
Proof. A block is repaired as soon as its size leaves the range (25) , and the repair creates blocks well inside the range, which must gain or lose many keys before they are repaired again.
Sizes. Consider a repair at level k k k , of a block g g g . Three facts determine the sizes of the new blocks.
Children are small. A child of a block of level k k k has at most a k / 2 a_k/2 a k /2 keys: it is a key, or a block of level k / 2 k/2 k /2 with at most 4 a k / 2 ≤ a k / 2 4a_{k/2} \le a_k/2 4 a k /2 ≤ a k /2 keys. So the two parts of a split, which cuts between two children as evenly as possible, differ by at most a k / 2 a_k/2 a k /2 keys.
A merge has a neighbour. Let p p p be the block of level 2 k 2k 2 k around g g g . If level 2 k 2k 2 k is not trivial, then ∣ p ∣ ≥ a 2 k − 1 > ∣ g ∣ |p|\ge a_{2k}-1>|g| ∣ p ∣ ≥ a 2 k − 1 > ∣ g ∣ , because level 2 k 2k 2 k is repaired after level k k k , so that p p p has lost at most the one key of the operation. If level 2 k 2k 2 k is trivial, p p p consists of all keys, and level k k k has a block other than g g g . In both cases g g g has a neighbour r r r of level k k k inside p p p . The operation has not changed r r r , so a k ≤ ∣ r ∣ ≤ 4 a k a_k\le|r|\le4a_k a k ≤ ∣ r ∣ ≤ 4 a k .
Hence the new blocks have the following numbers of keys.
parts of a split of g : 1 2 ( 4 a k + 1 ) ± 1 4 a k , 7 4 a k to 9 4 a k + 1 , a merged block : ( a k − 1 ) + ∣ r ∣ , 2 a k − 1 to 5 a k − 1 , parts of a merged block that is split : half of it ± 1 4 a k , 5 4 a k to 11 4 a k . \begin{aligned}
\text{parts of a split of }g &: \quad\frac{1}{2}(4a_k+1)\pm\frac{1}{4}a_k, && \frac{7}{4}a_k\text{ to }\frac{9}{4}a_k+1,\\
\text{a merged block}: && (a_k-1)+|r|, && 2a_k-1\text{ to }5a_k-1,\\
\text{parts of a merged block that is split}: && \text{half of it}\pm\frac{1}{4}a_k, && \frac{5}{4}a_k\text{ to }\frac{11}{4}a_k.
\end{aligned} parts of a split of g a merged block : parts of a merged block that is split : : 2 1 ( 4 a k + 1 ) ± 4 1 a k , ( a k − 1 ) + ∣ r ∣ , half of it ± 4 1 a k , 4 7 a k to 4 9 a k + 1 , 2 a k − 1 to 5 a k − 1 , 4 5 a k to 4 11 a k .
The last column gives the least and the largest possible size. A merged block is split only if it has more than 3 a k 3a_k 3 a k keys, so one that is not split has at most 3 a k 3a_k 3 a k keys. So all new blocks satisfy (25) . The nesting is kept, because a split cuts between two children and a merge stays inside one block of level 2 k 2k 2 k .
Count. By the three lines above, the size of a block of level k k k satisfies
when b is created and not split at once : 5 4 a k ≤ ∣ b ∣ ≤ 3 a k , when b is the block g of a repair : ∣ b ∣ ∈ { a k − 1 , 4 a k + 1 } . \begin{aligned}
\text{when }b\text{ is created and not split at once}:&\quad\frac{5}{4}a_k\le|b|\le3a_k,\\
\text{when }b\text{ is the block }g\text{ of a repair}:&\quad|b|\in\{a_k-1,\ 4a_k+1\}.
\end{aligned} when b is created and not split at once : when b is the block g of a repair : 4 5 a k ≤ ∣ b ∣ ≤ 3 a k , ∣ b ∣ ∈ { a k − 1 , 4 a k + 1 } .
So more than a k / 4 a_k/4 a k /4 insertions and deletions change the size of b b b in between. The same holds for the empty block of the level at the start, which is repaired only at 4 a k + 1 4a_k+1 4 a k + 1 keys. An insertion or a deletion changes the size of one block of level k k k . Every repair has one block g g g , where a merge and the split of its merged block count as one repair, and a repair creates at most three blocks. Hence
# { repairs at level k } < m a k / 4 = 4 m 8 k , N k ≤ 12 m 8 k , ∑ k ( k + 1 ) N k ≤ 12 m ∑ k ≥ 1 k + 1 8 k = O ( m ) . \#\{\text{repairs at level }k\}<\frac{m}{a_k/4}=\frac{4m}{8^k},\qquad N_k\le\frac{12m}{8^k},\qquad\sum_k(k+1)N_k\le12m\sum_{k\ge1}\frac{k+1}{8^k}=O(m). # { repairs at level k } < a k /4 m = 8 k 4 m , N k ≤ 8 k 12 m , k ∑ ( k + 1 ) N k ≤ 12 m k ≥ 1 ∑ 8 k k + 1 = O ( m ) .
The time of a block
The tree keeps, at each level, the blocks whose keys were used most recently. This subsection defines when a key is used and the time of a block, its recency , and proves the four facts about recencies on which the rest relies.
Throughout, a statement about the time t t t refers to the moment before operation t t t . The blocks of x t x_t x t are the blocks that contain x t x_t x t , or for an insertion the blocks that x t x_t x t joins. Operation t t t accesses one key, unless it deletes the only key. A search or an insertion accesses x t x_t x t . A deletion accesses the heir of x t x_t x t : its predecessor if this key lies in the block of level 1 of x t x_t x t , and its successor otherwise. One of the two lies there, because a block of level 1 has at least 8 keys or consists of all keys. So the accessed key lies in the blocks of x t x_t x t and is present after the operation. The access time acc ( y ) \operatorname{acc}(y) acc ( y ) of a present key y y y is the time of the last operation that accessed y y y since y y y was last inserted, and 0 before the first such operation, and the recency of a block g g g is
θ ( g ) : = max { acc ( y ) : y ∈ g } , \theta(g):=\max\{\operatorname{acc}(y):y\in g\}, θ ( g ) := max { acc ( y ) : y ∈ g } ,
or 0 if g g g is empty. While S 1 S_1 S 1 is empty, every present key was inserted, and its insertion accessed it, so every block that is not empty has a positive recency. The heir is what keeps a recency from falling. Suppose that a deletion accessed nothing, and that the last operations deleted keys of one block. Then the block would have the recency of some older key, as if these operations had never touched it, and the tree would have to stop keeping a block that is in use.
The next lemma collects these facts. Parts 1 and 2 say that the blocks of a level are ranked by their recency as in a cache with the least-recently-used rule, and that the ranking is compatible with the nesting of the levels. Part 3 bounds the number of blocks that compete with a recently used block, which keeps the block of x t x_t x t in the tree at the levels from λ ( t ) / 3 \lambda(t)/3 λ ( t ) /3 on. Part 4 says that the borders of a block move only with the block itself, which is what the comparison with the unified bound needs.
Lemma B.2 (Recency).
Part 3 (few recent blocks). Let v < t v < t v < t . Before operation t t t , at most 3 ρ ( v , t ) 3\rho(v,t) 3 ρ ( v , t ) blocks of a level have a recency larger than v v v .
Part 4 (keys stay on their side). Let g g g be a block that exists and is not empty from a time s s s to a time t t t , and let y y y be a key that is present from s s s to t t t . Then y y y is in g g g at all of these times or at none. In the second case, y y y is smaller than all keys of g g g at all of these times, or larger than all keys of g g g at all of these times.
Proof. Part 1. A maximum over more keys is not smaller. An operation accesses one key, so two present keys with positive access times have different access times, and the blocks of a level are disjoint.
Part 2. Let y y y be the key that operation t t t accesses. The operation changes the access times and the blocks in two ways only. It gives the access time t t t to y y y , which lies in the blocks of x t x_t x t and is present afterwards. And x t x_t x t joins or leaves its blocks, if the operation is an insertion or a deletion. So a block of x t x_t x t has the recency t t t afterwards, because it contains y y y and no access time exceeds t t t . Another block that exists before and after the operation has the same keys with the same access times.
Part 3. A block with a recency after v v v holds a key that was accessed after v v v , and different blocks hold different keys. An accessed key is the key of an operation, or the heir of a deleted key, which is the nearest remaining key on one side of it. So every key of an operation accounts for at most three accessed keys: itself and one on each side. Precisely, let
D : = { x r : v < r < t } , so that ∣ D ∣ = ρ ( v , t ) , D := \{x_r : v < r < t\}, \qquad\text{so that } |D| = \rho(v,t), D := { x r : v < r < t } , so that ∣ D ∣ = ρ ( v , t ) ,
and for z ∈ D z \in D z ∈ D let L ( z ) L(z) L ( z ) and R ( z ) R(z) R ( z ) be the next smaller and the next larger key than z z z among the keys outside D D D that are present before operation v + 1 v+1 v + 1 . Let b b b be a block of the level with θ ( b ) > v \theta(b) > v θ ( b ) > v , and let y b ∈ b y_b \in b y b ∈ b be the key that was accessed at the time u : = θ ( b ) u := \theta(b) u := θ ( b ) , where v < u < t v < u < t v < u < t . We show that
y b ∈ D ∪ { L ( z ) , R ( z ) : z ∈ D } . (26) y_b \in D \cup\{L(z), R(z) : z \in D\}. \tag*{(26)} y b ∈ D ∪ { L ( z ) , R ( z ) : z ∈ D } . (26)
The set on the right has at most 3 ∣ D ∣ 3|D| 3∣ D ∣ keys, and different blocks b b b have different keys y b y_b y b , which proves Part 3. For (26) , suppose that y b ∉ D y_b \notin D y b ∈ / D . Then operation u u u is a deletion and y b y_b y b is the heir of x u ∈ D x_u \in D x u ∈ D , a neighbour of x u x_u x u in S u S_u S u . No operation strictly between v v v and t t t inserts or deletes a key outside D D D . So
{ keys outside D that are present before operation v + 1 } = S u ∖ D , \{\text{keys outside }D\text{ that are present before operation }v+1\}=S_u\setminus D, { keys outside D that are present before operation v + 1 } = S u ∖ D ,
and y b y_b y b belongs to this set. A neighbour of x u x_u x u in S u S_u S u that lies in S u ∖ D S_u\setminus D S u ∖ D is the nearest key of S u ∖ D S_u\setminus D S u ∖ D on its side of x u x_u x u , so y b = L ( x u ) y_b=L(x_u) y b = L ( x u ) or y b = R ( x u ) y_b=R(x_u) y b = R ( x u ) .
Part 4. An inserted key joins the block of the next larger key, so it cannot push a present key across the border of a block. Precisely, a present key enters or leaves g g g only when g g g is created or destroyed, so y y y is in g g g at all of these times or at none. In the second case, let z z z be a key that is inserted between s s s and t t t , with anchor q q q .
y < min g y<\min g y < min g and z < y z<y z < y . Then q ≤ y q\le y q ≤ y , so q ∉ g q\notin g q ∈ / g , and z z z does not join g g g .
y > max g y>\max g y > max g and z > y z>y z > y . Then q > y q>y q > y , or q q q is the largest key, so again q ∉ g q\notin g q ∈ / g , and z z z does not join g g g .
Otherwise z z z lies on the same side of y y y as g g g , so it does not change the side of y y y even if it joins g g g .
A deletion does not change the side of y y y either.
Definition B.3 (Locality scale of an operation). Let ν k ( t ) \nu_k(t) ν k ( t ) be the recency of the block of x t x_t x t of level k k k before operation t t t . The locality scale of operation t t t is λ ( t ) : = 3 k \lambda(t):=3k λ ( t ) := 3 k for the least level k k k with
ν k ( t ) ≥ 1 and ρ ( ν k ( t ) , t ) + 2 ≤ 8 k , \nu_k(t)\ge1 \qquad\text{and}\qquad\rho(\nu_k(t),t)+2\le8^k, ν k ( t ) ≥ 1 and ρ ( ν k ( t ) , t ) + 2 ≤ 8 k , and λ ( t ) : = 3 \lambda(t):=3 λ ( t ) := 3 if S t S_t S t is empty.
In words, λ ( t ) / 3 \lambda(t)/3 λ ( t ) /3 is the least level at which the block of x t x_t x t was accessed recently, where recently means that at most 8 k − 2 8^k-2 8 k − 2 distinct keys had an operation since. This is Definition 4.1 with the levels in place of the scales. If S t S_t S t is not empty, every trivial level satisfies the condition, because its block contains the key that operation t − 1 t-1 t − 1 accessed. So λ ( t ) \lambda(t) λ ( t ) is defined, and λ ( t ) ≤ 3 K ^ t \lambda(t)\le3\widehat{K}_t λ ( t ) ≤ 3 K t for the top level K ^ t \widehat{K}_t K t before operation t t t . By Part 1 of Lemma B.2 , the condition then holds at every level from λ ( t ) / 3 \lambda(t)/3 λ ( t ) /3 on.
The algorithm
This subsection defines the kept blocks, states the algorithm, and proves that the set F \mathcal{F} F that it maintains follows the kept blocks (Lemma B.4 ). An access changes the kept blocks as a search does in § 4 ; what is new are the repairs.
For a level k k k that is not trivial, the recency list of level k k k lists the blocks of level k k k with a positive recency, by decreasing recency; by Part 1 of Lemma B.2 their recencies are distinct. The kept blocks of level k k k are the first
C k : = 3 ⋅ 8 k C_k:=3\cdot8^k C k := 3 ⋅ 8 k
blocks of this list, or all of them if there are fewer. The block of a trivial level is kept at all times. A level is missing for operation t t t if the block of x t x_t x t of this level is not kept before the operation. The access then moves this block to the front of the recency list, so it becomes kept, and if C k C_k C k blocks were kept, the last of them is no longer kept; we call it the victim of the level. Owners, regions, the region tree, the inner trees, the realization and the weights are those of § 4.3 , for the keys of $S and the heights above, with the largest key at the last leaf. The blocks of the levels above the top level own no keys.
Algorithm 5 processes an operation. Its first three phases are those of Algorithm 1, with the access in place of the search. Line 14 and line 23 add or remove one leaf of an inner tree and change no recency. An insertion splits the inner tree at the leaf of the anchor q q q and joins the two resulting trees, the leaf of q q q and the new leaf, in key order, by three joins. A deletion splits the inner tree at the leaf of x t x_t x t and joins the two resulting trees. As for the recencies, an inserted key has the access time 0 until line 17, and a deleted key leaves after its heir has received the time t t t . A region is a union of blocks of level 1, so it does not become empty unless the set does, and the empty set has one region, whose inner tree has no leaf until a key is inserted. Only the levels up to the top level take part in an operation. The fourth phase repairs the blocks, and F \mathcal{F} F must follow.
A split. Let g g g be split into g 1 g_1 g 1 , which has the key that the operation accessed, and g 2 g_2 g 2 . The obvious repair is to retire g g g and to add its two parts. This does not work: RETIRE needs a block that contains no kept block of a lower level, and for k ≥ 2 k \ge2 k ≥ 2 the block g g g contains the lower blocks of the accessed key, which are kept. So g g g is not retired. Its two parts take its place in F \mathcal{F} F (line 28). A key that g g g owned is then owned by its part, at the same height, so the regions stay as they are, except that a region of g g g that reaches across the cut is divided there (line 29). It remains to bring F \mathcal{F} F back to the kept blocks. In the recency list of level k k k , the split has the effect
( g , b 2 , b 3 , … ) ⟶ ( g 1 , b 2 , … , b j , g 2 , b j + 1 , … ) : (g,b_2,b_3,\ldots)\longrightarrow(g_1,b_2,\ldots,b_j,g_2,b_{j+1},\ldots): ( g , b 2 , b 3 , … ) ⟶ ( g 1 , b 2 , … , b j , g 2 , b j + 1 , … ) :
the part g 1 g_1 g 1 takes the place of g g g at the front, and g 2 g_2 g 2 enters at the place of its own recency, or not at all if its recency is 0. The kept blocks are the first C k C_k C k blocks of the list. So at most one block of F \mathcal{F} F is no longer kept: g 2 g_2 g 2 , if it is not among the first C k C_k C k , or else the block that g 2 g_2 g 2 has pushed out of them. This block retires (line 30).
A merge. Let g g g be merged with r r r into g ^ \hat{g} g ^ . This is the mirror image. If r r r is not kept, it is added first (line 32), so that both blocks are in F \mathcal{F} F . Then g ^ \hat{g} g ^ takes their place (line 33), the keys that they owned are owned by g ^ \hat{g} g ^ , and their two regions at the seam, the border between g g g and r r r , become one (line 34). In the recency list,
( g , b 2 , … , b j , r , b j + 1 , … ) ⟶ ( g ^ , b 2 , … , b j , b j + 1 , … ) : (g,b_2,\ldots,b_j,r,b_{j+1},\ldots)\longrightarrow(\hat{g},b_2,\ldots,b_j,b_{j+1},\ldots): ( g , b 2 , … , b j , r , b j + 1 , … ) ⟶ ( g ^ , b 2 , … , b j , b j + 1 , … ) :
the block g ^ \hat{g} g ^ takes the place of g g g , and r r r is gone. So if r r r was among the first C k C_k C k blocks, the block after them moves up, and it is added (line 35). If the merged block is then split, this is a split as above.
The next lemma is the counterpart of Lemma 5.1. Part 1 is the nesting of the kept blocks, Part 2 bounds the missing levels, and Part 3 says that F \mathcal{F} F follows the kept blocks through accesses and repairs, so that RETIRE and ADD are always applied as Lemma 5.2 requires. Here F \mathcal{F} F is the set that Algorithm 5 maintains, and whether a block is kept is always judged by the blocks and the access times of the moment in question. Initially F \mathcal{F} F consists of the kept blocks, which are the empty blocks of all levels; only the levels up to the top level ever take part in an operation.
Algorithm 5 The online BST with insertions and deletions, procedure Operation. It replaces the procedure Search of Algorithm 1.
1: data structure OnlineBST ▷ continues Algorithm 1 2: procedure Operation(t t t ) ▷ operation t t t , with the key x t x_t x t 3: M ← \mathcal{M} \gets M ← the missing levels of operation t t t 4: /* Phase 1: make room at the missing levels / 5: for each k ∈ M k \in\mathcal{M} k ∈ M , by increasing k k k do 6: if F \mathcal{F} F has C k C_k C k blocks of level k k k then 7: Retire(the victim of the level k k k ) ▷ cost O ( k + 1 ) O(k+1) O ( k + 1 ) 8: end if 9: end for 10: / Phase 2: go to the key / 11: walk from the root to x t x_t x t , or along its search path in an insertion ▷ cost O ( λ ( t ) ) O(\lambda(t)) O ( λ ( t )) 12: if operation t t t is an insertion then 13: x t x_t x t joins the blocks of its anchor q q q 14: add the leaf of x t x_t x t to the inner tree of the region of q q q ▷ cost O ( λ ( t ) ) O(\lambda(t)) O ( λ ( t )) 15: end if 16: / Phase 3: access and promote / 17: give the access time t t t to x t x_t x t , or to its heir in a deletion 18: for each k ∈ M k \in\mathcal{M} k ∈ M , by decreasing k k k do 19: Add(the block of x t x_t x t of level k k k ) ▷ cost O ( λ ( t ) ) O(\lambda(t)) O ( λ ( t )) for all of them 20: end for 21: if operation t t t is a deletion then 22: x t x_t x t leaves its blocks 23: remove the leaf of x t x_t x t from the inner tree of its region ▷ cost O ( λ ( t ) ) O(\lambda(t)) O ( λ ( t )) 24: end if 25: / Phase 4: repair the blocks; every split or merge at level k k k costs O ( k + 1 ) O(k+1) O ( k + 1 ) */ 26: for each level k k k , by increasing k k k , and each split or merge at level k k k do 27: if it splits g g g into g 1 g_1 g 1 and g 2 g_2 g 2 then 28: F ← ( F ∖ { g } ) ∪ { g 1 , g 2 } \mathcal{F} \gets(\mathcal{F} \setminus\{g\}) \cup\{g_1,g_2\} F ← ( F ∖ { g }) ∪ { g 1 , g 2 } 29: ChangeRegions(the region that g g g owned across the cut, its two parts) ▷ if any 30: Retire(the block of level k k k in F \mathcal{F} F that is not kept) ▷ if there is one 31: else ▷ it merges g g g and r r r into g ^ \hat{g} g ^ 32: Add(r r r ) ▷ if r ∉ F r \notin\mathcal{F} r ∈ / F 33: F ← ( F ∖ { g , r } ) ∪ { g ^ } \mathcal{F} \gets(\mathcal{F} \setminus\{g,r\}) \cup\{\hat{g}\} F ← ( F ∖ { g , r }) ∪ { g ^ } 34: ChangeRegions(the two regions at the seam, their union) ▷ if g g g and r r r owned them 35: Add(the kept block of level k k k that is not in F \mathcal{F} F ) ▷ if there is one 36: end if 37: end for 38: end procedure 39: end data structure
Part 3 (the steps). The set F \mathcal{F} F consists of the kept blocks when an operation begins, after its third phase, and after the lines of every split or merge. Every block that Algorithm 5 retires or adds contains, at that moment, no block of F \mathcal{F} F of a lower level. At every moment, F \mathcal{F} F contains the blocks of the higher levels around each of its blocks and has at most C k + 1 C_k+1 C k + 1 blocks of each level k k k . In a split or a merge, the replacement of the blocks changes the regions only as line 29
and line 34 say.
Proof. As for searches, the kept blocks of each level form a cache with the least-recently-used rule, now of C k C_k C k blocks. An access changes the recency lists exactly as a search does in Lemma 5.1, so the first three phases need no new argument. A split or a merge at level k k k changes one recency list only locally, and as a result it changes the kept blocks of level k k k , apart from the blocks it destroys and creates, by at most one block. The lines of the repair replace the destroyed blocks and then retire or add this one block.
Part 1. Let g g g be a kept block of level k k k , and let g ′ g' g ′ be the block of a higher level k ′ k' k ′ around it. If level k ′ k' k ′ is trivial, g ′ g' g ′ is kept. Otherwise level k k k is not trivial either, so g g g is in its recency list, with θ ( g ) > 0 \theta(g)>0 θ ( g ) > 0 . For a block b b b with θ ( b ) > 0 \theta(b)>0 θ ( b ) > 0 , let
Pre ( b ) : = { c : c is a block of the level of b and θ ( c ) > θ ( b ) } \operatorname{Pre}(b):=\{c:c\text{ is a block of the level of }b\text{ and }\theta(c)>\theta(b)\} Pre ( b ) := { c : c is a block of the level of b and θ ( c ) > θ ( b )}
be the set of the blocks that precede b b b in its recency list, so that a block b b b of a level j j j that is not trivial is kept if and only if ∣ Pre ( b ) ∣ < C j |\operatorname{Pre}(b)|<C_j ∣ Pre ( b ) ∣ < C j . We show that g ′ g' g ′ has no more blocks before it than g g g has, by mapping Pre ( g ′ ) \operatorname{Pre}(g') Pre ( g ′ ) into Pre ( g ) \operatorname{Pre}(g) Pre ( g ) . For c ∈ Pre ( g ′ ) c\in\operatorname{Pre}(g') c ∈ Pre ( g ′ ) , let y c ∈ c y_c\in c y c ∈ c be the key with the access time θ ( c ) \theta(c) θ ( c ) , and let ι ( c ) \iota(c) ι ( c ) be the block of level k k k that contains y c y_c y c . It lies in c c c and contains y c y_c y c , so its recency is exactly θ ( c ) \theta(c) θ ( c ) , and by Part 1 of Lemma B.2 ,
θ ( ι ( c ) ) = θ ( c ) > θ ( g ′ ) ≥ θ ( g ) , \theta(\iota(c))=\theta(c)>\theta(g')\geq\theta(g), θ ( ι ( c )) = θ ( c ) > θ ( g ′ ) ≥ θ ( g ) ,
that is, ι ( c ) ∈ Pre ( g ) \iota(c)\in\operatorname{Pre}(g) ι ( c ) ∈ Pre ( g ) . The blocks of Pre ( g ′ ) \operatorname{Pre}(g') Pre ( g ′ ) are disjoint, so ι \iota ι is injective, and
∣ Pre ( g ′ ) ∣ ≤ ∣ Pre ( g ) ∣ < C k ≤ C k ′ . |\operatorname{Pre}(g')|\leq|\operatorname{Pre}(g)|<C_k\leq C_{k'}. ∣ Pre ( g ′ ) ∣ ≤ ∣ Pre ( g ) ∣ < C k ≤ C k ′ .
Since θ ( g ′ ) ≥ θ ( g ) > 0 \theta(g')\geq\theta(g)>0 θ ( g ′ ) ≥ θ ( g ) > 0 , the block g ′ g' g ′ is in its list, so it is kept.
Part 2. The first claim follows from Part 1. Let k ≥ λ ( t ) / 3 k\geq\lambda(t)/3 k ≥ λ ( t ) /3 be a level; we show that it is not missing. If it is trivial, the block of x t x_t x t of level k k k is kept. Otherwise S t S_t S t is not empty and level k k k satisfies the condition of Definition B.3 . So the block of x t x_t x t has the recency v k ( t ) ≥ 1 v_k(t)\geq1 v k ( t ) ≥ 1 , and by Part 3 of Lemma B.2 the number of blocks of level k k k that precede it in the recency list is at most
3 ρ ( v k ( t ) , t ) ≤ 3 ⋅ ( 8 k − 2 ) < C k . 3\rho(v_k(t),t)\leq3\cdot(8^k-2)<C_k. 3 ρ ( v k ( t ) , t ) ≤ 3 ⋅ ( 8 k − 2 ) < C k .
So the block of x t x_t x t is kept.
Part 3, the first three phases. If the operation deletes the only key, all levels are trivial before and after it, and F \mathcal{F} F does not change. Otherwise, by Part 2 of Lemma B.2 , the access moves the blocks of x t x_t x t to the front of their recency lists and changes nothing else; adding or removing the leaf of x t x_t x t changes no recency. So at a missing level the block of x t x_t x t becomes kept, and the victim ceases to be kept if C k C_k C k blocks were kept. These are the changes of Part 4 of Lemma 5.1, with the levels in place of the promoted scales, and the first and third phases make them as in Algorithm 1. The proof of Part 5 of Lemma 5.1 carries over word for word, with the following reading. The family F t \mathcal{F}_t F t is the set of the kept blocks before the access and F t + 1 \mathcal{F}_{t+1} F t + 1 the set after it; both are closed by Part 1. The set V V V consists of the victims of the missing levels, A A A of the blocks of x t x_t x t at the missing levels, and U U U of the other kept blocks. Facts (V) and (A) of that proof use only that F t \mathcal{F}_t F t and F t + 1 \mathcal{F}_{t+1} F t + 1 are closed, that F t + 1 = U ∪ A \mathcal{F}_{t+1}=U\cup A F t + 1 = U ∪ A , and that the blocks of x t x_t x t at the levels that are not missing are kept and are not victims, all of which hold here. That x t x_t x t joins or leaves its blocks during the operation changes neither which blocks exist nor how they are nested. So the proof gives the claims for these phases, with at most C k C_k C k blocks of level k k k .
Part 3, a split or a merge at level k k k . Let K − \mathcal{K}^{-} K − and K + \mathcal{K}^{+} K + be the sets of the kept blocks before and after the split or merge, which we call the event . The proof has five steps. (i) The kept blocks. We show that
split: K + ∪ E = ( K − ∖ { g } ) ∪ { g 1 , g 2 } , merge: K + = ( K − ∖ { g , r } ) ∪ { g ^ } ∪ E (27) \begin{aligned}
\text{split:}\quad\mathcal{K}^{+} \cup E &= \left(\mathcal{K}^{-} \setminus\{g\}\right) \cup\{g_{1},g_{2}\}, \qquad
\text{merge:}\quad\mathcal{K}^{+} = \left(\mathcal{K}^{-} \setminus\{g,r\}\right) \cup\{\hat{g}\} \cup E \tag*{(27)}
\end{aligned} split: K + ∪ E = ( K − ∖ { g } ) ∪ { g 1 , g 2 } , merge: K + = ( K − ∖ { g , r } ) ∪ { g ^ } ∪ E (27)
where E E E is a set of at most one block of level k k k , which is not kept after a split and not kept before a merge. Only blocks of level k k k are destroyed or created, so the recencies and the kept blocks of the other levels stay the same. At level k k k , the recencies change as follows.
The block g g g contains, or contained, x t x_t x t , so it contains the key that the operation accessed and is at the front of its list, with the recency t t t .
The new block that contains this key, g 1 g_1 g 1 or g ^ \hat{g} g ^ , has the recency t t t and takes the front.
The part g 2 g_2 g 2 has the largest access time of its keys, and every other block of level k k k keeps its recency.
This gives the two displays before the lemma. The kept blocks are the first C k C_k C k blocks of the list, so (27) holds with the following set E E E .
Split. If g 2 g_2 g 2 is not among the first C k C_k C k blocks of the new list, then E = { g 2 } E=\{g_2\} E = { g 2 } . Otherwise E E E consists of the block that g 2 g_2 g 2 has pushed out of them, if there is one.
Merge. If r r r was among the first C k C_k C k blocks, then E E E consists of the block that moves up into them, if there is one. Otherwise E = ∅ E=\varnothing E = ∅ .
At a trivial level, where there is no recency list, (27) holds with E = ∅ E=\varnothing E = ∅ : after a split of the only block, both parts are kept because C k ≥ 2 C_k \ge2 C k ≥ 2 , and a merge of the last two blocks, which are both kept, gives the only block of its level.
(ii) The set F \mathcal{F} F . By induction, F = K − \mathcal{F}=\mathcal{K}^{-} F = K − before the event. The lines of the event then change F \mathcal{F} F as follows, where r r r is added at line 32 only if r ∉ K − r\notin\mathcal{K}^{-} r ∈ / K − , and the last step is void if E = ∅ E=\varnothing E = ∅ :
split: K − → line 28 K + ∪ E → line 30: retire E K + , merge: K − → line 32 K − ∪ { r } → line 33 K + ∖ E → line 35: add E K + . (28) \begin{aligned}
\text{split:}\quad&\mathcal{K}^{-} \xrightarrow{\text{line 28}} \mathcal{K}^{+}\cup E \xrightarrow{\text{line 30: retire }E} \mathcal{K}^{+},\\
\text{merge:}\quad&\mathcal{K}^{-} \xrightarrow{\text{line 32}} \mathcal{K}^{-}\cup\{r\} \xrightarrow{\text{line 33}} \mathcal{K}^{+}\setminus E \xrightarrow{\text{line 35: add }E} \mathcal{K}^{+}. \tag*{(28)}
\end{aligned} split: merge: K − line 28 K + ∪ E line 30: retire E K + , K − line 32 K − ∪ { r } line 33 K + ∖ E line 35: add E K + . (28)
So F = K + \mathcal{F}=\mathcal{K}^{+} F = K + after the event. Each family in (28) has at most one block of level k k k more than K − \mathcal{K}^{-} K − or K + \mathcal{K}^{+} K + , so at most C k + 1 C_k+1 C k + 1 .
(iii) The blocks that are retired or added contain no block of F \mathcal{F} F of a lower level. These blocks are the block of E E E and r r r . Each of them is not kept at the moment in question: the block of E E E after a split is not in K + \mathcal{K}^{+} K + , and r r r and the block of E E E in a merge are not in K − \mathcal{K}^{-} K − . The blocks of F \mathcal{F} F of lower levels are kept, both before and after the event, because the lower levels do not change. A block that contains a kept block of a lower level is kept, by Part 1. So a block that is retired or added contains none.
(iv) Nesting. We show that every family in (28) contains, with each of its blocks, the blocks of the higher levels around it. For K − \mathcal{K}^{-} K − and K + \mathcal{K}^{+} K + this is Part 1. For the three others:
K + ∖ E \mathcal{K}^{+}\setminus E K + ∖ E arises from K + \mathcal{K}^{+} K + by removing a block that contains no block of the family of a lower level, by (iii).
K − ∪ { r } \mathcal{K}^{-}\cup\{r\} K − ∪ { r } and K + ∪ E \mathcal{K}^{+}\cup E K + ∪ E with E = { g 2 } E=\{g_2\} E = { g 2 } arise by adding r r r or g 2 g_2 g 2 . The block of level 2 k 2k 2 k around it is the block around g g g , which is kept because g g g is (Part 1), and so are the higher blocks around it.
K + ∪ E K^+ \cup E K + ∪ E , where the block of E E E was pushed out, arises by adding a block that was kept before the event, and so were the blocks around it.
In the last two cases the blocks around the added block are kept before the event, and the blocks of the levels above k k k do not change.
(v) The regions. The blocks g 1 g_1 g 1 , g 2 g_2 g 2 and g ^ \hat{g} g ^ have the height of g g g . So after a split, a key that g g g owned is owned by the part that contains it. After a merge, a key that g g g or r r r owned is owned by g ^ \hat{g} g ^ ; here we use that r ∈ F r \in\mathcal{F} r ∈ F before line 33. Every other key keeps its owner. Hence, after a split, the only change of the regions is that a region of g g g with keys in both parts is divided at the cut, and after a merge, that the two regions at the seam become one if g g g and r r r owned them.
By Part 3, the parent of a block of level k k k in F \mathcal{F} F is always the block of level 2 k 2k 2 k around it, of height 6 k 6k 6 k , as in (6) , and RETIRE and ADD are applied only as Lemma 5.2 requires.
The cost and the unified bound with updates
This subsection fills the two slots of the budget at the start of the appendix. Lemma B.5 bounds the cost of an operation by its locality scale and the levels of its repairs, and Lemma B.6 bounds the sum of the locality scales by the unified bound.
Lemma B.5 (Cost of an operation). Operation t t t costs O ( λ ( t ) ) O(\lambda(t)) O ( λ ( t )) for its first three phases and O ( k + 1 ) O(k+1) O ( k + 1 ) for every split or merge at a level k k k , and it costs O ( log ( n t + 2 ) ) O(\log(n_t+2)) O ( log ( n t + 2 )) in all.
Proof. The budget is that of a search in Theorem 4.4 , plus the leaf of x t x_t x t and the repairs:
∑ k missing O ( k + 1 ) + O ( λ ( t ) ) + O ( λ ( t ) ) + ∑ splits, merges O ( k + 1 ) . \sum_{\substack{k\ \text{missing}}} O(k+1) + O(\lambda(t)) + O(\lambda(t)) + \sum_{\text{splits, merges}} O(k+1). k missing ∑ O ( k + 1 ) + O ( λ ( t )) + O ( λ ( t )) + splits, merges ∑ O ( k + 1 ) .
The tools of § 6 apply, with regions at most five times as large. They need two bounds, on the total weight W W W of the regions and on the size of a region Q Q Q of height 3 k 3k 3 k :
W ≤ 2 ∑ k ( C k + 1 ) ⋅ 4 − 3 k ≤ 2 ∑ k 4 ⋅ 8 k ⋅ 4 − 3 k = 8 ∑ j ≥ 0 8 − 2 j < 2 , ∣ Q ∣ < 5 a k = 5 ⋅ 2 3 k . W \le2\sum_k(C_k+1)\cdot4^{-3k} \le2\sum_k 4\cdot8^k\cdot4^{-3k} = 8\sum_{j\ge0}8^{-2j}<2,\qquad|Q|<5a_k=5\cdot2^{3k}. W ≤ 2 k ∑ ( C k + 1 ) ⋅ 4 − 3 k ≤ 2 k ∑ 4 ⋅ 8 k ⋅ 4 − 3 k = 8 j ≥ 0 ∑ 8 − 2 j < 2 , ∣ Q ∣ < 5 a k = 5 ⋅ 2 3 k .
Here (a) is the proof of Lemma 6.2 , with the at most C k + 1 C_k+1 C k + 1 blocks of height 3 k 3k 3 k in F \mathcal{F} F (Part 3 of Lemma B.4 ), and (b) holds because Q Q Q lies in a block of level k k k , which has fewer than 5 a k 5a_k 5 a k keys at every moment, also when it is a merged block before its split. So Lemmas 6.3 and 6.4 hold with 5 ⋅ 2 a 5\cdot2^a 5 ⋅ 2 a in place of 2 a 2^a 2 a . Their proofs use only that the leaves of the region tree are the regions of one family of blocks before the call and of another after it, both of total weight less than 2 and with regions of the stated size, where the heights of the old regions are read in the first family and those of the new regions in the second. This also holds for the calls of CHANGEREGIONS in line 29 and line 34, with the family F \mathcal{F} F before and after line 28 or line 33. Hence a call of CHANGEREGIONS for regions of height at most a a a costs O ( a + 1 ) O(a+1) O ( a + 1 ) , and retiring or adding a block of level k k k costs O ( k + 1 ) O(k+1) O ( k + 1 ) , because its parent has the height 6 k 6k 6 k . Retirements and additions. The missing levels are powers of two below λ ( t ) / 3 \lambda(t)/3 λ ( t ) /3 (Part 2 of Lemma B.4 ), so they sum to less than λ ( t ) \lambda(t) λ ( t ) , and as in the proof of Lemma 6.5 these steps cost O ( λ ( t ) ) O(\lambda(t)) O ( λ ( t )) ).
The walk. The level λ ( t ) / 3 \lambda(t)/3 λ ( t ) /3 is not missing, and the first phase retires no block of x t x_t x t . So during the walk the region of x t x_t x t , or of its anchor q q q in an insertion, has a height of at most λ ( t ) \lambda(t) λ ( t ) , and its keys have depth O ( λ ( t ) ) O(\lambda(t)) O ( λ ( t )) . In an insertion no key lies between x t x_t x t and q q q , so a search for x t x_t x t and a search for q q q turn the same way at every node, and the search path of x t x_t x t is the path to the leaf of q q q in the expanded tree.
The leaf of x t x_t x t . This is the one step that changes the set of keys. The model of § 10.1 asks for a top set U U U that contains the search path of x t x_t x t in an insertion, and the node of x t x_t x t and that of its predecessor in a deletion. We take
U : = { nodes that the split and the joins visit } ∪ { nodes on the path from the root to the leaf of x t , or of q in an insertion } . U := \{\text{nodes that the split and the joins visit}\}
\cup\{\text{nodes on the path from the root to the leaf of }x_t,\text{ or of }q\text{ in an insertion}\}. U := { nodes that the split and the joins visit } ∪ { nodes on the path from the root to the leaf of x t , or of q in an insertion } .
where the path is in the expanded tree and starts at the root of the region tree. If the largest key is deleted, U U U also contains the path to the leaf of its predecessor, which becomes the last leaf. We check the size of U U U , that nothing outside U U U changes, and that U U U is as the model asks.
The set U U U has O ( λ ( t ) ) O(\lambda(t)) O ( λ ( t )) nodes. Adding or removing the leaf is a split and at most three joins in an inner tree of fewer than 5 ⋅ 2 λ ( t ) 5 \cdot2^{\lambda(t)} 5 ⋅ 2 λ ( t ) leaves, so it takes O ( λ ( t ) ) O(\lambda(t)) O ( λ ( t )) time and visits as many nodes. The paths have O ( λ ( t ) ) O(\lambda(t)) O ( λ ( t )) nodes by the bound on the walk; for the predecessor of the largest key this holds because it lies in the same region.
Nothing outside U U U changes, for two reasons. The split and the joins change children only at nodes that they visit. And a node holds the key of the last leaf in its left subtree, so the only nodes whose key changes are those that hold the key x t x_t x t or the key of its predecessor, and the node of a key lies above the leaf of this key, on the paths in U U U .
Finally, U U U is a top set by Lemma 3.2 , because the visited nodes form a top set of the inner tree and the paths contain all nodes above it. In an insertion it contains the search path of x t x_t x t . In a deletion it contains the node of x t x_t x t and the node of its predecessor, which separates the leaf of x t x_t x t from the leaf before it and so lies above both.
Repairs. A split or a merge at level k k k makes one call of ChangeRegions, for regions of height 3 k 3k 3 k , and retires or adds at most one block of level k k k : in a merge, if r r r is added at line 32, then r r r was not among the first C k C_k C k blocks, so E = ∅ E = \varnothing E = ∅ and line 35 adds nothing. Each of these costs O ( k + 1 ) O(k+1) O ( k + 1 ) .
For the bound on one operation, a block of a level k > K ^ t k > \widehat K_t k > K t has at most n t + 1 ≤ 4 a k n_t+1 \le4a_k n t + 1 ≤ 4 a k keys, so the repairs of operation t t t are at most one merge and one split at each level up to K ^ t \widehat K_t K t . These levels sum to less than 2 K ^ t 2\widehat K_t 2 K t , and λ ( t ) ≤ 3 K ^ t = O ( log ( n t + 2 ) ) \lambda(t) \le3\widehat K_t = O(\log(n_t+2)) λ ( t ) ≤ 3 K t = O ( log ( n t + 2 )) .
The next lemma is the counterpart of Lemma 7.2 , and its proof has the same plan.
Lemma B.6 (Block boundaries). ∑ t = 1 m λ ( t ) = O ( m + UB ( X ) ) \sum_{t=1}^{m}\lambda(t) = O(m+\operatorname{UB}(X)) ∑ t = 1 m λ ( t ) = O ( m + UB ( X )) .
Proof. We follow the proof of Lemma 7.2 : level by level, an operation with a large locality scale pays for itself or is charged to its chain of partners (Lemma 7.1 ), which stays near a border of its block, and every operation is on at most one chain per level. Two things are new. Operations in a block that a split or a merge has just created are paid for by Lemma B.1 . And the borders move, but a key never crosses the border of a block that survives (Part 4 of Lemma B.2 ).
Levels. By Definition B.3 , λ ( t ) = 3 k ′ \lambda(t)=3k' λ ( t ) = 3 k ′ for a level k ′ k' k ′ , and a power of two is one more than the sum of the smaller powers of two, so
∑ t = 1 m λ ( t ) = 3 m + 3 ∑ k k ⋅ # { t : λ ( t ) > 3 k } , (29) \sum_{t=1}^{m}\lambda(t)=3m+3\sum_k k\cdot\#\{t:\lambda(t)>3k\}, \tag*{(29)} t = 1 ∑ m λ ( t ) = 3 m + 3 k ∑ k ⋅ # { t : λ ( t ) > 3 k } , (29)
where k k k ranges over the levels. Fix a level k k k , let J : = 3 k J:=3k J := 3 k , and fix an operation t t t with λ ( t ) > J \lambda(t)>J λ ( t ) > J . Then S t S_t S t is not empty, and level k k k is not trivial before operation t t t , because a trivial level satisfies the condition of Definition B.3 . Let g g g be the block of x t x_t x t of level k k k before operation t t t , and v : = v k ( t ) v:=v_k(t) v := v k ( t ) its recency. The block g g g is not empty, so v ≥ 1 v\ge1 v ≥ 1 (Section B.2). One of three cases holds.
The operation pays for itself: τ t > J − 5 \tau_t > J - 5 τ t > J − 5 . (The constant 5 is the slack c c c with which Lemma 7.1 is applied below; it is chosen so that the chain stays within 3 ⋅ 2 J − 4 3 \cdot2^{J-4} 3 ⋅ 2 J − 4 keys of a border, less than half a block.) The levels k k k with 3 k < τ t + 5 3k < \tau_t + 5 3 k < τ t + 5 sum to less than 2 ( τ t + 5 ) / 3 2(\tau_t + 5)/3 2 ( τ t + 5 ) /3 , so these cases contribute O ( m + UB ( X ) ) O(m + \operatorname{UB}(X)) O ( m + UB ( X )) to (29) .
The block is new: a split or a merge of an operation later than v v v created g g g . Each block is in this case for at most one operation. Indeed, level k k k is not trivial before operation t t t , and it becomes trivial only by a merge of its last two blocks, which destroys g g g . An empty set has only trivial levels, so the set is not empty while g g g exists. So by Part 2 of Lemma B.2 the recency of g g g is at least t t t after operation t t t and for as long as g g g exists. This is later than the creation of g g g , so g g g is not new for any later operation. Hence, by Lemma B.1 , these cases contribute at most 3 ∑ k k N k = O ( m ) 3 \sum_k kN_k = O(m) 3 ∑ k k N k = O ( m ) .
It remains to charge the crossings. In a crossing, x t x_t x t is close to the key of an operation that is not long ago, and yet its block g g g has not been accessed for a long time. This is possible only if a border of g g g lies between the two keys; Figure 4 shows the situation for searches, where the border is fixed; here it is the border of a block, and it moves with the keys. We charge the crossing to earlier operations near this border, and we show that no operation is charged twice at one level.
For every operation r r r whose term is attained by a witness we fix one such witness u u u , its partner ; then ρ ( u , r ) ≤ 2 τ r − 2 \rho(u,r) \le2^{\tau_r} - 2 ρ ( u , r ) ≤ 2 τ r − 2 , because τ r = τ ( u , r ) \tau_r = \tau(u,r) τ r = τ ( u , r ) . Fix a crossing t t t of level k k k , with g g g and v v v as above. Since λ ( t ) > J \lambda(t) > J λ ( t ) > J and v ≥ 1 v \ge1 v ≥ 1 , Definition B.3 gives
ρ ( v , t ) ≥ 2 J − 1. (30) \rho(v,t) \ge2^J - 1. \tag*{(30)} ρ ( v , t ) ≥ 2 J − 1. (30)
Three more facts hold for the crossing.
Large blocks. From the end of operation v v v to operation t t t , every block of level k k k has at least 2 J 2^J 2 J keys. Indeed, level k k k is not trivial at any of these times: otherwise g g g would be its only block, and it would have to be split for the level to become non-trivial again. So (25) gives at least a k = 2 J a_k = 2^J a k = 2 J keys.
Partners. Every operation r r r with v < r ≤ t v < r \le t v < r ≤ t and τ r ≤ J − 5 \tau_r \le J - 5 τ r ≤ J − 5 has a partner. Indeed, the set has at least 2 J 2^J 2 J keys at these times, so τ r ≤ J − 5 < log 2 ( n r + 2 ) \tau_r \le J - 5 < \log_2(n_r + 2) τ r ≤ J − 5 < log 2 ( n r + 2 ) , and the term of r r r is attained by a witness.
The chain. By Lemma 7.1 with c = 5 c = 5 c = 5 , whose hypotheses are (30) and the fact about partners, the chain s 0 > ⋯ > s i ∗ s_0 > \cdots> s_{i_*} s 0 > ⋯ > s i ∗ of t t t is defined and later than v v v , and every operation s s s of the chain satisfies
ρ ( s , t ) < P ( s ) < 2 J − 4 . (31) \rho(s,t) < P(s) < 2^{J-4}. \tag*{(31)} ρ ( s , t ) < P ( s ) < 2 J − 4 . (31)
Recall that P ( s ) P(s) P ( s ) is the sum of the numbers 2 τ 2^{\tau} 2 τ of t t t and of the operations of the chain that are later than s s s , and that the chain ends when the numbers of its operations reach 2 J − 5 2^{J-5} 2 J − 5 .
Three facts about the chain remain.
The partner of t t t lies across a border of g g g . Operation u : = s 0 u := s_0 u := s 0 accessed x u x_u x u , which is present until t t t , so
acc ( x u ) ≥ u > v = θ ( g ) , and hence x u ∉ g . \operatorname{acc}(x_u) \ge u > v = \theta(g), \qquad\text{and hence} \qquad x_u \notin g. acc ( x u ) ≥ u > v = θ ( g ) , and hence x u ∈ / g .
Since d t ( x u , x t ) < 2 J − 5 d_t(x_u,x_t) < 2^{J-5} d t ( x u , x t ) < 2 J − 5 and every block of level k k k has at least 2 J 2^J 2 J keys, x u x_u x u lies in a block next to g g g . The border of g g g on this side is the cut of the crossing. It has two properties.
The cut exists as long as g g g does. At all these times g g g has a block next to it on this side. Indeed, if g g g were the last block of its level on this side at some time, it would remain the last one, because a key that is inserted beyond the last block of a level joins this block; but at time t t t the key x u x_u x u lies beyond g g g .
The chain stays near the cut. The keys of the chain form a path of short hops that starts next to the cut, so they cannot go far from it. Precisely, for every operation s s s of the chain,
# { keys of S s between x s and the cut at time s } < 3 P ( s ) < 3 ⋅ 2 J − 4 . (32) \#\{\text{keys of } S_s \text{ between } x_s \text{ and the cut at time } s\} < 3P(s) < 3 \cdot2^{J-4}. \tag*{(32)} # { keys of S s between x s and the cut at time s } < 3 P ( s ) < 3 ⋅ 2 J − 4 . (32)
To see this, let H H H be the set of all keys, present or not, from the smallest to the largest of x t x_t x t and the keys of the chain down to x s x_s x s , and let w w w be the one of x t , x u x_t,x_u x t , x u that lies on the other side of the cut than x s x_s x s , where the side of x s x_s x s is taken at time s s s and the sides of x t x_t x t and x u x_u x u at time t t t . A key of S s S_s S s between x s x_s x s and the cut is deleted between s s s and t t t , or it stays on its side of the cut until t t t and so lies between x s x_s x s and w w w , in H H H . So the number of these keys is at most
# { keys deleted between s and t } ⏟ < P ( s ) (a) + # ( S s ∩ H ) ⏟ < 2 P ( s ) (b) . \underbrace{\#\{\text{keys deleted between }s\text{ and }t\}}_{<P(s)\ \text{(a)}}+\underbrace{\#(S_s\cap H)}_{<2P(s)\ \text{(b)}}. < P ( s ) (a) # { keys deleted between s and t } + < 2 P ( s ) (b) # ( S s ∩ H ) .
(a) Every deleted key is the key of an operation between s s s and t t t , and ρ ( s , t ) < P ( s ) \rho(s,t) < P(s) ρ ( s , t ) < P ( s ) by (31) .
(b) The set H H H is covered by the hops of the chain: the intervals between x r x_r x r and x r ′ x_{r'} x r ′ , where r r r is t t t or an operation of the chain later than s s s , and r ′ r' r ′ is its partner. A hop has at most d r ( x r ′ , x r ) + 1 < 2 τ r d_r(x_{r'},x_r)+1 < 2^{\tau_r} d r ( x r ′ , x r ) + 1 < 2 τ r keys of S r S_r S r , and these bounds sum to P ( s ) P(s) P ( s ) . The keys of S s S_s S s in a hop that are not in S r S_r S r are deleted between s s s and t t t , and there are fewer than P ( s ) P(s) P ( s ) of these in all, by (a).
An operation is on one chain per level. Suppose that an operation s s s is on the chains of two crossings t 1 < t 2 t_1 < t_2 t 1 < t 2 of level k k k , with blocks g 1 , g 2 g_1,g_2 g 1 , g 2 and partners u 1 , u 2 u_1,u_2 u 1 , u 2 . We show that a key of g 2 g_2 g 2 is accessed at the time s s s or later, and before t 2 t_2 t 2 . By Part 2 of Lemma B.2 , the recency of g 2 g_2 g 2 before operation t 2 t_2 t 2 is then at least s s s . This recency is v k ( t 2 ) v_k(t_2) v k ( t 2 ) , and the chain of t 2 t_2 t 2 lies after it, so
s ≤ θ ( g 2 ) = v k ( t 2 ) < s , s \le\theta(g_2)=v_k(t_2)<s, s ≤ θ ( g 2 ) = v k ( t 2 ) < s ,
a contradiction.
First, the two cuts are the same border. Both blocks exist at time s s s , since v k ( t 1 ) < s v_k(t_1)<s v k ( t 1 ) < s and v k ( t 2 ) < s v_k(t_2)<s v k ( t 2 ) < s . By (32) , each cut has fewer than 3 ⋅ 2 J − 4 3\cdot2^{J-4} 3 ⋅ 2 J − 4 keys of S s S_s S s between itself and x s x_s x s , so
# { keys of S s between the two cuts } < 2 ⋅ 3 ⋅ 2 J − 4 + 1 < 2 J . \#\{\text{keys of }S_s\text{ between the two cuts}\}<2\cdot3\cdot2^{J-4}+1<2^J. # { keys of S s between the two cuts } < 2 ⋅ 3 ⋅ 2 J − 4 + 1 < 2 J .
Two different borders have a whole block of level k k k between them, which has at least 2 J 2^J 2 J keys. So the cuts are one border. Hence g 1 = g 2 g_1=g_2 g 1 = g 2 , or g 1 g_1 g 1 and g 2 g_2 g 2 are the two blocks at this border. In the second case they remain the two blocks at this border while both exist, because a key that is inserted between them joins one of them.
Now a key of g 2 g_2 g 2 is accessed. If g 1 = g 2 g_1=g_2 g 1 = g 2 , operation t 1 t_1 t 1 accesses a key of g 2 g_2 g 2 , and t 1 > s t_1>s t 1 > s . Otherwise x u 1 x_{u_1} x u 1 lies across the cut from g 1 g_1 g 1 at time t 1 t_1 t 1 . The cut exists as long as g 1 g_1 g 1 does, and the block on its other side at time t 1 t_1 t 1 is g 2 g_2 g 2 , which exists from before s s s to t 2 t_2 t 2 . So x u 1 x_{u_1} x u 1 lies in g 2 g_2 g 2 , and acc ( x u 1 ) ≥ u 1 ≥ s \operatorname{acc}(x_{u_1})\ge u_1\ge s acc ( x u 1 ) ≥ u 1 ≥ s , because the chain of t 1 t_1 t 1 starts at u 1 u_1 u 1 and runs backwards. Both times are before t 2 t_2 t 2 .
Charging. The operation s i s_i s i of the chain of a crossing of level k k k receives min ( 1 , 2 τ s i + 5 − 3 k ) \min(1,2^{\tau_{s_i}+5-3k}) min ( 1 , 2 τ s i + 5 − 3 k ) . By Lemma 7.1 , these amounts sum to at least 1 for every crossing, and an operation s s s receives such an amount at most once per level. In (29) the amount is multiplied by 3 k 3k 3 k . Let k 0 k_0 k 0 be the least level with 3 k 0 > τ s + 5 3k_0>\tau_s+5 3 k 0 > τ s + 5 , so that the levels from k 0 k_0 k 0 on are 2 j k 0 2^j k_0 2 j k 0 for j ≥ 0 j\geq0 j ≥ 0 . Then the operation s s s receives, up to the factor 3, at most
∑ k k ⋅ min ( 1 , 2 τ s + 5 − 3 k ) ≤ ∑ k < k 0 k + ∑ j ≥ 0 2 j k 0 ⋅ 8 − ( 2 j − 1 ) k 0 < 3 k 0 ≤ 2 ( τ s + 5 ) . \sum_k k\cdot\min(1,2^{\tau_s+5-3k})\leq\sum_{k<k_0}k+\sum_{j\geq0}2^j k_0\cdot8^{-(2^j-1)k_0}<3k_0\leq2(\tau_s+5). k ∑ k ⋅ min ( 1 , 2 τ s + 5 − 3 k ) ≤ k < k 0 ∑ k + j ≥ 0 ∑ 2 j k 0 ⋅ 8 − ( 2 j − 1 ) k 0 < 3 k 0 ≤ 2 ( τ s + 5 ) .
The first sum is over powers of two below k 0 k_0 k 0 . In the second, 2 τ s + 5 − 3 ⋅ 2 j k 0 < 2 3 k 0 − 3 ⋅ 2 j k 0 2^{\tau_s+5-3\cdot2^j k_0}<2^{3k_0-3\cdot2^j k_0} 2 τ s + 5 − 3 ⋅ 2 j k 0 < 2 3 k 0 − 3 ⋅ 2 j k 0 by the choice of k 0 k_0 k 0 , and each term is at most a quarter of the previous one. The last step holds because k 0 = 1 k_0=1 k 0 = 1 , or 3 k 0 / 2 ≤ τ s + 5 3k_0/2\leq\tau_s+5 3 k 0 /2 ≤ τ s + 5 by the choice of k 0 k_0 k 0 . So an operation receives O ( τ s + 1 ) O(\tau_s+1) O ( τ s + 1 ) in all, and (29) gives
∑ t = 1 m λ ( t ) ≤ 3 m + ∑ t O ( τ t + 1 ) ⏟ pays for itself + O ( m ) ⏟ new blocks + ∑ s O ( τ s + 1 ) ⏟ crossings = O ( m + UB ( X ) ) . \sum_{t=1}^{m}\lambda(t)\leq3m+\underbrace{\sum_t O(\tau_t+1)}_{\text{pays for itself}}+\underbrace{O(m)}_{\text{new blocks}}+\underbrace{\sum_s O(\tau_s+1)}_{\text{crossings}}=O(m+\operatorname{UB}(X)). t = 1 ∑ m λ ( t ) ≤ 3 m + pays for itself t ∑ O ( τ t + 1 ) + new blocks O ( m ) + crossings s ∑ O ( τ s + 1 ) = O ( m + UB ( X )) .
Proof of Theorem 10.1 . Let first S 1 S_1 S 1 be empty. The algorithm is Algorithm 5. Every split or merge at level k k k creates a block of level k k k , so by Lemmas B.1 , B.5 and B.6 the total cost is
O ( ∑ t = 1 m λ ( t ) + ∑ k ( k + 1 ) N k ) = O ( m + UB ( X ) ) . O\left(\sum_{t=1}^{m}\lambda(t)+\sum_k(k+1)N_k\right)=O(m+\operatorname{UB}(X)). O ( t = 1 ∑ m λ ( t ) + k ∑ ( k + 1 ) N k ) = O ( m + UB ( X )) .
and operation t t t costs O ( log ( n t + 2 ) ) O(\log(n_t+2)) O ( log ( n t + 2 )) . For a set S 1 S_1 S 1 of n 0 n_0 n 0 keys, the initial tree is the tree of the algorithm after it has inserted the keys of S 1 S_1 S 1 in increasing order into the empty set. Let X ′ X' X ′ consist of these n 0 n_0 n 0 insertions followed by X X X . Each of the insertions has the term 1 in UB ( X ′ ) \operatorname{UB}(X') UB ( X ′ ) , with the insertion before it as a witness. An operation of X X X has the same witnesses in X ′ X' X ′ as in X X X , with the same values τ ( u , t ) \tau(u,t) τ ( u , t ) , and further ones. So UB ( X ′ ) ≤ n 0 + UB ( X ) \operatorname{UB}(X')\leq n_0+\operatorname{UB}(X) UB ( X ′ ) ≤ n 0 + UB ( X ) , and the algorithm serves X X X at a cost of O ( n 0 + m + UB ( X ) ) O(n_0+m+\operatorname{UB}(X)) O ( n 0 + m + UB ( X )) .
AI methodology statement
We used Claude Opus 5.5, Claude Fable 5.1 and Claude Fable 5.2 (Anthropic) and GPT-6 Astra and GPT-5.6 Sol (OpenAI) to assist with literature review, the design and analysis of the data structure, independent checking of the proofs, and manuscript drafting. These tools materially affected all sections. The authors take full responsibility for the contents of this paper.