I want to implement the following function: delete a node of certain value in a binary search tree. I want to do it by two steps: 1.find the node of the value 2.delete the node.
//Definition for a binary tree node.
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
To simplify the problem, assuming that the node to be deleted is a leaf, so we can delete it directly.
I have implemented the search function which returns the reference to the pointer of treenode, so I can change the tree structure directly without the need to track the parent node. But it doesn't work(the node is not deleted).
TreeNode *&searchBST(TreeNode *&root, int val)
{
if (!root)
return root;
if (root->val == val)
return root;
else if (root->val > val)
{
return searchBST(root->left, val);
}
else
{
return searchBST(root->right, val);
}
}
I have also implemented the search function which returns the pointer to pointer of treenode and it works.
TreeNode **searchBST(TreeNode *&root, int val)
{
if (!root)
return &root;
if (root->val == val)
return &root;
else if (root->val > val)
{
return searchBST(root->left, val);
}
else
{
return searchBST(root->right, val);
}
}
The full code:
#include <iostream>
using namespace std;
//Definition for a binary tree node.
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
TreeNode *&searchBST(TreeNode *&root, int val)
{
if (!root)
return root;
if (root->val == val)
return root;
else if (root->val > val)
{
return searchBST(root->left, val);
}
else
{
return searchBST(root->right, val);
}
}
TreeNode *deleteNode(TreeNode *root, int key)
{
TreeNode *node = searchBST(root, key);
if (!node)
return root;
node = NULL;
return root;
}
int main()
{
TreeNode n1(1), n2(0), n3(2);
n1.left = &n2;
n1.right = &n3;
TreeNode *res = deleteNode(&n1, 2);
return 0;
}
The code that works:
#include <iostream>
using namespace std;
//Definition for a binary tree node.
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
TreeNode **searchBST(TreeNode *&root, int val)
{
if (!root)
return &root;
if (root->val == val)
return &root;
else if (root->val > val)
{
return searchBST(root->left, val);
}
else
{
return searchBST(root->right, val);
}
}
TreeNode *deleteNode(TreeNode *root, int key)
{
TreeNode **node = searchBST(root, key);
if (!node)
return root;
*node = NULL;
return root;
}
int main()
{
TreeNode n1(1), n2(0), n3(2);
n1.left = &n2;
n1.right = &n3;
TreeNode *res = deleteNode(&n1, 2);
if (res->right != NULL)
cout << res->right->val << endl;
return 0;
}
How should I define a function that return reference to a pointer?
The way you wrote TreeNode *&searchBST(TreeNode *&root, int val)
is fine. The premise of your question is wrong: What makes the difference between your working version and the non-working one is not the searchBST
but deleteNode
.
Here:
TreeNode *deleteNode(TreeNode *root, int key)
{
TreeNode *node = searchBST(root, key);
if (!node)
return root;
node = NULL;
return root;
}
node
is a local variable and assigning NULL
to it has not effect whatsoever on the actual tree.
On the other hand here:
TreeNode *deleteNode(TreeNode *root, int key)
{
TreeNode **node = searchBST(root, key);
if (!node)
return root;
*node = NULL;
return root;
}
node
is a pointer to the actual pointer you store in the tree. Hence you can dereference it to assign to the pointer in the tree.
how can I free the space of the node I deleted?
You don't !
delete *node doesn't work.
Why do you want to use delete
? You never used new
, hence you should also not use delete
to free the memory. In main:
int main()
{
TreeNode n1(1), n2(0), n3(2);
//...
} // <---
n1
, n2
and n3
use automatic storage and get detroyed when they go out of scope (ie when main
returns, at <---
).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.