On weighted depths in random binary search trees
Abstract.
Following the model introduced by Aguech, Lasmar and Mahmoud [Probab. Engrg. Inform. Sci. 21 (2007) 133–141], the weighted depth of a node in a labelled rooted tree is the sum of all labels on the path connecting the node to the root. We analyze weighted depths of nodes with given labels, the last inserted node, nodes ordered as visited by the depth first search process, the weighted path length and the weighted Wiener index in a random binary search tree. We establish three regimes of nodes depending on whether the second order behaviour of their weighted depths follows from fluctuations of the keys on the path, the depth of the nodes, or both. Finally, we investigate a random distribution function on the unit interval arising as scaling limit for weighted depths of nodes with at most one child.
Key words and phrases:
analysis of algorithm, data structures, binary search trees, central limit theorems, contraction method, random probability measures1600 Mathematics Subject Classification:
60F05, 68P05, 68Q251. Introduction
The binary search tree is an important data structure in computer science allowing for efficient execution of database operations such as insertion, deletion and retrieving of data. Given a list of elements from a totally ordered set, it is the unique labelled rooted binary tree with nodes constructed by successive insertion of all elements satisfying the following property: for each node in the tree with label (or key), say , all keys stored in its left (right) subtree are at most equal to (strictly larger than) . For an illustration, see Figure 1.
Properties of binary search trees are typically analyzed under the random permutation model where the data are generated by a uniformly chosen permutation of the first integers. Among the quantities studied in binary search trees, one finds depths of and distances between nodes related to the performance of search queries and finger searches in the database, the (total) path length measuring the cost of constructing the tree as well as the Wiener index. Further, more complex parameters such as the height corresponding to worst case search times, the saturation level and the profile have been studied thoroughly. We review literature relevant in the context of our work below.
In this note we complement the wide literature on random binary search trees by the analysis of depths of nodes, path length and Wiener index in their weighted versions as introduced by Aguech, Lasmar and Mahmoud [1]. Here, the weighted depth of a node is the sum of all keys stored on the path to the root. In [1], results about weighted depths of extremal paths have been obtained. Kuba and Panholzer [19], [20] studied the problem in random increasing trees covering the random recursive tree and the random planeoriented recursive tree. Weighted depths of nodes and the weighted height were also studied by Broutin and Devroye [3] in a more general tree model, which relies on assigning weights to the edges of the tree. Further, the weighted path length in this model was investigated by Rüschendorf and Schopp [29]. Note that we deviate from the notation introduced in [1] and [19] using the term weighted depth for what is called weighted path length there since we also study a weighted version of the (total) path length of binary search trees.
2. Preliminaries
We introduce some notation. By the size of a finite binary tree, we refer to its number of nodes. Upon embedding a finite rooted binary tree in the complete infinite binary tree, a node is called external if its graph distance to the binary tree is one. Any node on level in a rooted binary tree is associated a vector where if and only if the path from the root to the node continues in the left subtree upon reaching level .
Let and . Under the random permutation model (short: permutation model), let be the depth of the node labelled . By we denote the sum of all keys on the path from the root to the node labelled including the labels of both endpoints. For , let be the maximal depth among nodes of the form . We use () to denote the (weighted) depth of the th inserted node. Finally, we define the height of the tree by .
Throughout the paper, we denote by the distribution of a random variable . For realvalued with finite second moment, we write for its standard deviation. By we denote a random variable with the standard normal distribution, and by the Dickman distribution on characterized by its Fourier transform,
(1) 
The origins of the Dickman distribution go back to Dickman’s [10] classical result on large prime divisors. Compare Hildebrandt and Tenenbaum [15] for a survey on the problem. In the probabilistic analysis of algorithm, first arose in Hwang and Tsai’s [17] study of the complexity of Hoare’s selection algorithm. We refer to this work for a discussion of more details on the distribution, historical background and further references.
Finally, we use the Landau notations little–, big–, little–, big– and big– as .
2.1. Depths and height
We recall the following fundamental property of random binary search trees going back to Devroye [6]: in probability and with respect to all moments, we have
(2) 
where is the larger of the two solutions to the transcendent equation . Next, by classical results due to Brown and Shubert [4] and Devroye [7], for any , in distribution,
(3) 
(In [7, Theorem O1], the first convergence in the last display is formulated for The general case follows, since, by symmetry, for all . The second convergence was also claimed in a footnote by Mahmoud and Pittel [23].) Grübel [13] studied the process , the socalled silhouette, thereby obtaining a functional limit theorem for its integrated version. The asymptotic behaviour of depths of nodes with given labels has been analyzed by Devroye and Neininger [9]: uniformly in and as ,
(4) 
Moreover, for any , which may depend on , in distribution
(5) 
Here, one should also compare Grübel and Stefanoski [14] for stronger results in the context of the corresponding Poisson approximation. For a survey on depths and distances in binary search trees, we refer to Mahmoud’s book [21]. Finally, the asymptotic behaviour of the weighted depths of the nodes associated with the vectors and denoted by and ( and stand for left and right) were studied in [1]. In distribution,
(6) 
where has the Dickman distribution. The first convergence is closely related to the limit law in Theorem 3.1 in [17].
2.2. Path length and Wiener index
In a rooted tree, the path length is defined as the sum over all depths of nodes. Moreover, the Wiener index is obtained by summing all distances of unordered pairs of vertices. For a random binary search tree of size , we denote its path length by and its Wiener index by . Denoting by the EulerMascheroni constant, we have
(7) 
going back to Hoare [16] and Knuth [18]. Further, by [25],
(8) 
Central limit theorems for the path length go back to Régnier [27] and Rösler [28], for the Wiener index to Neininger [25]. More precisely, by [25, Theorem 1.1], there exists a nontrivial random variable on characterized by a stochastic fixedpoint equation, such that, in distribution,
(9) 
2.3. The i.i.d. model
We also consider binary search trees of size where the data are chosen as the first values of a sequence of independent random variables each having the uniform distribution on . Since the vector constitutes a uniformly chosen permutation, in distribution, both the permutation model and the i.i.d. model lead to the same unlabelled tree. We use the same notation as in the permutation model for quantities not involving the labels of nodes, that is, and . Further, we define the weighted path length as the sum of all weighted depths, and the weighted Wiener index as the sum over all pairs of weighted distances. Here, the weighted distance between two nodes equals the sum of all labels on the path connecting them, labels of endpoints included. (Notice that the weighted distance between a node and itself is equal to its label.) Finally, analogously to , we define as the weighted depth of the node of largest depth on the path . We call , the weighted silhouette of the tree (at time ).
3. Main results
Our main results are divided into two groups: Theorems 1 and 2 hold in the permutation model while Theorems 3 and 4 are formulated in the i.i.d. model.
3.1. Results in the permutation model
We start with the expansions of the first two moments of the weighted depth . Uniformly in , as ,
(10)  
(11) 
It turns out that the asymptotic distributional behavior of with respect to terms of second order is entirely described by that of if and only if . Accordingly, in the remainder of this paper, we call nodes with labels of order large and of order small.
Theorem 1 (Weighted depths of large nodes).
For ,
(12) 
In particular, for and , in distribution,
(13) 
For the last inserted node, in distribution,
(14) 
where and are independent and is uniformly distributed on .
The asymptotic behavior of weighted depths of small nodes is to be compared with the corresponding results in [19]. Here, another phase transition occurs when .
Theorem 2 (Weighted depths of small nodes).
Let . Then, in distribution,
(15) 
where and are independent and has the Dickman distribution. Thus, if , in distribution,
In particular, if with , then, in distribution,
3.2. Results in the i.i.d. model
Any corresponds to a unique value by . This identification becomes onetoone upon allowing only those which contain infinitely many zeros and . In the i.i.d. model, for any , the node eventually appears in the sequence of binary search trees and we write for its ultimate label. The following theorem about the behavior of involves a random continuous distribution function arising as the almost sure limit of as . We believe that this process is of independent interest and state some of its properties in Proposition 1 in Section 3.3. The simulations of presented in Figure 2 illustrate the scaling limit.
Theorem 3 (Weighted silhouette).
There exists a random continuous and strictly increasing bijection , such that, almost surely, uniformly on the unit interval, . For any , in probability,
(16) 
Also, for any , in probability
(17) 
Further, in probability,
(18) 
with as in (2). Finally, for any , in distribution,
(19) 
where and are independent.
The next theorem extends the distributional convergence result in Theorem 1.1 in [25], that is (9), by central limit theorems for the weighted path length and the weighted Wiener index.
Theorem 4 (Weighted path length and Wiener index).
In the i.i.d. model, we have
and
The leading constants in the expansions of the covariances between and are given in (36)–(38). (The leading constant for was already given in [25].) As , with convergence in distribution and with respect to the first two moments in , we have
where the limiting distribution is the unique fixedpoint of the map in (35).
Conclusions. We have seen that there exist three types of nodes showing significantly different behavior with respect to their weighted depths. By Theorem 1, for , second order fluctuations of weighted depths are due to variations of the depth of nodes. In the second regime, when , variations of weighted depths are determined by two independent contributions, one for the depths and one for the keys on the paths. Finally, when only fluctuations of labels on paths influence second order terms of weighted depths. The third regime can be further subdivided with respect to the first order terms of and : for , they coincide, for , they are of the same magnitude, whereas, for , they are of different scale. By Theorem 3, the weighted silhouette behaves considerably different. Here, the lack of concentration around the mean leads to an interesting random distribution function on the unit interval as scaling limit.
3.3. Further results and remarks
Model comparison. We decided to present Theorems 3 and 4 in the i.i.d. model rather than in the permutation model since this allows for a stronger mode of convergence in (16), (17) and a clearer presentation of the proof of Theorem 4. In the i.i.d. model, denoting by the weighted depth of the node of rank among the first inserted keys, Theorems 1 and 2 remain valid upon replacing by . Similarly, Theorems 3 and 4 hold in the permutation model where weighted depths and the weighted path length are to be scaled down by a factor and the weighted Wiener index by a factor . The convergences in (16) and (17) then only hold in distribution. This can be deduced most easily from the following coupling of the two models: starting with the binary search tree in the i.i.d. model, also consider the random binary search tree in the permutation model relying on the permutation . Then, for all ,
(20) 
It is wellknown that the second factor on right hand side grows like , compare, e.g. Donsker’s theorem for empirical distribution functions or the DvoretzkyKieferWolfowitz inequality [11]. Combining this, (20) and (2) is sufficient to transfer all results in Section 3 between the two models.
The depth first search process. In the permutation model, let be the external nodes as discovered by the depth first search process from left to right. By and , we denote depth and weighted depth of the external node . Then, at the end of Section 4.1, we show that, uniformly in ,
(21) 
Thus, the results in Theorems 1 and 2 also cover the second order analysis of the sequences and .
Weighted distances. In the permutation model, let be the graph distance between the nodes labelled and be the sum of all labels on the path from to , labels at the endpoints included. Asymptotic normality for the sequence (after rescaling) under the optimal condition has been obtained in [9]. For uniformly chosen nodes, distributional convergence results date back to Mahmoud and Neininger [22] and Panholzer and Prodinger [26]. Analogously to Theorem 1, it is straightforward to prove central limit theorems jointly for weighted and nonweighted distances. We only state the results. If and , then
In particular, for and , we have, in distribution,
Here, are independent random variables both with the standard normal distribution.
The limit process . The process in Theorem 3 is a random distribution function. In particular, it can be regarded as an element in the set of càdlàg functions consisting of all , such that, for all , and exists. The absolute value of is defined by . Endowed with Skorokhod’s topology , becomes a Polish space. We refer to Chapter 3 in Billingsley’s book [2] for detailed information on this matter.
Proposition 1 (Properties of ).
The process is unique (in distribution) among all càdlàg processes with finite absolute second moment satisfying
(22) 
Here, are independent, has the uniform distribution on and is distributed like . We have

for all ;

;

has the arcsine distribution with density
where are independent and has the uniform distribution on ;

for , has a smooth density ;

for , , , is strictly monotonically decreasing and ;

with , and , we have for , for and as .
Random recursive trees. A random recursive tree is constructed as follows: starting with the root labelled one, in the th step, , a node labelled is inserted in the tree and connected to an already existing node chosen uniformly at random. Weighted depths in random binary search trees differ substantially from those in random recursive trees analyzed in [19] where all nodes show an asymptotic behaviour comparable to that of nodes labelled in the binary search tree. The difference is highlighted by the weighted path length. Being of the same order as the path length in binary search trees, it follows from results in [19] that the weighted path length in a random recursive tree of size is of order . The same is valid for its standard deviation. We conjecture that the sequence converges in distribution to a nontrivial limit; however, the recursive approach worked out in the proof of Theorem 4, which also applies to the analysis of the path length in random recursive trees, seems not to be fruitful in this context.
Outline. All results are proved in Section 4 starting with the proofs of Theorems 1 and 2 as well as (21) in Section 4.1. Here, most arguments are based on representations of (weighted) depths as sums of bounded independent random variables which go back to Devroye and Neininger [9]. Theorem 3 and Proposition 1 are proved in Section 4.2. In this part, the construction of the limiting process relies on suitable uniform bounds on the increments of the process while the properties of the limit laws formulated in Proposition 1 follow from the distributional fixedpoint equation (22). Finally, the proof of Theorem 4 relying on the contraction method is worked out in Section 4.3.
4. Proofs
4.1. Weighted depths of labelled nodes
In the permutation model, let be the event that the node labelled is in the subtree of the node labelled . Then, and . It is easy to see that and are two families of independent events; however, there exist subtle dependencies between the sets. Following the approach in [9], let for and for . For convenience, let be an almost sure event. The following lemma summarizes results in [9] and we refer to this paper for a proof. In this context, note that Devroye [8] gives distributional representations as sums of independent (or independent) indicator variables for quantities growing linearly in , such as the number of leaves.
Lemma 1.
Let . Then, the events , are independent. For , we have
From the lemma, it follows that
The ideas in [9] can also be used to analyze second (mixed) moments. Straightforward calculations show the following bounds:
and
Here, both terms are uniform in . Define and . We make the following observation:
For and , set and . Using Lemma 1, one easily computes
As and , both expansions (10) and (11) follow from observation O.
4.1.1. Weighted depths of large nodes
We prove Theorem 1. First, (12) follows from (4) and
(23) 
For , combining (4), (5) and (10), in distribution,
Considering the last inserted node with value , note that, conditionally on , the correlations between the events and vanish. More precisely, given , the family is distributed like a family of independent Bernoulli random variables with for and . Thus,
By (3), we have in probability. Hence, in order to prove (14), it suffices to show that, in distribution,
(24) 
For a sequence satisfying for , let us condition on the event . Then, by the central limit theorem for triangular arrays of rowwise independent uniformly bounded random variables with diverging variance applied to , in distribution,
Hence, (24) follows from an application of the theorem of dominated convergence noting that is uniformly distributed on .
4.1.2. Weighted depths of small nodes
We prove Theorem 2. Let and . Since , the same calculation as in (23) shows that,
(25) 
For , we have
By a standard Taylor expansion, the last display equals
Here, in the last step, we have used that the sum on the right hand side is a Riemann sum over the unit interval whose mesh size tends to zero. Thus, using the notation of the theorem, (1) and Lévy’s continuity theorem, in distribution,
(26) 
In order to deduce (15) note that, by Lemma 1, and are independent while
in distribution if and only if using the central limit theorem for sums of independent and uniformly bounded random variables. Since
we deduce
from (26) upon treating the cases and separately. From here, the assertion (15) follows with the help of (25) and observation O.
4.1.3. Proof of (21)
The main observation is that the th external node visited by the depth first search process is always contained in the subtree rooted at the node labelled . This can be proved by induction exploiting the decomposition of the tree at the root. Thus, denoting by the height of the subtree rooted at the node labelled , we have
Here, stands for the largest label in the subtree rooted at the node labelled . Let be the size of the subtree rooted at . Then where denotes the number of elements in the subtree rooted at with values smaller than . By Lemma 1, for , we have . Using the same arguments for the quantity , we deduce that, uniformly in ,
Thus, by an application of (2), for some ,
By the same arguments, for some , we have
4.2. The weighted silhouette
Proof of Theorem 3. We start with the uniform convergence of . For all , is distributed like the product of independent random variables, each of which having the uniform distribution on . In particular, by the union bound and Markov’s inequality, for any ,
For , let