Treap Deletion
Deleting a node from a treap is also much simpler than it is with a red-black tree. The trick is that we can use rotations to move the node we’re deleting downward through the treap until it becomes a leaf node (that is, it has no children). Once this node becomes a leaf, we simply detach it from the treap.
Here’s the specific algorithm:
-
First, we search the tree for the node with the value that we intend to delete. We’ll call this node the node_to_delete.
-
If we find this node, we run a loop that lasts as long as the node_to_delete has children.
-
Within this loop, we rotate the node_to_delete with its child. If the node_to_delete happens to have two children, we rotate it with the child that has the smaller priority. (I’ll explain ...
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