Clear() function in Binary Tree

Aug 12, 2008 at 1:48pm
Hi everybody, help me to finish my programm.
It's all works, except one function.
This function is clear();
This function must fully clear binary tree.
And the constructor Map(char * ch, double val)
doesn't works. I can't understand.

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
//Binary Search Tree Program

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

class Map
{
	private:
        struct tree_node
        {
           tree_node* left;
           tree_node* right;
           double data;
		   char * key;
        };
        tree_node* root;
		    
    public:
        Map()
        {
           root = NULL;
		}
      
		/*Map(char * key, double val)
		{
			root->key = key;
			root->data = val;
			root->left = NULL;
			root->right = NULL;
		}*/

        
		bool isEmpty() const { return root==NULL; }
        
		void print_inorder();
            void inorder(tree_node*);
            void print_preorder();
            void preorder(tree_node*);
            void print_postorder();
            void postorder(tree_node*);
		void find(char * ch);
		int print_count();
		int count(tree_node * t);
		//void clear_o();
		//void clear_c(tree_node*p);
		void print_s(tree_node ** p);
		void insert(char * ch, double d);
            void remove(char * dkey);
};


// Smaller elements go left
// larger elements go right
void Map::find(char * ch)
{
   //Find code
}


void Map::insert(char * ch, double val)
{
   struct tree_node *curr,*parent;
   if(root == NULL)
   {
      root = (struct tree_node *) malloc(sizeof(struct tree_node)); /* insert the new node as root node*/
      
	  if(root == NULL)
      {
	      printf("Cannot allocate\n");
		  exit(0);
      }
      
	  root->key = ch;
	  root->data = val;
      root->left=root->right=NULL;
   }
   else
   {
      curr = root;
      /* traverse the tree to get a pointer to that node whose child will be the newly created node*/
      while(curr != NULL)
	  {
		 parent = curr;
		 if(strcmp(curr ->key, ch)<0)
			curr = curr->left;

		 else
			curr = curr->right;
	  }

	  if(strcmp(parent->key,ch)<0)
	  {
		  parent->left = (struct tree_node*)malloc(sizeof(struct tree_node));/*inserts the newly created node as left child*/
          parent = parent->left;
          
		  if(parent == NULL)
          {
			 printf("Cannot allocate\n");
			 exit(0);
          }
			 parent->key = ch;	
			 parent->data = val;
			 parent->left=parent->right = NULL;
	  }
	  else
	  {
		  parent->right = (struct tree_node*)malloc(sizeof(struct tree_node));/*inserts the newly created node
			as left child*/
		  parent = parent->right;
		  if(parent == NULL)
          {
			  printf("Cannot allocate\n");
			  exit(0);
          }
		  
		  parent->key = ch;
		  parent->data = val;
		  parent->left=parent->right = NULL;
	  }
   }
}


void Map::remove(char * dkey)
{
    //Locate the element
    bool found = false;
    if(isEmpty())
    {
        cout<<" This Tree is empty! "<<endl;
        return;
    }
    
	tree_node * temp;
    tree_node* curr;
    tree_node* parent;
    curr = root;
    
    while(curr)
    {
         if(strcmp(curr->key,dkey)==0)
         {
			found = true;
            break;
         }
         else
         {
             parent = curr;
             if(strcmp(curr->key, dkey)<0)
				 curr = curr->left;
             else 
				 curr = curr->right;
         }
    }
    if(!found)
	{
        cout<<" Data not found! "<<endl;
        return;
    }

		 // 3 cases :
    // 1. We're removing a leaf node
    // 2. We're removing a node with a single child
    // 3. we're removing a node with 2 children


	
    
	/* this code is for deleting node having both children */
  if( curr->left != NULL && curr->right != NULL)
  {

	/* this code is for deleting root node with both children*/
    if( curr == root)
    {
        temp = curr->left;
        parent = curr->right;
        root = temp;
			
		while(temp->right != NULL)
			temp = temp->right;
		temp->right=parent;
		delete curr;
		root=NULL;
		return;
    }


       if(parent->left == curr)
       {
			temp = curr->left;
			parent->left = curr->left;
			
			while(temp->right != NULL)
				temp = temp->right;
			
			temp->right=curr->right;
			curr->left=NULL;
			curr->right=NULL;
       }
       else
       {
			temp = curr->right;
			parent->right = curr->right;
			
			while(temp->left != NULL)
				temp = temp->left;
			
			temp->left=curr->left;
			curr->left=NULL;
			curr->right=NULL;
       }

	   delete curr;
	   return;
  }


	/* this code is for deleting a node with one child*/
    if(curr->left == NULL && curr->right != NULL)
    {
		if( curr == root)/* this code is for deleting root node with right child*/
		{
			temp = curr->left;
			parent = curr->right;
			root = temp;
			
			while(temp->right != NULL)
				temp = temp->right;
			
			temp->right=parent;
			root=NULL;
			delete curr;
			return;
		}

		if(parent->left == curr)
			parent->left = curr->right;
        else
            parent->right = curr->right;
            curr->right = NULL;
            delete curr;
			return;
    }

	if(curr->left != NULL && curr->right == NULL)
    {
		if( curr == root)/* this code is for deleting root node with left child*/
		{
			temp = curr->left;
			parent = curr->right;
			root = temp;
			
			while(temp->right != NULL)
				temp = temp->right;
			
			temp->right=parent;
			delete curr;
			root=NULL;
			return;
		}

        if(parent->left == curr)
			parent->left = curr->left;
        else
            parent->right = curr->left;
            curr->left = NULL;
            return;
    }
	
    
	//We're looking at a leaf node
	if( curr->left == NULL && curr->right == NULL)
    {
		if(curr == root)/* this code is for deleting root node with no children*/
		{
			delete curr;
			root = NULL;
			return;
		}

        if(parent->left == curr)
			parent->left = NULL;
        else 
			parent->right = NULL;
		delete curr;
		return;
    }

 }
int Map::print_count(){ //count code }
int Map::count(tree_node * t){//count code}
void Map::print_preorder(){//print_preorder code}
void Map::preorder(tree_node* p){//print_preorder code}
void Map::print_postorder(){//print_postorder}
void Map::postorder(tree_node* p){//print_postorder}
void Map::print_inorder(){//print_inorder}
void Map::inorder(tree_node* p){print_inorder}


int main()
{
    Map salary;
	string temp, ftarget, dtarget;
	int ch,tmp,tmp1,size,c;
	double data;
    while(1)
    {
       cout<<endl<<endl;
       cout<<" Binary Search Tree Operations "<<endl;
       cout<<" ----------------------------- "<<endl;
       cout<<" 1. Insertion/Creation "<<endl;
       cout<<" 2. Print Inorder "<<endl;
	   cout<<" 3. Print Preorder "<<endl;
	   cout<<" 4. Print Postorder "<<endl;
	   cout<<" 5. Find "<<endl;
       cout<<" 6. Removal "<<endl;
	   cout<<" 7. Count of Elements "<<endl;
	   cout<<" 8. Clear "<<endl;
	    
       //cout<<" 6. Exit "<<endl;
       cout<<" Enter your choice : ";
		       cin>>ch;
	   char * pch;
       switch(ch)
       {
           case 1 : cout<<" Enter key: ";
                    cin>>temp;
	                pch = (char *) malloc(sizeof(char)*temp.size()); 			
					strcpy(pch, temp.c_str());
					cout << " Enter data: ";
					cin >> data;
					salary.insert(pch,data); break;
           
		   case 2 : cout<<endl;
				    cout<<" In-Order Traversal "<<endl;
                    cout<<" -------------------"<<endl;
                    salary.print_inorder(); break;
				
           case 3 : cout<<endl;
                    cout<<" Pre-Order Traversal "<<endl;
                    cout<<" -------------------"<<endl;
                    salary.print_preorder(); break;

           case 4 : //Post-Order Traversal 
                    break;

	     case 5:  if(salary.isEmpty())
			      cout<<"\n This Tree is empty! "<<endl;
			  else
			  {
    				cout << "Enter key: ";
				cin >> temp;
				pch = (char *) malloc(sizeof(char)*temp.size()); 			                  strcpy(pch, temp.c_str());
				salary.find(pch);
		        }  break;
			
           case 6 : if(salary.isEmpty())
			     cout<<"\n This Tree is empty! "<<endl;
			  else
			  {   cout<<" Enter key to be deleted : ";
				cin >> temp;
				pch = (char *) malloc(sizeof(char)*temp.size()); 					strcpy(pch, temp.c_str());
				salary.remove(pch);
			  }
		        break;

	      case 7:  if(salary.isEmpty())
				cout<<"\n This Tree is empty! "<<endl;
			   else
			   {
			      c = salary.print_count();
				cout << "\nThere are " << c << " elements. \n";
			   }
			   break;

/*		case 8:  salary.clear_o();
			   break;*/

	   		                              
       }
    }
}
Last edited on Aug 12, 2008 at 2:07pm
Aug 12, 2008 at 2:00pm
The clear function is very simple if you implement it in the nodes:
1
2
3
4
5
6
7
8
~tree_node(){
	if (this->left)
		delete this->left;
	if (this->right)
		delete this->right;
	if (this->key)
		delete this->key;
}

Then to clear the tree just delete the root and you're done.
The constructor doesn't work because you're trying to initialize the members of a null pointer.
Last edited on Aug 12, 2008 at 2:00pm
Aug 12, 2008 at 3:53pm
The constructor doesn't work because you're trying to initialize the members of a null pointer.


How can i do this?
Aug 12, 2008 at 10:58pm
Call insert() from inside the constructor with the same parameters. You may need to change the order of declaration.
Last edited on Aug 12, 2008 at 10:59pm
Topic archived. No new replies allowed.