forked from Adel-Nasim/Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary Search Tree.txt
369 lines (307 loc) · 6.89 KB
/
Binary Search Tree.txt
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
#include <iostream>
#include <cassert>
using namespace std;
struct nodeType
{
int info;
nodeType *left;
nodeType *right;
};
class binarySearchTreeType
{
public:
bool isEmpty();
bool search(int);
bool searchRec(int);
void insert(int);
void remove(int);
void inorderTraversal();
void preorderTraversal();
void postorderTraversal();
int treeHeight();
int treeNodeCount();
int treeLeavesCount();
void clearTree();
binarySearchTreeType();
~binarySearchTreeType();
private:
nodeType *root;
void clear(nodeType* &p);
void inorder(nodeType *p);
void preorder(nodeType *p);
void postorder(nodeType *p);
int height(nodeType *p);
int max(int x, int y);
int nodeCount(nodeType *p);
int leavesCount(nodeType *p);
void deleteFromTree(nodeType * &p);
bool searchRecPriv(nodeType *, int);
};
binarySearchTreeType::binarySearchTreeType()
{
root = NULL;
}
bool binarySearchTreeType::isEmpty()
{
return (root == NULL);
}
void binarySearchTreeType::inorderTraversal()
{
inorder(root);
}
void binarySearchTreeType::preorderTraversal()
{
preorder(root);
}
void binarySearchTreeType::postorderTraversal()
{
postorder(root);
}
int binarySearchTreeType::treeHeight()
{
return height(root);
}
int binarySearchTreeType::treeNodeCount()
{
return nodeCount(root);
}
int binarySearchTreeType::treeLeavesCount()
{
return leavesCount(root);
}
void binarySearchTreeType::inorder(nodeType *p)
{
if(p != NULL)
{
inorder(p->left);
cout<<p->info<<" ";
inorder(p->right);
}
}
void binarySearchTreeType::preorder(nodeType *p)
{
if(p != NULL)
{
cout<<p->info<<" ";
preorder(p->left);
preorder(p->right);
}
}
void binarySearchTreeType::postorder(nodeType *p)
{
if(p != NULL)
{
postorder(p->left);
postorder(p->right);
cout<<p->info<<" ";
}
}
void binarySearchTreeType::clear(nodeType* &p)
{
if(p != NULL)
{
clear(p->left);
clear(p->right);
delete p;
p = NULL;
}
}
void binarySearchTreeType::clearTree()
{
clear(root);
}
binarySearchTreeType::~binarySearchTreeType()
{
clear(root);
}
int binarySearchTreeType::height(nodeType *p)
{
if(p == NULL)
return 0;
else
return 1 + max(height(p->left), height(p->right));
}
int binarySearchTreeType::max(int x, int y)
{
if(x >= y)
return x;
else
return y;
}
int binarySearchTreeType::nodeCount(nodeType *p)
{
if(p == NULL)
return 0;
else
return 1 + nodeCount(p->left) + nodeCount(p->right);
}
int binarySearchTreeType::leavesCount(nodeType *p)
{
if(p == NULL)
return 0;
else if ((p->left == NULL) && (p->right == NULL))
return 1;
else
return leavesCount(p->left) + leavesCount(p->right);
}
bool binarySearchTreeType::search(int item)
{
nodeType *current = root;
while(current != NULL)
{
if(current->info == item)
return true;
else if(current->info > item)
current = current->left;
else
current = current->right;
}
return false;
}
bool binarySearchTreeType::searchRec(int item)
{
return searchRecPriv(root, item);
}
bool binarySearchTreeType::searchRecPriv(nodeType *p, int item)
{
if(p == NULL)
return false;
else if (p->info == item)
return true;
else if(p->info > item)
return searchRecPriv(p->left, item);
else
return searchRecPriv(p->right, item);
}
void binarySearchTreeType::insert(int item)
{
nodeType *current; //pointer to traverse the tree
nodeType *trailCurrent; //pointer behind current
nodeType *newNode; //pointer to create the node
newNode = new nodeType;
assert(newNode != NULL);
newNode->info = item;
newNode->left = NULL;
newNode->right = NULL;
if(root == NULL)
root = newNode;
else
{
current = root;
while(current != NULL)
{
trailCurrent = current;
if(current->info == item)
{
cout<<"The insert item is already in the list -- ";
cout<<"duplicates are not allowed."<<endl;
return;
}
else
if(current->info > item)
current = current->left;
else
current = current->right;
}//end while
if(trailCurrent->info > item)
trailCurrent->left = newNode;
else
trailCurrent->right = newNode;
}
}
//this function only determines the node to be deleted
void binarySearchTreeType::remove(int item)
{
nodeType *current; //pointer to traverse the tree
nodeType *trailCurrent; //pointer behind current
if(root == NULL)
{
cout<<"Cannot delete from the empty tree."<<endl;
return;
}
if(root->info == item)
{
deleteFromTree(root);
return;
}
//if you get here, then the item to be deleted is not the root
trailCurrent = root;
if(root->info > item)
current = root->left;
else
current = root->right;
//search for the item to be deleted.
while(current != NULL)
{
if(current->info == item)
break;
else
{
trailCurrent = current;
if(current->info > item)
current = current->left;
else
current = current->right;
}
}// once the while is done, current points to either NULL or to the node to be deleted
if(current == NULL)
cout<<"The delete item is not in the tree."<<endl;
else if(trailCurrent->info > item)
deleteFromTree(trailCurrent->left);
else
deleteFromTree(trailCurrent->right);
}
void binarySearchTreeType::deleteFromTree
(nodeType* &p)
{
nodeType *current; //pointer to traverse
//the tree
nodeType *trailCurrent; //pointer behind current
nodeType *temp; //pointer to delete the node
if(p->left == NULL && p->right == NULL)
{
delete p;
p = NULL;
}
else if(p->left == NULL)
{
temp = p;
p = p->right;
delete temp;
}
else if(p->right == NULL)
{
temp = p;
p = p->left;
delete temp;
}
else
{
current = p->left;
trailCurrent = NULL;
while(current->right != NULL)
{
trailCurrent = current;
current = current->right;
}//end while
p->info = current->info;
if(trailCurrent == NULL) //current did not move;
//current == p->left; adjust p
p->left = current->left;
else
trailCurrent->right = current->left;
delete current;
}//end else
}//end deleteFromTree
int main()
{
binarySearchTreeType b;
b.insert(10);
b.insert(20);
b.insert(5);
b.remove(10);
b.inorderTraversal();
b.postorderTraversal();
b.preorderTraversal();
return 0;
}