Node *deleteFromTree(Node *root, int data)
{
if (root == NULL)
{
return NULL;
}
if (root->data == data)
{
// case where er are trying to delete the leaf node
if (root->left == NULL && root->right == NULL)
{
delete root;
cout << "deleted leaf element" << endl;
return NULL;
}
// case where we are trying to delete node having only left child
if (root->left != NULL && root->right == NULL)
{
Node *successor = root->left; // handle links linked with that node for further management
delete root;
cout << "deleted element having only left childrens" << endl;
return successor;
}
if (root->right != NULL && root->left == NULL)
{
Node *successor = root->right; // handle links linked with that node for further management
delete root;
cout << "deleted element having only right childrens" << endl;
return successor;
}
if (root->right != NULL && root->left != NULL)
{
int maxRight = (findMax(root->left))->data; // first finding maximum element fromleft subtree
root->data = maxRight; // assigining tahat max data to root
root->left = deleteFromTree(root->left, maxRight); // delete the node whose connection exists still
return root;
}
}
else
{
if (data >= root->data)
{
// bigger search in right
root->right = deleteFromTree(root->right, data);
}
else
{
// smaller search nin left subtree
root->left = deleteFromTree(root->left, data);
}
}
}
actually i have dout as my code has condition which call recursively left and right as per the condtion in left subtree to find maximum element from bst and replace it with node which we want to delete but while calling right section recursively when i get node like leaf node i return null at that point to that call stack but previous stack not getting any value still my code works fine and appends appropriate childs to their position how it working is my problem?