0

I was trying to code deletion of node in BST and am now bit confused about the case where node has right_child only.

Most of the algorithms handle 4 cases:

  • If it's the leaf node, delete it

  • If the node has only left_child, promote it

  • If the node has only right_child, promote it

  • If node has 2 children, replace the value with inorder_successor of node, and delete the successor

When I was trying to code it, I handled following 3 cases:

  • If it's the leaf node, delete it

  • If the node doesn't have right_child, promote the left child

  • Else, replace the value with the inorder_successor of the node, and delete the successor

Eventually, the resulting BST will be different for the following case:

- With the way I delete node with right child only:

       1        Delete(1)      3
         4     ----------->      4
       3

- The way other algos suggest:
       1        Delete(1)        4
         4     ----------->    3
       3

I can't seem to find if there is anything wrong with the way I am deleting it

Sure, the BST could be more right-skewed and more deletions would happen recursively, but the BST invariant is still intact, right ?

There was some discussion here and here but there is no accepted answer

mittal
  • 915
  • 10
  • 29

1 Answers1

0

When I was trying to code it, I handled following 3 cases ...

It is more natural to add a case:

  • If it's the leaf node, delete it
  • If the node doesn't have right_child, promote the left child
  • If the node doesn't have left_child, promote the right child
  • Else, replace the value with the inorder_successor of the node, and delete the successor

This limits the use of the algorithm in the last bullet point to the very minimum, as that is the more costly one.

However, there is not just one way to get a valid BST, so even if you skip that case and use the "Else" algorithm for it, you still will get a valid BST.

trincot
  • 317,000
  • 35
  • 244
  • 286
  • My question is why do we need to handle that extra case. What's wrong with the way I am handling it – mittal May 02 '22 at 09:38
  • 1
    Well if you don't treat that case, you will get a different result. Not wrong, just different. It is also involving more operations. It is natural to include the extra case, as it is the mirror of the previous one, and it involves less work than doing the final case. – trincot May 02 '22 at 09:46
  • Yes, a different result is fine as long as BST invariant is intact. More operations and potential right-skew possibility I understand. But it would still be a valid BST, right – mittal May 02 '22 at 09:52
  • Yes, it would be valid. – trincot May 02 '22 at 09:54
  • Thanks for confirming this. Just wanted to validate my algorithm. You know, in an interview setup it is sometimes difficult to defend your algorithm if it deviates from the standard and you get into a debate with the interviewer :) If they are open-minded and willing to understand then it's fine but if they are just going by the book, they won't even listen :P – mittal May 02 '22 at 09:59
  • 1
    Sure, but you'll make a better impression if you also listen to the arguments as to why it really is better to have the case. You don't want to give the impression that you prefer shorter code over efficient code. – trincot May 02 '22 at 10:00
  • Sure, that is why in my original post I added the condition that my algorithm will have more operations and can be right-skewed but is still correct. It is definitely not more efficient but is not wrong either – mittal May 02 '22 at 10:13