I’m trying to delete the node in BST which have two child nodes.
For example,
|
12
/ \
5 15
/ \ \
2 6 20
I want to delete node contain info=12. I need help to perform this operation.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You need to get either the right-most child of its left-subtree, or else the left-most child of its right-subtree (6 or 15 in your example), and move one of them to that position, then you can delete the node you want to.
If you’re doing anything to keep track of the number of nodes in the sub-trees, you generally want to pick the node from the larger subtree, so when you move it, the tree will be at least as well balanced as it started. For example, in this case it would be better to get 6 than 15 to help preserve the balance — but if you just have a plain, unbalanced BST, you may not have that information easily available.