# Deletion by merging in binary search tree

• June 24th, 2013, 09:36 AM
Quentin026
Deletion by merging in binary search tree
Hi there,

I'm reading about a technique known as deletion by merging in context of binary search trees. Here's the PDF:

and the code for 2 related functions: deleteByMerging and findAndDeleteByMerging, is on page 4-5. The book, from which the PDF comes from, says that:

Quote:

Using search() in function findAndDeleteByMerging() is a treacherous simplification. search() returns a pointer to the node containing el. In findAndDeleteByMerging(), it is important to have this pointer stored specifically in one of the pointers of the node's parent. In other words, a caller to search() is satisfied if it can access the node from any direction, whereas findAndDeleteByMerging() wants to access it from either its parent's left or right pointer data member. Otherwise, access to the entire subtree having this node as its root would be lost.
This quote pertains probably to this snippet on page 5:

Code:

```if(node != 0 && node->el == el)     if(node == root)         deleteByMerging(root);     else if(prev->left == node)         deleteByMerging(prev->left);     else         deleteByMerging(prev->right);```
I don't get it, why is it necessary to have 4 if-clauses? Wouldn't one, the outer, be sufficient? I mean, if prev->left == node or if prev->right == node then one way or another, we pass node in the call to deleteByMerging.

Wouldn't it be much simpler to just write:

Code:

```if(node != 0 && node->el == el)     deleteByMerging(node);```
?
• July 23rd, 2013, 02:15 AM
razzle
Re: Deletion by merging in binary search tree
Quote:

Originally Posted by Quentin026
I don't get it, why is it necessary to have 4 if-clauses?

Well, it certainly looks suspicious. Still what you suggest is not equivalent to the original code.

In the original code if node is neither root nor prev->left then deleteByMerging(prev->right) will be called, whereas in your suggestion deleteByMerging(node) would be called.

It may be significant, I cannot say.
• July 23rd, 2013, 06:22 AM
Eri523
Re: Deletion by merging in binary search tree
I didn't look at the original code, but I guess the parameter passed to deleteByMerging() is a non-const reference. In this case, even if prev->left == node, it does matter which of the two is passed: It determines which object gets modified by the function call.