-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTree.cpp
More file actions
379 lines (341 loc) · 7.37 KB
/
Tree.cpp
File metadata and controls
379 lines (341 loc) · 7.37 KB
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
#include <iostream>
#include <algorithm>
using namespace std;
struct Node
{
Node *lchild;
int data;
Node *rchild;
};
class Tree
{
public:
Node *root;
Tree()
{
root = NULL;
}
int isEmpty();
void insert(int);
void inOrder(Node *);
void preOrder(Node *);
void postOrder(Node *);
void search(int);
void smallest();
void largest();
void del(int);
int totalNodes(Node *);
int externalNodes(Node *);
int internalNodes(Node *);
int height(Node *);
void deleteTree(Node *);
};
int Tree::isEmpty()
{
if (root == NULL)
return 1;
return 0;
}
Node *createNode(int item)
{
Node *ptr = new Node;
ptr->lchild = NULL;
ptr->data = item;
ptr->rchild = NULL;
return ptr;
}
void Tree::insert(int key)
{
Node *curr = createNode(key);
if (isEmpty())
{
root = curr;
return;
}
Node *temp = root, *parent;
while (temp != NULL)
{
parent = temp;
if (key < temp->data)
temp = temp->lchild;
else
temp = temp->rchild;
}
if (key > parent->data)
parent->rchild = curr;
else
parent->lchild = curr;
}
void Tree::inOrder(Node *r)
{
if (isEmpty())
{
cout << "Tree is Empty" << endl;
return;
}
if (r == NULL)
return;
inOrder(r->lchild);
cout << r->data << " ";
inOrder(r->rchild);
}
void Tree::preOrder(Node *r)
{
if (isEmpty())
{
cout << "Tree is Empty" << endl;
return;
}
if (r == NULL)
return;
cout << r->data << " ";
preOrder(r->lchild);
preOrder(r->rchild);
}
void Tree::postOrder(Node *r)
{
if (isEmpty())
{
cout << "Tree is Empty" << endl;
return;
}
if (r == NULL)
return;
postOrder(r->lchild);
postOrder(r->rchild);
cout << r->data << " ";
}
void Tree::search(int key)
{
if (isEmpty())
{
cout << "Tree is Empty" << endl;
return;
}
Node *temp = root;
while (temp != NULL)
{
if (temp->data == key)
{
cout << "Item Found" << endl;
return;
}
else if (key < temp->data)
temp = temp->lchild;
else
temp = temp->rchild;
}
cout << "Item not Found" << endl;
}
void Tree::smallest()
{
if (isEmpty())
{
cout << "Tree is Empty" << endl;
return;
}
Node *temp = root;
while (temp->lchild != NULL)
temp = temp->lchild;
cout << "Smallest Element -> " << temp->data;
}
void Tree::largest()
{
if (isEmpty())
{
cout << "Tree is Empty" << endl;
return;
}
Node *temp = root;
while (temp->rchild != NULL)
temp = temp->rchild;
cout << "Largest Element -> " << temp->data;
}
int Tree::totalNodes(Node *r)
{
if (r == NULL)
return 0;
else
return totalNodes(r->lchild) + totalNodes(r->rchild) + 1;
}
void Tree::del(int key)
{
Node *temp = root, *parent = NULL;
while (temp != NULL && key != temp->data)
{
parent = temp;
if (key < temp->data)
temp = temp->lchild;
else
temp = temp->rchild;
}
if (temp == NULL)
{
cout << "Item not found" << endl;
return;
}
if (temp->lchild == NULL || temp->rchild == NULL)
{
Node *ptr;
if (temp->lchild == NULL)
ptr = temp->rchild;
else
ptr = temp->lchild;
if (parent == NULL)
{
root = ptr;
return;
}
if (parent->lchild == temp)
parent->lchild = ptr;
else
parent->rchild = ptr;
delete temp;
temp = NULL;
}
// When both of the children node is present of node is to be deleted
else
{
Node *succ = temp->rchild, *psucc = NULL;
while (succ->lchild != NULL)
{
psucc = succ;
succ = succ->lchild;
}
temp->data = succ->data;
if (psucc == NULL)
temp->rchild = succ->rchild;
else
psucc->lchild = succ->rchild;
delete succ;
succ = NULL;
}
cout << "Item deleted successfully" << endl;
}
int Tree::externalNodes(Node *r)
{
if (r == NULL)
return 0;
if (r->lchild == NULL && r->rchild == NULL)
return 1;
else
return externalNodes(r->lchild) + externalNodes(r->rchild);
}
int Tree::internalNodes(Node *r)
{
return totalNodes(r) - externalNodes(r);
}
int Tree::height(Node *r)
{
int LTheight, RTheight;
if (r == NULL)
return 0;
LTheight = height(r->lchild);
RTheight = height(r->rchild);
return max(LTheight, RTheight) + 1;
}
void Tree::deleteTree(Node *r){
if(r == NULL)
return;
deleteTree(r->lchild);
deleteTree(r->rchild);
cout << "\n Deleting Node... " << r->data;
delete r;
root = NULL;
}
int main()
{
Tree t;
int item, choice;
while (true)
{
cout << "\n\n";
cout << "1.INSERT" << endl;
cout << "2.INORDER" << endl;
cout << "3.PREORDER" << endl;
cout << "4.POSTORDER" << endl;
cout << "5.SEARCH" << endl;
cout << "6.SMALLEST ELEMENT" << endl;
cout << "7.LARGEST ELEMENT" << endl;
cout << "8.TOTAL NODES" << endl;
cout << "9.DELETE" << endl;
cout << "10.INTERNAL NODES" << endl;
cout << "11.EXTERNAL NODES" << endl;
cout << "12.HEIGHT" << endl;
cout << "13.DELETE TREE" << endl;
cout << "Enter Choice ->";
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter a item :";
cin >> item;
t.insert(item);
break;
case 2:
t.inOrder(t.root);
break;
case 3:
t.preOrder(t.root);
break;
case 4:
t.postOrder(t.root);
break;
case 5:
cout << "Enter a item to search in BST :";
cin >> item;
t.search(item);
break;
case 6:
t.smallest();
break;
case 7:
t.largest();
break;
case 8:
cout << "Total Nodes in BST ->" << t.totalNodes(t.root);
break;
case 9:
if (!t.isEmpty())
{
cout << "Enter an item to delete :";
cin >> item;
t.del(item);
}
else
{
cout << "Tree is empty" << endl;
}
break;
case 10:
if (!t.isEmpty())
{
cout << "Internal Nodes ->" << t.internalNodes(t.root);
}
else
{
cout << "Tree is empty" << endl;
}
break;
case 11:
if (!t.isEmpty())
{
cout << "External Nodes ->" << t.externalNodes(t.root);
}
else
{
cout << "Tree is empty" << endl;
}
break;
case 12:
cout << "The Height of the BST is " << t.height(t.root);
break;
case 13:
t.deleteTree(t.root);
break;
default:
cout << "Invalid Choice" << endl;
exit(0);
}
}
}