Treap Insertion
We’re now ready to see how treap insertions work. Like a red-black tree, treap insertion goes through two phases (as discussed in Red-Black Tree Insertion). In the first phase, the new node is inserted in the same way a node is inserted into a regular BST, and in the second phase, the tree is “fixed” so that it doesn’t violate any of the rules.
Let’s insert the letter V into the treap shown at the end of the previous section. I mentioned earlier that with randomized treaps, a node’s priority is randomly generated. So, let’s say that the randomly generated priority is 22.
The First Phase
To begin the first phase of insertion, we first compare the V against the root’s value of M:
Because the value V is “greater” than the value ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access