Removing (generally speaking) is not commutative. Here is a counterexample:
4 / \ 3 7 / 6
What if we remove 4 and then 3?
When we remove 4, we get 6 as the new root:
6 / \ 3 7
Removing 3 does not change the tree, but gives us the following:
6 \ 7
What if we remove 3 and then 4?
When we delete 3, the tree does not change:
4 \ 7 / 6
However, when we now remove 4, the new root becomes 7:
7 / 6
The two resulting trees do not match, so deletion is not commutative.
UPDATE
I have not read the limitations that this is when you always delete a node with two children. My solution for the general case. I will update it if / when I find a counter example.
OTHER UPDATE
I have no concrete evidence, but I'm going to be afraid:
In general, you handle deletions differently depending on whether you have two children, one child, or no children. In the above counterprocess example, I first delete the node with two children, and then the node with one child. After that I will remove the node without children, and then another node with one child.
In the special case, only to remove nodes with two children, you should consider the case when both nodes are in the same subtree (since it doesn’t matter if they are in different subtrees, you can make sure that the general structure does not change depending on the order removal). What you really need to prove matters: Does the order of deleting nodes in the same subtree, where each node has two children, have?
Consider two nodes A and B, where A is the ancestor of B. Then you can further clarify the question:
Is a deletion commutative when you consider removing two nodes from a binary search tree that are related between ancestors and descendants of each other (this means that they are in the same subtree)?
When you delete the node (let them say A), you go over the right subtree to find the minimum element. This node will be a leaf of a node and can never be equal to B (since B has two children and cannot be a leaf of a node). Then you replace the value of A with the value of this sheet - node. This means that the only structural change to the tree is to replace the value of A with the value of the leaf node and the loss of the leaf node.
The same process is used for B. That is, you replace the node value and replace the leaf-node. In the general case, when you delete a node with two children, the only structural change is to change the value of the node that you are deleting, and delete the leaf node, whose value you are using as a replacement.
So, the question is being clarified:
Can you guarantee that you will always have a node replacement regardless of the order of removal (when you always delete a node with two children)?
The answer (I think) is yes. What for? Here are a few notes:
- Suppose you first remove the descendant node and the ancestor node. The subtree that was changed when you deleted the node descendant is not in the left subtree of the child child of the node ancestor. This means that this subtree remains unaffected. What this also means, regardless of the order of deletion, is changing two different subtrees, and therefore the operation is commutative.
- Again, let's say you delete the descendant node first and the ancestor node second. The subtree that was changed when you deleted the descendant node is in the left subtree of the child child of the node ancestor. But even here there is no overlap. The reason is that first you delete the descendant node, you look at the left subtree of the child child of the child node. When you remove the ancestor of a node, you will never go down from this subtree, since you will always go left after you enter the right child tree of the predecessor node. So, again, no matter what you delete first, you change the different subtrees, and therefore it seems that the order doesn't matter.
- Another thing is if you first remove the ancestor node, and you find that the minimum node is a descendant of the descendant node. This means that the child of the node will be in one child, and the removal of one child is trivial. Now, consider the case where in this scenario you first deleted the node descendant. Then you replace the value of the descendant node with its right child and then delete the correct child. Then, when you delete the ancestor of the node, you get the same minimum node (the old remote node is the left child, which is also the replaced node left child). In any case, you get the same structure.
This is not a rigorous proof; These are just some of the comments I made. By all means, feel free to breathe!