Delete nodes from bst

what is error in it

@vikashkmr519,
Your tree construction is wrong. The nodes have to be added to tree in the order in which they are given.

you need to write an “add” function which will add the nodes according to the order given.
I have given a start
void add(node root,int item) {
if(root.data<item) {
if(root.right==null) {
node node =new node();
node.data=item;
root.right=node;
}else {
add(root.right,item);
}
}else { … Please complete the function.

https://ide.codingblocks.com/s/168256 i have made the changes in the BST constructir and creted the add() function also but now it is giving run error please tell me what is the error

@vikashkmr519,
In your original code for the input
1
5
99 46 18 42 49
1
99
This will give a null pointer exception because parent.right does not exist and is null. Hence the null pointer exception.
https://ide.codingblocks.com/s/171501 Here is the corrected code. In this the delete function is without parent and in your main, there needs to be a t-- at the end. Else it will run to infinity.

what we do in BST while deleting nodes is , we swap it with the smallest Node of right subtree or largests node of left subtree, but in your delete function , i dry run it , and it is not able to delete 46 in the above testCase you have given , please once you also dry run , and tell me whether i m wrong or not , if wrong then please explain me.

@vikashkmr519,
If input is
1
5
99 46 18 42 49
1
46
Tree will be:

                      99
                      /      
                     46
                    /  \
                   18  49
                     \
                     42

Answer will be:

                      99
                      /      
                     49
                    /  
                   18  
                     \
                      42

We find successor by finding the minimum value in right child of the node.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.