Tree Delete

This code is inserting numbers into tree with templates.But i couldn't succeed to delete them with templates.Does anyone know how to delete?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
template <class Data> class MyNode
{
      Data* pData;
      MyNode* pLeft;
      MyNode* pRight;

public:
      MyNode<Data>(Data* p): pData(p) {}
      bool IsLE(Data* p) 
      {
            return (*pData) < (*p);
      }
};

template <class Data>
class MyTree
{
      MyNode<Data>* pRoot;

      void Insert(MyNode<Data>*& pRoot, Data* pNew)
      {
            if (!pRoot)
            {
                  pRoot = new MyNode<Data>(pNew);
                  return;
            }

            if (pRoot->IsLE(pNew))
                  Insert(pRoot->pRight, pNew);
            else
                  Insert(pRoot->pLeft, pNew);
      }

public:
      void Insert(Data* pNew)
      {
            Insert(pRoot, pNew);
      }


      Delete(Data*){}
      Data* Find(Data*){}
};
   
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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??
How can i write Find(Data*); ??
start at the root, end at a leaf.
If a node in your way has got
what you are looking for
return it, you are done.

Which way do I need to take, you may ask
It is a binary tree, so it only could be left or right.
Drop a coin or think. It is lesser than what I need?
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??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void Delete(MyNode<Data>*& proot){
           if(proot->pleft) 
			   Delete(proot->pleft);
           if(proot->pright) 
			   Delete(proot->pright);
           delete proot->pData;
           proot = 0;
		}
		   MyNode<Data>** Find(Data*)
			   {
				   if(!s->pLeft)
					   return s->pRight;
				   else if(!s->pRight)
					   return s->pLeft;
		   }
				   

         void Delete(Data* dat){
                Mynode<Data>** node = Find(dat);
                if(node) 
					Delete(*node);
}
Your Find doesn't make sense.

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.
Topic archived. No new replies allowed.