void Delete(MyNode<Data>*& root){
if(root->left) Delete(root->left);
if(root->right) Delete(root->right);
delete root->pData;
root = 0;
}
MyNode<Data>** Find(Data*);//I'll leave for you to implement this
//note the MyNode<>** instead of *&, so you can return 0 if nothing is found.
void Delete(Data* dat){
Mynode<Data>** node = Find(dat);
if(node) Delete(*node);
}
the problem with this:
if you have several pointers to a node, and Delete(), only one is set to 0. you shouldn't have several pointers to one node though. just watch out for local pointers. if you must, try to use Node*& instead.
Thanks but i actually don't anything about what this Find function does.My aim is to ask users to write some integers and i will insert them in the tree then i will delete what they want.How can i do that??
Is this the complete version of finding and deleting the tree?I spent so many hours but i couldn't print it to the console.I'm not sure if it is deleting nodes with two chıldren??
Find(Data* d) is a recursive function. (It doesn't have to be, but I thing this will be easier to understand)
Firstly, compare *root->pData with *d, then if they are equal, return root. Otherwise, since
*root->left->pData < *root->pData < *root->right->pData.
If *d is <, Find *d in root->left. Else if *d is >, Find *d in root->right.