views:

67

answers:

2

Consider the deletion procedure on a BST, when the node to delete has two children. Let's say i always replace it with the node holding the minimum key in its right subtree.

The question is: is this procedure commutative? That is, deleting x and then y has the same result than deleting first y and then x?

I think the answer is no, but i can't find a counterexample, nor figure out any valid reasoning.

EDIT:

Maybe i've got to be clearer.

Consider the transplant(node x, node y) procedure: it replace x with y (and its subtree). So, if i want to delete a node (say x) which has two children i replace it with the node holding the minimum key in its right subtree:

y = minimum(x.right)
transplant(y, y.right) // extracts the minimum (it doesn't have left child)
y.right = x.right
y.left = x.left
transplant(x,y)

The question was how to prove the procedure above is not commutative.

+2  A: 

Deletion (in general) is not commutative. Here is a counterexample:

    4
   / \
  3   7
     /
    6

What if we delete 4 and then 3?

When we delete 4, we get 6 as the new root:

   6
  / \
 3   7

Deleting 3 doesn't change the tree, but gives us this:

  6
   \
    7

What if we delete 3 and then 4?

When we delete 3 the tree doesn't change:

 4
  \
   7
  /
 6

However, when we now delete 4, the new root becomes 7:

  7
 /
6

The two resulting trees are not the same, therefore deletion is not commutative.

UPDATE

I didn't read the restriction that this is when you always delete a node with 2 children. My solution is for the general case. I'll update it if/when I can find a counter-example.

ANOTHER UPDATE

I don't have concrete proof, but I'm going to hazard a guess:

In the general case, you handle deletions differently based on whether you have two children, one child, or no children. In the counter-example I provided, I first delete a node with two children and then a node with one child. After that, I delete a node with no children and then another node with one child.

In the special case of only deleting nodes with two children, you want to consider the case where both nodes are in the same sub-tree (since it wouldn't matter if they are in different sub-trees; you can be sure that the overall structure won't change based on the order of deletion). What you really need to prove is whether the order of deletion of nodes in the same sub-tree, where each node has two children, matters.

Consider two nodes A and B where A is an ancestor of B. Then you can further refine the question to be:

Is deletion commutative when you are considering the deletion of two nodes from a Binary Search Tree which have a ancestor-descendant relationship to each other (this would imply that they are in the same sub-tree)?

When you delete a node (let's say A), you traverse the right sub-tree to find the minimum element. This node will be a leaf node and can never be equal to B (because B has two children and cannot be a leaf node). You would then replace the value of A with the value of this leaf-node. What this means is that the only structural change to the tree is the replacement of A's value with the value of the leaf-node, and the loss of the leaf-node.

The same process is involved for B. That is, you replace the value of the node and replace a leaf-node. So in general, when you delete a node with two children, the only structural change is the change in value of the node you are deleting, and the deletion of the leaf node who's value you are using as replacement.

So the question is further refined:

Can you guarantee that you will always get the same replacement node regardless of the order of deletion (when you are always deleting a node with two children)?

The answer (I think) is yes. Why? Here are a few observations:

  • Let's say you delete the descendant node first and the ancestor node second. The sub-tree that was modified when you deleted the descendant node is not in the left sub-tree of the ancestor node's right child. This means that this sub-tree remains unaffected. What this also means is regardless of the order of deletion, two different sub-trees are modified and therefore the operation is commutative.
  • Again, let's say you delete the descendant node first and the ancestor node second. The sub-tree that was modified when you deleted the descendant node is in the left sub-tree of the ancestor node's right child. But even here, there is no overlap. The reason is when you delete the descendant node first, you look at the left sub-tree of the descendant node's right child. When you then delete the ancestor node, you will never go down that sub-tree since you will always be going towards the left after you enter the ancestor node's right-child's left sub-tree. So again, regardless of what you delete first you are modifying different sub-trees and so it appears order doesn't matter.
  • Another case is if you delete the ancestor node first and you find that the minimum node is a child of the descendant node. This means that the descendant node will end up with one child, and deleting the one child is trivial. Now consider the case where in this scenario, you deleted the descendant node first. Then you would replace the value of the descendant node with its right child and then delete the right child. Then when you delete the ancestor node, you end up finding the same minimum node (the old deleted node's left child, which is also the replaced node's left child). Either way, you end up with the same structure.

This is not a rigorous proof; these are just some observations I've made. By all means, feel free to poke holes!

Vivin Paliath
oh, yes, it was pretty simple. Thank you!
Metz
How did the new root become 7 in the 2nd case after deleting 4? Shouldn't it be 6? After deleting 4, the next minimum element in the right subtree of 4 is 6, so the root changes to 6? In the above eg. deletion of 3 and 4 or deletion of 4 and 3 gives the same result. However, I am not sure if it works as a general rule.
srikfreak
In the second case, you are deleting 4. 4 is a node with one child. When you delete a node with one child (no need to search for the right node, since you can be sure that the only child is lesser or greater depending on whether it is the right or left child), you can replace the node with its child. More information here: http://en.wikipedia.org/wiki/Binary_search_tree#Deletion
Vivin Paliath
Like @Vivin said, this isn't following the restriction that you never delete a node with less than 2 children.
IVlad
Mmh, Mad is right, that was simple because the second node has not 2 children. i untick vivin's answer. I apologize for my inattention.
Metz
@Metz, I apologize for *my* inattention - didn't read the restriction that this is for two children. I thought it was the general case. :)
Vivin Paliath
A: 

I respond here to Vivin's second update.

I think this is a good recast of the question:

Is deletion commutative when you are considering the deletion of two nodes from a Binary Search Tree which have a ancestor-descendant relationship to each other (this would imply that they are in the same sub-tree)?

but this bold sentence below is not true:

When you delete a node (let's say A), you traverse the right sub-tree to find the minimum element. This node will be a leaf node and can never be equal to B

since the minimum element in A's right subtree can have a right child. So, it is not a leaf. Let's call the minimum element in A's right subtree successor(A). Now, it is true that B cannot be successor(A), but it can be in its right subtree. So, it is a mess.

I try to summarize.

Hypothesis:

  1. A and B have two children each.
  2. A and B are in the same subtree.

Other stuff we can deduce from hypothesis:

  1. B is not successor(A), neither A is successor(B).

Now, given that, i think there are 4 different cases (as usual, let be A an ancestor of B):

  1. B is in A's left subtree
  2. B is an ancestor of successor(A)
  3. successor(A) is an ancestor of B
  4. B and successor(A) don't have any relationship. (they are in different A's subtrees)

I think (but of course i cannot prove it) that cases 1, 2 and 4 don't matter. So, only in the case successor(A) is an ancestor of B deletion procedure could not be commutative. Or could it?

I pass the ball : )

Regards.

Metz