Treap: Deletion
- Explain and trace the balancing operations of a Treap.
- Explain the role of the priorities in rebalancing and the importance of being random.
Consider the following (max-heap) treap:

Let's remove the node with key .
We find the entry to be removed (look-up as in BST, ignoring priorities). Since we have a "max-heap" teap and the priorities are non-negative, we set the priority of the entry to be removed to :

The max-heap order property is violated. To fix it, we need to bring the child node with key above the node with key (because has a higher priority between the children of ).
Since is to the right of , we perform a left rotation:

The max-heap order property is still violated. To fix it, we need to bring the child node with key above the node with key . Since is to the left of , we perform a right rotation:

We can easily remove the node with key as it is a leaf now.

Notice the resulting treap is a BST over the keys and a max-heap over the priorities, and its height is .