Subsections


6.2 BinarySearchTree: An Unbalanced Binary Search Tree

A BinarySearchTree is a special kind of binary tree in which each node, $ \ensuremath{\ensuremath{\mathit{u}}}$, also stores a data value, $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{x}}}$, from some total order. The data values in a binary search tree obey the binary search tree property: For a node, $ \ensuremath{\ensuremath{\mathit{u}}}$, every data value stored in the subtree rooted at $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{left}}}$ is less than $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{x}}}$ and every data value stored in the subtree rooted at $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{right}}}$ is greater than $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{x}}}$. An example of a BinarySearchTree is shown in Figure 6.5.

Figure 6.5: A binary search tree.
\includegraphics[scale=0.90909]{figs-python/bst-example}

6.2.1 Searching

The binary search tree property is extremely useful because it allows us to quickly locate a value, $ \ensuremath{\ensuremath{\mathit{x}}}$, in a binary search tree. To do this we start searching for $ \ensuremath{\ensuremath{\mathit{x}}}$ at the root, $ \ensuremath{\ensuremath{\mathit{r}}}$. When examining a node, $ \ensuremath{\ensuremath{\mathit{u}}}$, there are three cases:

  1. If $ \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}< \ensuremath{\ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{x}}}}$, then the search proceeds to $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{left}}}$;
  2. If $ \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}> \ensuremath{\ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{x}}}}$, then the search proceeds to $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{right}}}$;
  3. If $ \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}= \ensuremath{\ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{x}}}}$, then we have found the node $ \ensuremath{\ensuremath{\mathit{u}}}$ containing $ \ensuremath{\ensuremath{\mathit{x}}}$.
The search terminates when Case 3 occurs or when $ \ensuremath{\ensuremath{\mathit{u}}\gets \ensuremath{nil}}$. In the former case, we found $ \ensuremath{\ensuremath{\mathit{x}}}$. In the latter case, we conclude that $ \ensuremath{\ensuremath{\mathit{x}}}$ is not in the binary search tree.
\begin{leftbar}
\begin{flushleft}
\hspace*{1em} \ensuremath{\mathrm{find\_eq}(\e...
...{return}} \ensuremath{\ensuremath{\mathit{nil}}}\\
\end{flushleft}\end{leftbar}

Two examples of searches in a binary search tree are shown in Figure 6.6. As the second example shows, even if we don't find $ \ensuremath{\ensuremath{\mathit{x}}}$ in the tree, we still gain some valuable information. If we look at the last node, $ \ensuremath{\ensuremath{\mathit{u}}}$, at which Case 1 occurred, we see that $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{x}}}$ is the smallest value in the tree that is greater than $ \ensuremath{\ensuremath{\mathit{x}}}$. Similarly, the last node at which Case 2 occurred contains the largest value in the tree that is less than $ \ensuremath{\ensuremath{\mathit{x}}}$. Therefore, by keeping track of the last node, $ \ensuremath{\ensuremath{\mathit{z}}}$, at which Case 1 occurs, a BinarySearchTree can implement the $ \ensuremath{\mathrm{find}(\ensuremath{\mathit{x}})}$ operation that returns the smallest value stored in the tree that is greater than or equal to $ \ensuremath{\ensuremath{\mathit{x}}}$:
\begin{leftbar}
\begin{flushleft}
\hspace*{1em} \ensuremath{\mathrm{find}(\ensur...
...\ensuremath{\mathit{z}}.\ensuremath{\mathit{x}}}\\
\end{flushleft}\end{leftbar}

Figure 6.6: An example of (a) a successful search (for $ 6$) and (b) an unsuccessful search (for $ 10$) in a binary search tree.
\includegraphics[width=\textwidth ]{figs-python/bst-example-2} \includegraphics[width=\textwidth ]{figs-python/bst-example-3}
(a) (b)

6.2.2 Addition

To add a new value, $ \ensuremath{\ensuremath{\mathit{x}}}$, to a BinarySearchTree, we first search for $ \ensuremath{\ensuremath{\mathit{x}}}$. If we find it, then there is no need to insert it. Otherwise, we store $ \ensuremath{\ensuremath{\mathit{x}}}$ at a leaf child of the last node, $ \ensuremath{\ensuremath{\mathit{p}}}$, encountered during the search for $ \ensuremath{\ensuremath{\mathit{x}}}$. Whether the new node is the left or right child of $ \ensuremath{\ensuremath{\mathit{p}}}$ depends on the result of comparing $ \ensuremath{\ensuremath{\mathit{x}}}$ and $ \ensuremath{\ensuremath{\mathit{p}}.\ensuremath{\mathit{x}}}$.
\begin{leftbar}
\begin{flushleft}
\hspace*{1em} \ensuremath{\mathrm{add}(\ensure...
...}, \mathrm{new\_node}(\ensuremath{\mathit{x}})})\\
\end{flushleft}\end{leftbar}

\begin{leftbar}
\begin{flushleft}
\hspace*{1em} \ensuremath{\mathrm{find\_last}(...
...return}} \ensuremath{\ensuremath{\mathit{prev}}}\\
\end{flushleft}\end{leftbar}

\begin{leftbar}
\begin{flushleft}
\hspace*{1em} \ensuremath{\mathrm{add\_child}(...
...eturn}} \ensuremath{\ensuremath{\mathit{true}}} \\
\end{flushleft}\end{leftbar}
An example is shown in Figure 6.7. The most time-consuming part of this process is the initial search for $ \ensuremath{\ensuremath{\mathit{x}}}$, which takes an amount of time proportional to the height of the newly added node $ \ensuremath{\ensuremath{\mathit{u}}}$. In the worst case, this is equal to the height of the BinarySearchTree.

Figure 6.7: Inserting the value $ 8.5$ into a binary search tree.
\includegraphics[width=\textwidth ]{figs-python/bst-example-4} \includegraphics[width=\textwidth ]{figs-python/bst-example-5}

6.2.3 Removal

Deleting a value stored in a node, $ \ensuremath{\ensuremath{\mathit{u}}}$, of a BinarySearchTree is a little more difficult. If $ \ensuremath{\ensuremath{\mathit{u}}}$ is a leaf, then we can just detach $ \ensuremath{\ensuremath{\mathit{u}}}$ from its parent. Even better: If $ \ensuremath{\ensuremath{\mathit{u}}}$ has only one child, then we can splice $ \ensuremath{\ensuremath{\mathit{u}}}$ from the tree by having $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{parent}}}$ adopt $ \ensuremath{\ensuremath{\mathit{u}}}$'s child (see Figure 6.8):
\begin{leftbar}
\begin{flushleft}
\hspace*{1em} \ensuremath{\mathrm{splice}(\ens...
... \gets \ensuremath{\ensuremath{\mathit{n}} - 1}}\\
\end{flushleft}\end{leftbar}

Figure 6.8: Removing a leaf ($ 6$) or a node with only one child ($ 9$) is easy.
\includegraphics[scale=0.90909]{figs-python/bst-splice}

Things get tricky, though, when $ \ensuremath{\ensuremath{\mathit{u}}}$ has two children. In this case, the simplest thing to do is to find a node, $ \ensuremath{\ensuremath{\mathit{w}}}$, that has less than two children and such that $ \ensuremath{\ensuremath{\mathit{w}}.\ensuremath{\mathit{x}}}$ can replace $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{x}}}$. To maintain the binary search tree property, the value $ \ensuremath{\ensuremath{\mathit{w}}.\ensuremath{\mathit{x}}}$ should be close to the value of $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{x}}}$. For example, choosing $ \ensuremath{\ensuremath{\mathit{w}}}$ such that $ \ensuremath{\ensuremath{\mathit{w}}.\ensuremath{\mathit{x}}}$ is the smallest value greater than $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{x}}}$ will work. Finding the node $ \ensuremath{\ensuremath{\mathit{w}}}$ is easy; it is the smallest value in the subtree rooted at $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{right}}}$. This node can be easily removed because it has no left child (see Figure 6.9).
\begin{leftbar}
\begin{flushleft}
\hspace*{1em} \ensuremath{\mathrm{remove\_node...
...remath{\mathrm{splice}(\ensuremath{\mathit{w}})}\\
\end{flushleft}\end{leftbar}

Figure 6.9: Deleting a value ($ 11$) from a node, $ \ensuremath{\ensuremath{\mathit{u}}}$, with two children is done by replacing $ \ensuremath{\ensuremath{\mathit{u}}}$'s value with the smallest value in the right subtree of $ \ensuremath{\ensuremath{\mathit{u}}}$.
\includegraphics[width=\textwidth ]{figs-python/bst-delete-1} \includegraphics[width=\textwidth ]{figs-python/bst-delete-2}  

6.2.4 Summary

The $ \ensuremath{\mathrm{find}(\ensuremath{\mathit{x}})}$, $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$, and $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{x}})}$ operations in a BinarySearchTree each involve following a path from the root of the tree to some node in the tree. Without knowing more about the shape of the tree it is difficult to say much about the length of this path, except that it is less than $ \ensuremath{\ensuremath{\mathit{n}}}$, the number of nodes in the tree. The following (unimpressive) theorem summarizes the performance of the BinarySearchTree data structure:

Theorem 6..1   BinarySearchTree implements the SSet interface and supports the operations $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$, $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{x}})}$, and $ \ensuremath{\mathrm{find}(\ensuremath{\mathit{x}})}$ in $ O(\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}})$ time per operation.

Theorem 6.1 compares poorly with Theorem 4.1, which shows that the SkiplistSSet structure can implement the SSet interface with $ O(\log \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}})$ expected time per operation. The problem with the BinarySearchTree structure is that it can become unbalanced. Instead of looking like the tree in Figure 6.5 it can look like a long chain of $ \ensuremath{\ensuremath{\mathit{n}}}$ nodes, all but the last having exactly one child.

There are a number of ways of avoiding unbalanced binary search trees, all of which lead to data structures that have $ O(\log
\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}})$ time operations. In Chapter 7 we show how $ O(\log \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}})$ expected time operations can be achieved with randomization. In Chapter 8 we show how $ O(\log \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}})$ amortized time operations can be achieved with partial rebuilding operations. In Chapter 9 we show how $ O(\log \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}})$ worst-case time operations can be achieved by simulating a tree that is not binary: one in which nodes can have up to four children.

opendatastructures.org