Subsections


7.1 Random Binary Search Trees

Consider the two binary search trees shown in Figure 7.1, each of which has $ \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}=15$ nodes. The one on the left is a list and the other is a perfectly balanced binary search tree. The one on the left has a height of $ \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}-1=14$ and the one on the right has a height of three.

Figure 7.1: Two binary search trees containing the integers $ 0,\ldots,14$.
\includegraphics[scale=0.90909,scale=0.95]{figs-python/bst-path} \includegraphics[scale=0.90909,scale=0.95]{figs-python/bst-balanced}

Imagine how these two trees could have been constructed. The one on the left occurs if we start with an empty BinarySearchTree and add the sequence

$\displaystyle \langle 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14 \rangle \enspace .
$

No other sequence of additions will create this tree (as you can prove by induction on $ \ensuremath{\ensuremath{\mathit{n}}}$). On the other hand, the tree on the right can be created by the sequence

$\displaystyle \langle 7,3,11,1,5,9,13,0,2,4,6,8,10,12,14 \rangle \enspace .
$

Other sequences work as well, including

$\displaystyle \langle 7,3,1,5,0,2,4,6,11,9,13,8,10,12,14 \rangle \enspace ,
$

and

$\displaystyle \langle 7,3,1,11,5,0,2,4,6,9,13,8,10,12,14 \rangle \enspace .
$

In fact, there are $ 21,964,800$ addition sequences that generate the tree on the right and only one that generates the tree on the left.

The above example gives some anecdotal evidence that, if we choose a random permutation of $ 0,\ldots,14$, and add it into a binary search tree, then we are more likely to get a very balanced tree (the right side of Figure 7.1) than we are to get a very unbalanced tree (the left side of Figure 7.1).

We can formalize this notion by studying random binary search trees. A random binary search tree of size $ \ensuremath{\ensuremath{\mathit{n}}}$ is obtained in the following way: Take a random permutation, $ \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}_0,\ldots,\ensuremath{\ensure...
...\ensuremath{\mathit{x}}}}_{\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}-1}$, of the integers $ 0,\ldots,\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}-1$ and add its elements, one by one, into a BinarySearchTree. By random permutation we mean that each of the possible $ \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}!$ permutations (orderings) of $ 0,\ldots,\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}-1$ is equally likely, so that the probability of obtaining any particular permutation is $ 1/\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}!$.

Note that the values $ 0,\ldots,\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}-1$ could be replaced by any ordered set of $ \ensuremath{\ensuremath{\mathit{n}}}$ elements without changing any of the properties of the random binary search tree. The element $ \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}\in\{0,\ldots,\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}-1\}$ is simply standing in for the element of rank $ \ensuremath{\ensuremath{\mathit{x}}}$ in an ordered set of size $ \ensuremath{\ensuremath{\mathit{n}}}$.

Before we can present our main result about random binary search trees, we must take some time for a short digression to discuss a type of number that comes up frequently when studying randomized structures. For a non-negative integer, $ k$, the $ k$-th harmonic number, denoted $ H_k$, is defined as

$\displaystyle H_k = 1 + 1/2 + 1/3 + \cdots + 1/k \enspace .
$

The harmonic number $ H_k$ has no simple closed form, but it is very closely related to the natural logarithm of $ k$. In particular,

$\displaystyle \ln k < H_k \le \ln k + 1 \enspace .
$

Readers who have studied calculus might notice that this is because the integral $ \int_1^k\! (1/x)\, \mathrm{d}x= \ln k$. Keeping in mind that an integral can be interpreted as the area between a curve and the $ x$-axis, the value of $ H_k$ can be lower-bounded by the integral $ \int_1^k\! (1/x)\, \mathrm{d}x$ and upper-bounded by $ 1+ \int_1^k\! (1/x)\, \mathrm{d}x$. (See Figure 7.2 for a graphical explanation.)

Figure 7.2: The $ k$th harmonic number $ H_k=\sum_{i=1}^k 1/i$ is upper- and lower-bounded by two integrals. The value of these integrals is given by the area of the shaded region, while the value of $ H_k$ is given by the area of the rectangles.
\includegraphics[width=\textwidth ]{figs-python/harmonic-2} \includegraphics[width=\textwidth ]{figs-python/harmonic-3}

Lemma 7..1   In a random binary search tree of size $ \ensuremath{\ensuremath{\mathit{n}}}$, the following statements hold:
  1. For any $ \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}\in\{0,\ldots,\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}-1\}$, the expected length of the search path for $ \ensuremath{\ensuremath{\mathit{x}}}$ is $ H_{\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}+1} + H_{\ensuremath{\ensu...
...uremath{\mathit{n}}}}-\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}} - O(1)$.7.1
  2. For any $ \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}\in(-1,n)\setminus\{0,\ldots,\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}-1\}$, the expected length of the search path for $ \ensuremath{\ensuremath{\mathit{x}}}$ is $ H_{\lceil\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}\rceil}
+ H_{\ensur...
...th{\mathit{n}}}}-\lceil\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}\rceil}$.

We will prove Lemma 7.1 in the next section. For now, consider what the two parts of Lemma 7.1 tell us. The first part tells us that if we search for an element in a tree of size $ \ensuremath{\ensuremath{\mathit{n}}}$, then the expected length of the search path is at most $ 2\ln n + O(1)$. The second part tells us the same thing about searching for a value not stored in the tree. When we compare the two parts of the lemma, we see that it is only slightly faster to search for something that is in the tree compared to something that is not.

7.1.1 Proof of Lemma 7.1

The key observation needed to prove Lemma 7.1 is the following: The search path for a value $ \ensuremath{\ensuremath{\mathit{x}}}$ in the open interval $ (-1,\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}})$ in a random binary search tree, $ T$, contains the node with key $ i < \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}$ if, and only if, in the random permutation used to create $ T$, $ i$ appears before any of $ \{i+1,i+2,\ldots,\lfloor\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}\rfloor\}$.

To see this, refer to Figure 7.3 and notice that until some value in $ \{i,i+1,\ldots,\lfloor\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}\rfloor\}$ is added, the search paths for each value in the open interval $ (i-1,\lfloor\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}\rfloor+1)$ are identical. (Remember that for two values to have different search paths, there must be some element in the tree that compares differently with them.) Let $ j$ be the first element in $ \{i,i+1,\ldots,\lfloor\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}\rfloor\}$ to appear in the random permutation. Notice that $ j$ is now and will always be on the search path for $ \ensuremath{\ensuremath{\mathit{x}}}$. If $ j\neq i$ then the node $ \ensuremath{\ensuremath{\ensuremath{\mathit{u}}}}_j$ containing $ j$ is created before the node $ \ensuremath{\ensuremath{\ensuremath{\mathit{u}}}}_i$ that contains $ i$. Later, when $ i$ is added, it will be added to the subtree rooted at $ \ensuremath{\ensuremath{\ensuremath{\mathit{u}}}}_j\ensuremath{.\ensuremath{\ensuremath{\mathit{left}}}}$, since $ i<j$. On the other hand, the search path for $ \ensuremath{\ensuremath{\mathit{x}}}$ will never visit this subtree because it will proceed to $ \ensuremath{\ensuremath{\ensuremath{\mathit{u}}}}_j\ensuremath{.\ensuremath{\ensuremath{\mathit{right}}}}$ after visiting $ \ensuremath{\ensuremath{\ensuremath{\mathit{u}}}}_j$.

Figure 7.3: The value $ i<\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}$ is on the search path for $ \ensuremath{\ensuremath{\mathit{x}}}$ if and only if $ i$ is the first element among $ \{i,i+1,\ldots,\lfloor\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}\rfloor\}$ added to the tree.
% latex2html id marker 84546
\includegraphics[width=\textwidth ]{figs-python/rbst-records}

Similarly, for $ i>\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}$, $ i$ appears in the search path for $ \ensuremath{\ensuremath{\mathit{x}}}$ if and only if $ i$ appears before any of $ \{\lceil\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}\rceil,
\lceil\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}\rceil+1,\ldots,i-1\}$ in the random permutation used to create $ T$.

Notice that, if we start with a random permutation of $ \{0,\ldots,\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}\}$, then the subsequences containing only $ \{i,i+1,\ldots,\lfloor\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}\rfloor\}$ and $ \{\lceil\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}\rceil, \lceil\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}\rceil+1,\ldots,i-1\}$ are also random permutations of their respective elements. Each element, then, in the subsets $ \{i,i+1,\ldots,\lfloor\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}\rfloor\}$ and $ \{\lceil\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}\rceil,
\lceil\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}\rceil+1,\ldots,i-1\}$ is equally likely to appear before any other in its subset in the random permutation used to create $ T$. So we have

$\displaystyle \Pr\{$$\displaystyle \mbox{$i$\ is on the search path for \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}}$$\displaystyle \}
= \left\{ \begin{array}{ll}
1/(\lfloor\ensuremath{\ensuremat...
...emath{\ensuremath{\ensuremath{\mathit{x}}}}$}
\end{array}\right . \enspace .
$

With this observation, the proof of Lemma 7.1 involves some simple calculations with harmonic numbers:

Proof. [Proof of Lemma 7.1] Let $ I_i$ be the indicator random variable that is equal to one when $ i$ appears on the search path for $ \ensuremath{\ensuremath{\mathit{x}}}$ and zero otherwise. Then the length of the search path is given by

$\displaystyle \sum_{i\in\{0,\ldots,\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}-1\}\setminus\{\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}\}} I_i
$

so, if $ \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}\in\{0,\ldots,\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}-1\}$, the expected length of the search path is given by (see Figure 7.4.a)

$\displaystyle \mathrm{E}\left[\sum_{i=0}^{\ensuremath{\ensuremath{\ensuremath{\...
...athit{x}}}}+1}^{\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}-1} I_i\right]$ $\displaystyle = \sum_{i=0}^{\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}-1...
...\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}-1} \mathrm{E}\left[I_i\right]$    
  $\displaystyle = \sum_{i=0}^{\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}-1...
...{n}}}}-1} 1/(i-\lceil\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}\rceil+1)$    
  $\displaystyle = \sum_{i=0}^{\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}-1...
...math{\mathit{n}}}}-1} 1/(i-\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}+1)$    
  $\displaystyle = \frac{1}{2}+\frac{1}{3}+\cdots+\frac{1}{\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}+1}$    
  $\displaystyle \quad {} + \frac{1}{2}+\frac{1}{3}+\cdots+\frac{1}{\ensuremath{\e...
...th{\ensuremath{\mathit{n}}}}-\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}}$    
  $\displaystyle = H_{\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}+1} + H_{\e...
...\mathit{n}}}}-\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}} - 2 \enspace .$    

The corresponding calculations for a search value $ \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}\in(-1,n)\setminus\{0,\ldots,\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}-1\}$ are almost identical (see Figure 7.4.b). $ \qedsymbol$

Figure 7.4: The probabilities of an element being on the search path for $ \ensuremath{\ensuremath{\mathit{x}}}$ when (a)  $ \ensuremath{\ensuremath{\mathit{x}}}$ is an integer and (b) when $ \ensuremath{\ensuremath{\mathit{x}}}$ is not an integer.
 \includegraphics[width=\textwidth ]{figs-python/rbst-probs-a}  
 (a)  
 \includegraphics[width=\textwidth ]{figs-python/rbst-probs-b}  
 (b)  

7.1.2 Summary

The following theorem summarizes the performance of a random binary search tree:

Theorem 7..1   A random binary search tree can be constructed in $ O(\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}\log \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}})$ time. In a random binary search tree, the $ \ensuremath{\mathrm{find}(\ensuremath{\mathit{x}})}$ operation takes $ O(\log
\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}})$ expected time.

We should emphasize again that the expectation in Theorem 7.1 is with respect to the random permutation used to create the random binary search tree. In particular, it does not depend on a random choice of $ \ensuremath{\ensuremath{\mathit{x}}}$; it is true for every value of $ \ensuremath{\ensuremath{\mathit{x}}}$.



Footnotes

....7.1
The expressions $ \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}+1$ and $ \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}-\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}$ can be interpreted respectively as the number of elements in the tree less than or equal to $ \ensuremath{\ensuremath{\mathit{x}}}$ and the number of elements in the tree greater than or equal to $ \ensuremath{\ensuremath{\mathit{x}}}$.
opendatastructures.org