How to delete node in a Binary Search Tree

You must be wondering what makes this so hard, just chuck a node and that’s it. Well, not that easy..not that easy. The major issue is to delete a node in such a way that the property of Binary Search Tree is also preserved.

example
searching our node
tweaked form of search function
handling case 1 : 
1> it has right child: then we can return right child
2> it has left child: then we can return left child.
handling case 2:
1> The idea behind a B.S.T. is that every node on the left is smaller and every node on the right is greater. What we can do is find the left most node for the right child and then attach our left child to that left most node. It won't destroy the structuring of our binary search tree, let's understand from an example.
5
/ \
3 6
/ \
2 4
\
1
after deletion:        5
/ \
4 6
/
2
\
1
reconnect() fun
final solution
final solution

--

--

I write articles on Android Development, and problem solving.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store