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 22.

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 1-1:

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

Since 33 is to the right of 22, 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 11 above the node with key 22. Since 11 is to the left of 22, we perform a right rotation:

We can easily remove the node with key 22 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 O(lgn)\Omicron(\lg n).