ide.codingblocks.com/s/437633 ,
please check my code ,what is problem in it?
DELETE NODES IN BST
Have updated your deleteBST function, check it here. But the way you are making your tree, isn’t right.
Below this deleteinbst code, i have attached build function you can consider that too.
node* deleteINBST(node* root ,int data)
{
if(root==NULL)
{
return NULL;
}
else if(data<root->data)
{
root->left=deleteINBST(root->left,data);//update
return root;
}
else if(root->data==data)
{
if(root->left==NULL && root->right==NULL)
{
delete root;
return NULL;
}
if(root->left!=NULL && root->right==NULL)
{
node* temp=root->left;
delete root;
return temp;
}
if(root->right!=NULL && root->left==NULL)
{
node* temp= root->right;
delete root;
return temp;
}
if(root->right!=NULL && root->left!=NULL)
{
node*replace=root->right;
node*temp2;
while(replace->left!=NULL)
{
/*if(replace->left->left==NULL)
{
temp2=replace;
}*/
replace =replace->left;
}
root->data=replace->data;
root->right = deleteINBST(root->right,replace->data);
//delete replace;
//temp2->left=NULL;
return root;
}
}
//else
//{
root->right=deleteINBST(root->right,data);
return root;
//}
}
Code to construct tree
And you are saying that way of construct a tree is also not right ? but why ??
My bad, input format is correct, it’s just you were doing node * root=deleteINBST(root,a2[i]); instead you should have done root=deleteINBST(root,a2[i]); also in deleteinbst root should be pass by reference.
I have modified your previous code which i said was wrong. You can check.
Now , only one test case is passing?
That’s because of the output format, for every test case you have to print tree in seperate line. In your pre-order print function add endl so that every test case, prints tree in a new line.
Hanji ho gya bhaiya , thank you so much
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.