Binary Search Tree - RemoveNodeFunction.

I have 1 problem when I want to delete a node in tree.I think problem in 3 function
1/RemoveRoot().
2/RemoveMatch(Node *parent, Node *match, bool left);
3/Node *ReturnNodePrivate(int val, Node *tmp)
Here is the code:

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
  #include<iostream>

using namespace std;

class Tree
{
private:
	struct Node
	{
		int c;
		Node *left;
		Node *right;
	};
	Node *top;
	void AddLeafPrivate(int val, Node *tmp)
	{
		if (top == NULL)
		{
			top = CreateLeaf(val);
		}
		else if (val < tmp->c)
		{
			if (tmp->left == NULL)
			{
				tmp->left = CreateLeaf(val);
			}
			else
			{
				AddLeafPrivate(val, tmp->left);
			}
		}
		else if (val > tmp->c)
		{
			if (tmp->right == NULL)
			{
				tmp->right = CreateLeaf(val);
			}
			else
			{
				AddLeafPrivate(val, tmp->right);
			}
		}
		else
		{
			cout << "This value " << val << " has already added in tree !!!" << endl;
		}
	}

	void infixPrivate(Node *tmp)
	{
		if (tmp != NULL)
		{
			if (tmp->left != NULL)
			{
				infixPrivate(tmp->left);
			}
			cout << tmp->c << " ";
			if (tmp->right != NULL)
			{
				infixPrivate(tmp->right);
			}

		}
	}

	Node *ReturnNodePrivate(int val, Node *tmp)
	{
		if (tmp != NULL)
		{
			if (tmp->c == val)
			{
				return tmp;
			}
			else if (val < tmp->c)
			{
				return ReturnNodePrivate(val, tmp->left);
			}
			else if (val > tmp->c)
			{
				return ReturnNodePrivate(val, tmp->right);
			}
		}
		else
		{
			return NULL;
		}
	}

	int FindTheSmallestPrivate(Node *tmp)
	{
		if (top == NULL)
		{
			cout << "The Tree is empty !!!" << endl;
		}
		else
		{
			if (tmp->left == NULL)
			{
				return tmp->c; 
			}
			else
			{
				FindTheSmallestPrivate(tmp->left);
			}
		}
	}

	void RemoveNodePrivate(int key, Node *tmp)
	{
		if (top != NULL)
		{
			if (top->c == key)
			{
				RemoveRoot();
			}
			else if (key < tmp->c && tmp->left != NULL)
			{
				tmp->left->c == key ?
					RemoveMatch(tmp,tmp->left,true) :
					RemoveNodePrivate(key, tmp->left);
			}
			else if (key > tmp->c && tmp->right != NULL)
			{
				tmp->right->c == key ?
					RemoveMatch(tmp,tmp->right,false) :
					RemoveNodePrivate(key, tmp->right);
			}
			else
			{
				cout << "The node has value" << key << "tha	t isn't in the tree !!" << endl;
			}
		}
		else
		{
			cout << "The Tree is empty !!" << endl;
		}
	}
public:
	
	Tree()
	{
		top = NULL;
	}

	Node *CreateLeaf(int val)
	{
		Node *n = new Node;
		n->c = val;
		n->left = NULL;
		n->right = NULL;
		return n;
	}

	void AddLeaf(int val)
	{
		AddLeafPrivate(val, top);
	}

	void infix()
	{
		infixPrivate(top);
	}

	Node *ReturnNode(int val)
	{
		return ReturnNodePrivate(val, top);
	}

	void PrintChildren(int val)
	{
		Node *tmp = ReturnNode(val);
		if (tmp == NULL)
		{
			cout << "This Value " << val << "don't in the tree !!!" << endl;
		}
		else
		{
			cout << "Parent Node = " << tmp->c << endl;
			tmp->left == NULL ?
				cout << "Left Child = NULL " << endl :
				cout << "Left Child = " << tmp->left->c << endl;

			tmp->right == NULL ?
				cout << "Right Child = NULL " << endl :
				cout << "Right Child = " << tmp->right->c << endl;
		}
	}

	int FindTheSmallest()
	{
		return FindTheSmallestPrivate(top);
	}

	void RemoveNode(int key)
	{
		RemoveNodePrivate(key, top);
	}

	void RemoveRoot()
	{
		if (top != NULL)
		{
			Node *delPointer = top;
			int topkey = top->c;
			int smallestRight;
			//Case 1: 0 Children
			if (top->left == NULL && top->right == NULL)
			{
				top = NULL;
				delete delPointer;
			}
			//Case 2: 1 Children
			else if (top->left != NULL && top->right == NULL)
			{
				top = top->left;
				delPointer->left = NULL;
				delete delPointer;
				cout << "The Root Node has " << topkey << ",was deleted" << endl;
				cout << "The New Root contains the value " << top->c << endl;
			}
			else if (top->left == NULL && top->right != NULL)
			{
				top = top->right;
				delPointer->right = NULL;
				delete delPointer;
				cout << "The Root Node has " << topkey << ",was deleted" << endl;
				cout << "The New Root contains the value " << top->c << endl;
			}
			//Case 3 : 2 Children
			else
			{
				smallestRight = FindTheSmallestPrivate(top->right);
				RemoveNodePrivate(smallestRight, top);
				top->c = smallestRight;
				cout << "The Root Containing key " << topkey <<
					" was overwritten with key " << top->c << endl;
			}	
		}
		else
		{
			cout << "The Tree is empty !!" << endl;
		}
	}

	//Muon Delete Node thi phai tao mot pointer delete = Node muon dele
	void RemoveMatch(Node *parent, Node *match, bool left)
	{
		if (top != NULL)
		{
			Node *delPointer;
			int Matchkey = match->c;
			int smallestRight1;
			//Case 1 : 0 Children
			if (match->left == NULL && match->right == NULL)
			{
				delPointer = match;
				left == true ?
					parent->left == NULL :
					parent->right == NULL;	
				delete delPointer;
			}

			//Case 2: 1 Child
			else if (match->left != NULL && match->right == NULL)
			{
				left == true ?
					parent->left = match->left :
					parent->right = match->left;
				match->left = NULL;
				delPointer = match;
				delete delPointer;
			}
			else if (match->left == NULL && match->right != NULL)
			{
				left == true ?
					parent->left = match->right :
					parent->right = match->right;
				match->right = NULL;
				delPointer = match;
				delete delPointer;
			}
			//Case 3: 2 Children
			else
			{
				smallestRight1 = FindTheSmallestPrivate(match->right);
				RemoveNodePrivate(smallestRight1, match);
				match->c = smallestRight1;
			}
		}
		else
		{
			cout << "Can't remove. The Tree is empty." << endl;
		}
	}
};


void main()
{
	int input = 0;
	Tree root;
	int a[15] = { 50, 76, 21, 32, 64, 15, 52, 14, 100, 83, 2, 3, 70, 87, 82 };
	for (int i = 0; i < 15; i++)
	{
		root.AddLeaf(a[i]);
	}
	
	cout << "Print Inorder form:\n";
	root.infix();
	cout << endl;
	for (int i = 0; i < 2; i++)
	{
		root.PrintChildren(a[i]);
		cout << endl;
	}
	cout << "The Smallest Number = " << root.FindTheSmallest() << endl;
	cout << "Enter value which you want to delete.Enter -1 to end the process." << endl;
	while (input != -1)
	{
		cout << "Delete Node: "; cin >> input;
		if (input != -1)
		{
			root.RemoveNode(input);
			root.infix();
			cout << endl;
		}
	}
}


P/s : You can copy this code and run it to see what is the problem.
P/s : You can copy this code and run it to see what is the problem.


Or you could just tell us.
Alright , Example :
I have the tree in Inorder form :
2 3 14 15 21 32 50 52 64 70 76 82 83 87 100

I want to delete node contain value : 21 but it just print.

2 3 14 15 32 .

and BinaryTreeTraversal.exe has stopped working.
Line 258 and 259 should probably be assigning (=) null, not comparing (==)
Thanks ,bro ,i just have solved it :D
Topic archived. No new replies allowed.