Subsections


7.2 Treap: A Randomized Binary Search Tree

The problem with random binary search trees is, of course, that they are not dynamic. They don't support the $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ or $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{x}})}$ operations needed to implement the SSet interface. In this section we describe a data structure called a Treap that uses Lemma 7.1 to implement the SSet interface.7.2

A node in a Treap is like a node in a BinarySearchTree in that it has a data value, $ \ensuremath{\ensuremath{\mathit{x}}}$, but it also contains a unique numerical priority, $ \ensuremath{\ensuremath{\mathit{p}}}$, that is assigned at random: In addition to being a binary search tree, the nodes in a Treap also obey the heap property:

In other words, each node has a priority smaller than that of its two children. An example is shown in Figure 7.5.

Figure 7.5: An example of a Treap containing the integers $ 0,\ldots,9$. Each node, $ \ensuremath{\ensuremath{\mathit{u}}}$, is illustrated as a box containing $ \ensuremath{\ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{x}}}},\ensuremath{\ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{p}}}}$.
\includegraphics[width=\textwidth ]{figs-python/treap}

The heap and binary search tree conditions together ensure that, once the key ( $ \ensuremath{\ensuremath{\mathit{x}}}$) and priority ( $ \ensuremath{\ensuremath{\mathit{p}}}$) for each node are defined, the shape of the Treap is completely determined. The heap property tells us that the node with minimum priority has to be the root, $ \ensuremath{\ensuremath{\mathit{r}}}$, of the Treap. The binary search tree property tells us that all nodes with keys smaller than $ \ensuremath{\ensuremath{\mathit{r}}.\ensuremath{\mathit{x}}}$ are stored in the subtree rooted at $ \ensuremath{\ensuremath{\mathit{r}}.\ensuremath{\mathit{left}}}$ and all nodes with keys larger than $ \ensuremath{\ensuremath{\mathit{r}}.\ensuremath{\mathit{x}}}$ are stored in the subtree rooted at $ \ensuremath{\ensuremath{\mathit{r}}.\ensuremath{\mathit{right}}}$.

The important point about the priority values in a Treap is that they are unique and assigned at random. Because of this, there are two equivalent ways we can think about a Treap. As defined above, a Treap obeys the heap and binary search tree properties. Alternatively, we can think of a Treap as a BinarySearchTree whose nodes were added in increasing order of priority. For example, the Treap in Figure 7.5 can be obtained by adding the sequence of $ (\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}},\ensuremath{\ensuremath{\ensuremath{\mathit{p}}}})$ values

$\displaystyle \langle
(3,1), (1,6), (0,9), (5,11), (4,14), (9,17), (7,22), (6,42), (8,49), (2,99)
\rangle
$

into a BinarySearchTree.

Since the priorities are chosen randomly, this is equivalent to taking a random permutation of the keys--in this case the permutation is

$\displaystyle \langle 3, 1, 0, 5, 9, 4, 7, 6, 8, 2 \rangle
$

--and adding these to a BinarySearchTree. But this means that the shape of a treap is identical to that of a random binary search tree. In particular, if we replace each key $ \ensuremath{\ensuremath{\mathit{x}}}$ by its rank,7.3 then Lemma 7.1 applies. Restating Lemma 7.1 in terms of Treaps, we have:

Lemma 7..2   In a Treap that stores a set $ S$ of $ \ensuremath{\ensuremath{\mathit{n}}}$ keys, the following statements hold:
  1. For any $ \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}\in S$, the expected length of the search path for $ \ensuremath{\ensuremath{\mathit{x}}}$ is $ H_{r(\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}})+1} + H_{\ensuremath{\e...
...math{\mathit{n}}}}-r(\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}})} - O(1)$.
  2. For any $ \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}\not\in S$, the expected length of the search path for $ \ensuremath{\ensuremath{\mathit{x}}}$ is $ H_{r(\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}})} + H_{\ensuremath{\ens...
...\ensuremath{\mathit{n}}}}-r(\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}})}$.
Here, $ r(\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}})$ denotes the rank of $ \ensuremath{\ensuremath{\mathit{x}}}$ in the set $ S\cup\{\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}\}$.

Again, we emphasize that the expectation in Lemma 7.2 is taken over the random choices of the priorities for each node. It does not require any assumptions about the randomness in the keys.

Lemma 7.2 tells us that Treaps can implement the $ \ensuremath{\mathrm{find}(\ensuremath{\mathit{x}})}$ operation efficiently. However, the real benefit of a Treap is that it can support the $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ and $ \ensuremath{\mathrm{delete}(\ensuremath{\mathit{x}})}$ operations. To do this, it needs to perform rotations in order to maintain the heap property. Refer to Figure 7.6. A rotation in a binary search tree is a local modification that takes a parent $ \ensuremath{\ensuremath{\mathit{u}}}$ of a node $ \ensuremath{\ensuremath{\mathit{w}}}$ and makes $ \ensuremath{\ensuremath{\mathit{w}}}$ the parent of $ \ensuremath{\ensuremath{\mathit{u}}}$, while preserving the binary search tree property. Rotations come in two flavours: left or right depending on whether $ \ensuremath{\ensuremath{\mathit{w}}}$ is a right or left child of $ \ensuremath{\ensuremath{\mathit{u}}}$, respectively.

Figure 7.6: Left and right rotations in a binary search tree.
\includegraphics[width=\textwidth ]{figs-python/rotation}

The code that implements this has to handle these two possibilities and be careful of a boundary case (when $ \ensuremath{\ensuremath{\mathit{u}}}$ is the root), so the actual code is a little longer than Figure 7.6 would lead a reader to believe:
\begin{leftbar}
\begin{flushleft}
\hspace*{1em} \ensuremath{\mathrm{rotate\_left...
...{r}}.\ensuremath{parent} \gets \ensuremath{nil}}\\
\end{flushleft}\end{leftbar}
In terms of the Treap data structure, the most important property of a rotation is that the depth of $ \ensuremath{\ensuremath{\mathit{w}}}$ decreases by one while the depth of $ \ensuremath{\ensuremath{\mathit{u}}}$ increases by one.

Using rotations, we can implement the $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ operation as follows: We create a new node, $ \ensuremath{\ensuremath{\mathit{u}}}$, assign $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{x}\gets \ensuremath{x}}$, and pick a random value for $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{p}}}$. Next we add $ \ensuremath{\ensuremath{\mathit{u}}}$ using the usual $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ algorithm for a BinarySearchTree, so that $ \ensuremath{\ensuremath{\mathit{u}}}$ is now a leaf of the Treap. At this point, our Treap satisfies the binary search tree property, but not necessarily the heap property. In particular, it may be the case that $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{parent}}.\ensuremath{\mathit{p}} > \ensuremath{\mathit{u}}.\ensuremath{\mathit{p}}}$. If this is the case, then we perform a rotation at node $ \ensuremath{\ensuremath{\mathit{w}}}$= $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{parent}}}$ so that $ \ensuremath{\ensuremath{\mathit{u}}}$ becomes the parent of $ \ensuremath{\ensuremath{\mathit{w}}}$. If $ \ensuremath{\ensuremath{\mathit{u}}}$ continues to violate the heap property, we will have to repeat this, decreasing $ \ensuremath{\ensuremath{\mathit{u}}}$'s depth by one every time, until $ \ensuremath{\ensuremath{\mathit{u}}}$ either becomes the root or $ \ensuremath{\ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{parent}}.\...
...}}} < \ensuremath{\ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{p}}}}$.
\begin{leftbar}
\begin{flushleft}
\hspace*{1em} \ensuremath{\mathrm{add}(\ensure...
...th{\ensuremath{\mathit{r}} \gets \ensuremath{u}}\\
\end{flushleft}\end{leftbar}
An example of an $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ operation is shown in Figure 7.7.

Figure 7.7: Adding the value 1.5 into the Treap from Figure 7.5.
\includegraphics[width=\textwidth ]{figs-python/treap-insert-a}
\includegraphics[width=\textwidth ]{figs-python/treap-insert-b}
\includegraphics[width=\textwidth ]{figs-python/treap-insert-c}

The running time of the $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ operation is given by the time it takes to follow the search path for $ \ensuremath{\ensuremath{\mathit{x}}}$ plus the number of rotations performed to move the newly-added node, $ \ensuremath{\ensuremath{\mathit{u}}}$, up to its correct location in the Treap. By Lemma 7.2, the expected length of the search path is at most $ 2\ln \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}+O(1)$. Furthermore, each rotation decreases the depth of $ \ensuremath{\ensuremath{\mathit{u}}}$. This stops if $ \ensuremath{\ensuremath{\mathit{u}}}$ becomes the root, so the expected number of rotations cannot exceed the expected length of the search path. Therefore, the expected running time of the $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ operation in a Treap is $ O(\log \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}})$. (Exercise 7.5 asks you to show that the expected number of rotations performed during an addition is actually only $ O(1)$.)

The $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{x}})}$ operation in a Treap is the opposite of the $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ operation. We search for the node, $ \ensuremath{\ensuremath{\mathit{u}}}$, containing $ \ensuremath{\ensuremath{\mathit{x}}}$, then perform rotations to move $ \ensuremath{\ensuremath{\mathit{u}}}$ downwards until it becomes a leaf, and then we splice $ \ensuremath{\ensuremath{\mathit{u}}}$ from the Treap. Notice that, to move $ \ensuremath{\ensuremath{\mathit{u}}}$ downwards, we can perform either a left or right rotation at $ \ensuremath{\ensuremath{\mathit{u}}}$, which will replace $ \ensuremath{\ensuremath{\mathit{u}}}$ with $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{right}}}$ or $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{left}}}$, respectively. The choice is made by the first of the following that apply:

  1. If $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{left}}}$ and $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{right}}}$ are both $ \ensuremath{\ensuremath{\mathit{nil}}}$, then $ \ensuremath{\ensuremath{\mathit{u}}}$ is a leaf and no rotation is performed.
  2. If $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{left}}}$ (or $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{right}}}$) is $ \ensuremath{\ensuremath{\mathit{nil}}}$, then perform a right (or left, respectively) rotation at $ \ensuremath{\ensuremath{\mathit{u}}}$.
  3. If $ \ensuremath{\ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{left}}.\en...
...h{\ensuremath{\mathit{u}}.\ensuremath{\mathit{right}}.\ensuremath{\mathit{p}}}}$ (or $ \ensuremath{\ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{left}}.\en...
...{\ensuremath{\mathit{u}}.\ensuremath{\mathit{right}}.\ensuremath{\mathit{p}}}})$, then perform a right rotation (or left rotation, respectively) at $ \ensuremath{\ensuremath{\mathit{u}}}$.
These three rules ensure that the Treap doesn't become disconnected and that the heap property is restored once $ \ensuremath{\ensuremath{\mathit{u}}}$ is removed.
\begin{leftbar}
\begin{flushleft}
\hspace*{1em} \ensuremath{\mathrm{remove}(\ens...
...ets \ensuremath{\ensuremath{\mathit{u}}.parent}}\\
\end{flushleft}\end{leftbar}
An example of the $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{x}})}$ operation is shown in Figure 7.8.
Figure 7.8: Removing the value 9 from the Treap in Figure 7.5.
\includegraphics[height=.25\textheight ]{figs-python/treap-delete-a}
\includegraphics[height=.25\textheight ]{figs-python/treap-delete-b}
\includegraphics[height=.25\textheight ]{figs-python/treap-delete-c}
\includegraphics[height=.25\textheight ]{figs-python/treap-delete-d}

The trick to analyze the running time of the $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{x}})}$ operation is to notice that this operation reverses the $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ operation. In particular, if we were to reinsert $ \ensuremath{\ensuremath{\mathit{x}}}$, using the same priority $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{p}}}$, then the $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ operation would do exactly the same number of rotations and would restore the Treap to exactly the same state it was in before the $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{x}})}$ operation took place. (Reading from bottom-to-top, Figure 7.8 illustrates the addition of the value 9 into a Treap.) This means that the expected running time of the $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{x}})}$ on a Treap of size $ \ensuremath{\ensuremath{\mathit{n}}}$ is proportional to the expected running time of the $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ operation on a Treap of size $ \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}-1$. We conclude that the expected running time of $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{x}})}$ is $ O(\log \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}})$.

7.2.1 Summary

The following theorem summarizes the performance of the Treap data structure:

Theorem 7..2   A Treap implements the SSet interface. A Treap supports the operations $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$, $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{x}})}$, and $ \ensuremath{\mathrm{find}(\ensuremath{\mathit{x}})}$ in $ O(\log \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}})$ expected time per operation.

It is worth comparing the Treap data structure to the SkiplistSSet data structure. Both implement the SSet operations in $ O(\log \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}})$ expected time per operation. In both data structures, $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ and $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{x}})}$ involve a search and then a constant number of pointer changes (see Exercise 7.5 below). Thus, for both these structures, the expected length of the search path is the critical value in assessing their performance. In a SkiplistSSet, the expected length of a search path is

$\displaystyle 2\log \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}} + O(1) \enspace ,
$

In a Treap, the expected length of a search path is

$\displaystyle 2\ln \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}} +O(1) \approx 1.386\log \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}} + O(1) \enspace .
$

Thus, the search paths in a Treap are considerably shorter and this translates into noticeably faster operations on Treaps than Skiplists. Exercise 4.7 in Chapter 4 shows how the expected length of the search path in a Skiplist can be reduced to

$\displaystyle e\ln \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}} + O(1) \approx 1.884\log \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}} + O(1)
$

by using biased coin tosses. Even with this optimization, the expected length of search paths in a SkiplistSSet is noticeably longer than in a Treap.



Footnotes

... interface.7.2
The names Treap comes from the fact that this data structure is simultaneously a binary search tree (Section 6.2) and a heap (Chapter 10).
... rank,7.3
The rank of an element $ \ensuremath{\ensuremath{\mathit{x}}}$ in a set $ S$ of elements is the number of elements in $ S$ that are less than $ \ensuremath{\ensuremath{\mathit{x}}}$.
opendatastructures.org