Skip to main content

Section 32.11 Treaps

Another data structure that uses random information to maintain balance is the treap. A treap is a binary search tree that also maintains a heap property. Only instead of the heap property being based on the values of the nodes, it is based on a separate priority value that is randomly selected for each node as it is inserted.
The root node has a value of 835 and a priority of 882. It’s children both have lower priorities. The left child has value 54 and priority 754, and the right child has value 912 and priority 774.πŸ”—
Figure 32.11.1. A treap. Each node has a priority value (shown next to the node) and a value (shown inside the node). The structure is a valid binary search tree with respect to the values and a valid heap with respect to the priorities. (The nodes are also colored based on their priorities. Darker blue indicates higher priority.)
Every time a value is added, we generate a random priority for it. We then insert the new node into the tree based on its value, just like we would in a normal binary search tree. After inserting the node, we check if the heap property is violated. If the new node’s priority is greater than its parent’s priority, we perform rotations to move it up the tree until the heap property is restored.

Activity 32.11.1. Treap Structure.

The treap below loads the values 100, 200, 300, 400, 500, and 600 in that order. In a plain BST that would produce a linear chain of nodes.
But because each node gets a random priority, the structure of the tree the Treap produces is different. Press the Reset button a few times to see how the structure changes from run to run as the random priorities change.
Then try inserting other values. As a value is placed in a leaf position, look at its priority. Compare that to the priority of its parents and try to guess its final position before stepping through the rotations.

Instructions.

When the > button is highlighted, an animation is prepared to run. Use the Step button to step through the animation one step at a time.
When no animation is in progress, you can use the data controls to start a new animation.
You can click and drag on the animation area to pan around. Use the Ctrl + mouse wheel (or touchpad scroll gesture) while over the animation area to zoom in and out.
  • Core Controls.
    • << : Skip to the beginning of the animation.
    • < : Step backwards one step in the animation.
    • > : Step forward one step in the animation.
    • >> : Skip to the end of the animation.
    • Auto Step Speed Set to anything other than off to automatically step through the animation at the selected speed.
    • Zoom Set the zoom level. You can also use Ctrl + Mouse Wheel to zoom in and out.
  • Data Controls.
    • Value Use this area to enter a value when inserting, finding, deleting, etc...
This process means that any given node can end up as the root node, not just the first node we insert. In addition, each time a node moves up through the heap it also changes the structure of the tree. Although not every change will make the tree more balanced, on average, the random priorities will help to keep the tree balanced.
In fact, it can be shown that the expected height of a treap, and thus the average time complexity for search, insertion, and deletion operations is \(O(\log n)\text{.}\) However, this is a probabilistic guarantee, not a strict guarantee. It is possible to get unlucky with the random priorities and end up with a very unbalanced tree. In the worst case, the height (and thus the common operations) could end up being \(O(n)\text{.}\)

Insight 32.11.1.

A treap has a probabilistic average time complexity of \(O(\log n)\) for insert/find/delete, but the worst-case time complexity can be \(O(n)\text{.}\)
You have attempted of activities on this page.