How can you delete a node in Binary Search Tree?
Let x be the node that has to be deleted from a binary search tree T. So, to delete the node x, we can have three cases:
- x has no children
- x has one child
- x has two children
Case 1: x has no children
Let us take the first case first. When x has no children the deletion is fairly trivial. Take an example of following tree:
Now, to delete node B, all you have to do is go to the parent of B, which is A, and change the right pointer to null.
Similarly, if B were the left child of A you could go to the parent of B and change the left pointer to null. So if the node that has to be delete is a leaf node its fairly straightforward.
Case 2: x has one child
If the node that you are trying to delete has only one child, the deletion process is similar to deleting a node from linked list. Look at the following example:
Here, we are deleting node A which has a child B. You can delete node A (pointed by x) by going to the parent of A and changing its left pointer point to B. This will not violate the binary search property because everything on the left of D is smaller than D.
Case 3: x has two children
Let D be the node we need to delete which has two children A and F.
If a node to be deleted has two children and let x is the reference that points to the node that has to be deleted then you need to follow the procedure below:
- Find the predecessor (or successor) of x, call it y.
- Remove the y ( note that y has only one child at most because it is the predecessor of x.
- Replace x with y and delete predecessor node.
To find the predecessor just to to the left subtree of node and keep going through all the right nodes of left subtree.
Node with value B is the predecessor for our example pointed by y. So, replace D (pointed by x) with B and delete the node pointed by y.
If the node pointed by y (B in figure) has left subtree then the right child pointer is updated to point to the subtree of B.
With these processes we can delete a node in Binary Search Tree.
If you find this article useful, you are welcome to comment, suggest and add value to the topic.


3 Responses to “How can you delete a node in Binary Search Tree?”
Dipanjan Saha on March 14, 2011
I like these helps which has proven 2 b useful 2 me in doing the programs of Data Structures. I will b grateful 2 u if u attach the pseudocodes/algorithms alongwith these steps which would prove 2 b sufficient resource for any student of Computer Science Engineering…
Vikas on March 11, 2012
its awesome y the way what r u doing now a days i mean engineering or teaching? reply sooner……….
shkhanal on March 12, 2012
Dear Vikas,
I’m trying to offer true help to the candidates of Computer Operator Examination and the students.