3
$\begingroup$

I have a Treap, and want to bulk delete nodes in a given key range (i.e. the nodes to be deleted are consecutive nodes in an in-order walk of the tree). If I have $n$ nodes in the Treap, and $k$ nodes are to be deleted, is there an algorithm that can let me do this in $O(k + \log n)$. Or maybe provide a lower-bound proof that shows that that is impossible?

The obvious algorithm seems to be the one where the following steps are performed:

  1. The treap is first split into 2 treaps by rotating the predecessor of the first node to be deleted to the root: $O(\log n)$
  2. Detach the right child of the root: $O(1)$
  3. Rotate the successor of the last node to be deleted in the detached treap and throw away the left subtree of this treap: $O(\log n)$
  4. Meld the 2 remaining treaps: $O(\log n)$

However, I'm worried that the treap will get skewed (or will they)?

$\endgroup$
  • 1
    $\begingroup$ It may be worth mentioning explicitly that it's very easy to do bulk deletes in $O(\log n)$ while maintaining the treap property. The issue is that the probabilistic performance bounds will no longer apply. Whether or not that's an issue in your algorithm depends on how you meld the treaps. $\endgroup$ – SamM Feb 23 '14 at 17:24
  • $\begingroup$ @SamM Hello! :) So can we prove that the above algorithm doesn't maintain the expected height bounds? I can intuitively see that it doesn't. Is it possible to "fix" the algorithm by performing some rotations post the meld operation? $\endgroup$ – dhruvbird Feb 24 '14 at 3:14
  • 1
    $\begingroup$ The operation you described (removing a set of consecutively-ranked keys) is called excision. And yes, you can do that in treaps. See Section 5.9 of the journal paper. $\endgroup$ – Maverick Woo Mar 21 '14 at 21:17
3
$\begingroup$

Section 2 of the original treaps paper ("Randomized Search Trees" by Aradon and Seidel), as well as the journal version of the same paper (also called "Randomized Search Trees"), explains how to split and join treaps without affecting the validity of the randomized balancing. That's all you need to bulk delete.

In order to split a treap storing $X$ according to some $a$, simply insert an item with key $a$ and "infinite" priority. By the heap-order property the newly inserted item will be at the root of the new treap. By the in-order property the left subtree of the root will be a treap for $X_1$ and the right subtree will be a treap for $X_2$. In order to join the treaps of two sets $X_1$ and $X_2$ as above, simply create a dummy root whose left subtree is a treap for $X_1$ and whose right subtree is a treap for $X_2$, and perform a delete operation on the dummy root.

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ That talks about deleting a single node from a treap. I'm talking about deleting a set of nodes. I'm assuming that the assumption when you join 2 treaps is that they were the result of an earlier split, and no change to them has occurred in between (in terms of adding/deleting nodes and/or height changes). $\endgroup$ – dhruvbird Feb 26 '14 at 16:18
  • 1
    $\begingroup$ This uses deleting a single node in order to perform split. In order to bulk delete, perform two splits, partitioning A into A1 and A2, then A2 into A3 and A4, Leaving A1, A3 and A4. Then join A3 and A4. You assumption about what is required to join two treaps is incorrect - any two treaps with keys in order can be joined this way. $\endgroup$ – jbapple Feb 27 '14 at 4:44
  • $\begingroup$ Sorry, I meant, "Join A1 and A4". $\endgroup$ – jbapple Feb 27 '14 at 5:51
  • $\begingroup$ I get it now. The final deletion after merging is the key step required to preserve the expected bounds on the running time of the subsequent treap operations. Thanks! :) $\endgroup$ – dhruvbird Mar 22 '14 at 1:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.