Andreas BH Andreas BH - 1 month ago 16
C++ Question

C++ Delete node from binary search tree

I have built a binary search tree, and inserted some random value nodes. I am trying to implement a function to delete the nodes, but for some reason it doesn't work. When trying to delete a given node, it seems that the parent node of the deleted node and the child node of the deleted node won't "connect".

Can anyone see what I've done wrong? I've tried debugging the program multiple times to see where my mistake is, but I don't understand how I can link the parent and the child together.

Here's my program:

#include <iostream>
using namespace std;

struct Node
{
int data;
Node* left;
Node* right;
};

Node* insertNode(Node* root, int n);
Node* newNode(int d);
Node* deleteNode(Node* root, int d);
Node* findMin(Node* root);

int main()
{
int num;
Node* rootPtr = NULL;
Node* min;

rootPtr = insertNode(rootPtr, 15);
rootPtr = insertNode(rootPtr, 10);
rootPtr = insertNode(rootPtr, 20);
rootPtr = insertNode(rootPtr, 24);
rootPtr = insertNode(rootPtr, 7);
rootPtr = insertNode(rootPtr, 25);
rootPtr = insertNode(rootPtr, 5);

rootPtr = deleteNode(rootPtr, 7);

cout << "\nEnter a number to search for: ";
cin >> num;
if(search(rootPtr, num))
cout << "\nFound.";
else
cout << "\nNot found.";

cout << endl;
return 0;
}

Node* insertNode(Node* root, int n)
{
if(root == NULL)
root = newNode(n);
else if(n <= root->data)
root->left = insertNode(root->left, n);
else if(n > root->data)
root->right = insertNode(root->right, n);
return root;
}

Node* newNode(int d)
{
Node* newNode = new Node();
newNode->data = d;
newNode->left = newNode->right = NULL;
return newNode;
}

bool search(Node* root, int d)
{
if(root == NULL)
return false;
else if(root->data == d)
return true;
else if(d < root->data)
return search(root->left, d);
else if(d > root->data)
return search(root->right, d);
}

Node* deleteNode(Node* root, int d)
{
if(root == NULL)
return root;
else if(d < root->data)
deleteNode(root->left, d);
else if(d > root->data)
deleteNode(root->right, d);
else
{
if(root->left == NULL && root->right == NULL)
{
delete root;
root = NULL;
}
else if(root->left == NULL)
{
Node* temp = root;
root = root->right;
delete temp;
}
else if(root->right == NULL)
{
Node* temp = root;
root = root->left;
delete temp;
}
else
{
Node* temp = findMin(root->right);
root->data = temp->data;
root->right = deleteNode(root->right, temp->data);
}
}
return root;
}

Node* findMin(Node* root)
{
if(root == NULL)
cout << "\nThe tree is empty.";
else
{
Node* temp = root;
while(temp->left != NULL)
temp = temp->left;
return temp;
}
}

Answer

In the deleteNode() function, the nodes are not getting connected in the return path of the recursion. You might need to use the return value of the function like you did for insertNode(). For example,

else if(d < root->data)
    deleteNode(root->left, d);
else if(d > root->data) 
    deleteNode(root->right, d);

might be (something like)

else if(d < root->data)
    root->left = deleteNode(root->left, d);
else if(d > root->data) 
    root->right = deleteNode(root->right, d);

Also, the caller of findMin() might need both the min node and its parent. Let it return both. In deleteNode() you might need to set one of the child pointer of parent to NULL.

Comments