-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbst.cpp
More file actions
436 lines (400 loc) · 8.88 KB
/
bst.cpp
File metadata and controls
436 lines (400 loc) · 8.88 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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#include<iostream>
#include<string>
using namespace std;
class node
{
string word;
string meaning;
node *left;
node *right;
friend class binarytree;
};
class binarytree
{
node *root;
public:
void bstrootcreate();
void create_bst(node *root);
void preordernr(node *root);
void preordernr();
void postordernr(node *root);
void postordernr();
void inordernr(node *root);
void inordernr();
node *copytreerec(node *root);
node *copytreerec();
void copytreenonrec(node *root);
void copytreenonrec();
void mirrorimage(node *root);
void mirrorimage();
void levelnr();
bool search(node *root,string w);
void delnode(string w);
};
class queue2
{
int f = -1;
int r = -1;
node *q[20];
public:
void insert(node *temp);
node* remove();
bool isempty();
friend class binarytree;
};
void queue2 :: insert(node *temp)
{
q[++f] = temp;
}
node* queue2:: remove()
{
node *temp = q[++r];
return temp;
}
bool queue2:: isempty()
{
if(f == r)
return true;
else
return false;
}
void binarytree :: bstrootcreate()
{
root = new node();
cout << "Enter Word" << endl;
cin>>root->word;
cout << "Enter Meaning Of The Word" << endl;
cin>>root->meaning;
root->left = root->right = NULL;
create_bst(root);
}
void binarytree :: create_bst(node *root)
{
char ch='Y';
int flag;
while(ch=='Y')
{
node *curr = new node();
node *temp = root;
flag = 0;
cout<<"Enter Word : "<<endl;
cin>>curr->word;
cout << "Enter Meaning Of The Word" << endl;
cin>>curr-> meaning;
curr->left = curr->right = NULL;
while(flag == 0)
{
if(curr->word.compare(temp->word) <= 0) // currword <= temp->word
{
if(temp->left == NULL)
{
temp->left = curr;
flag = 1;
break;
}
else
{
temp = temp->left;
flag = 0;
}
}
else
{
if(temp->right == NULL)
{
temp->right = curr;
flag = 1;
break;
}
else
{
temp = temp->right;
flag = 0;
}
}
}
cout<<"Do you want to insert a new Node? (Y/N)"<<endl;
cin>>ch;
}
}
bool binarytree :: search(node *root, string w)
{
if(root ==NULL)
{
return false;
}
if(root->word.compare(w) == 0)
{
return true;
}
if(root->word.compare(w) < 0)
{
return search(root->right,w);
}
else
{
return search(root->left,w);
}
}
void binarytree :: delnode(string w)
{
bool b = search(root,w);
node *temp = root;
node *parent = NULL;
if(b==false)
{
cout<<"NODE NOT FOUND. DELETE A NODE WHICH IS ACTUALLY PRESENT.";
}
else
{
cout<<"NODE HAS BEEN FOUND.\n";
cout<<w<<" HAS BEEN DELETED.";
if(root->word.compare(w) != 0)
{
temp = root;
while(temp!=NULL)
{
if(temp->word.compare(w) == 0)
break;
else if(temp->word.compare(w) > 0)
{
parent = temp;
temp = temp->left;
}
else
{
parent = temp;
temp = temp->right;
}
}
if(temp->left == NULL && temp->right == NULL)
{
if(parent->right == temp)
{
parent->right = NULL;
}
else
{
parent->left = NULL;
}
delete temp;
}
else if(temp->left == NULL && temp->right != NULL)
{
if(parent->right == temp)
{
parent->right = temp->right;
}
else
{
parent->left = temp->right;
}
delete temp;
}
else if(temp->left != NULL && temp->right == NULL)
{
if(parent->right == temp)
{
parent->right = temp->left;
}
else
{
parent->left = temp->left;
}
delete temp;
}
else
{
node *s = temp->left;
node *r = temp->right;
if(parent->right == temp)
{
parent->right = r;
}
else
{
parent->left = r;
}
while(r->left!=NULL)
{
r = r->left;
}
r->left = s;
temp->right = NULL;
delete temp;
}
}
else
{
if(temp->left == NULL && temp->right == NULL)
{
root= NULL;
delete root;
}
else if(temp->left == NULL && temp->right != NULL)
{
root = temp->right;
temp->right = NULL;
delete temp;
}
else if(temp->left != NULL && temp->right == NULL)
{
root = temp->left;
temp->left = NULL;
delete temp;
}
else
{
node *s = temp->left;
node *r = temp->right;
root = r;
while(r->left!=NULL)
{
r = r->left;
}
r->left = s;
temp->right = NULL;
delete temp;
}
}
}
}
void binarytree :: preordernr(node *root)
{
if(root!=NULL)
{
cout<<"Word:" <<root->word<< "\n";
cout<<"Meaning:" <<root->meaning<< "\n";
preordernr(root->left);
preordernr(root->right);
}
}
void binarytree :: preordernr()
{
preordernr(root);
}
void binarytree :: postordernr(node *root)
{
if(root!=NULL)
{
postordernr(root->left);
postordernr(root->right);
cout<<"Word ->" <<root->word<< "\n";
cout<<"Meaning ->" <<root->meaning<< "\n\n";
}
}
void binarytree :: postordernr()
{
postordernr(root);
}
void binarytree :: inordernr(node *root)
{
if(root!=NULL)
{
inordernr(root->left);
cout<<"Word ->" <<root->word<< "\n";
cout<<"Meaning ->" <<root->meaning<< "\n\n";
inordernr(root->right);
}
}
node* binarytree :: copytreerec(node *root)
{
node *temp = NULL;
if(root !=NULL)
{
temp = new node();
temp->word = root->word;
temp->meaning= root->meaning;
temp->left = copytreerec(root->left);
temp->right = copytreerec(root->right);
}
return temp;
}
node* binarytree :: copytreerec()
{
node *t = copytreerec(root);
return t;
}
void binarytree :: inordernr()
{
inordernr(root);
}
void binarytree :: mirrorimage(node *root)
{
node* t1;
if(root!=NULL)
{
t1 = root->left;
root->left = root->right;
root->right = t1;
mirrorimage(root->left);
mirrorimage(root->right);
}
}
void binarytree :: mirrorimage()
{
mirrorimage(root);
}
void binarytree :: levelnr()
{
queue2 Q;
node* t;
Q.insert(root);
while(!Q.isempty())
{
t = Q.remove();
cout<<"Word " <<t->word<< "\n";
cout<<"Meaning" <<t->meaning<< "\n\n";
if(t->left !=NULL)
{
Q.insert(t->left);
}
if(t->right!=NULL)
{
Q.insert(t->right);
}
}
}
int main()
{
int ch=1;
binarytree b1;
node *n1;
string w;
while(ch<9 && ch>0)
{
cout<<"Binary Search Tree Operations.\n1 = Create.\n2 = Inorder.\n3 = Preorder.\n4 = Postorder.\n5 = LevelWise.\n6 = Copy Recursively.\n7 = Mirror Image. \n8 = Delete Node.\nPress Any Other Number To Exit."<<endl;
cin>>ch;
cout<<"\n\n";
switch(ch)
{
case 1 : b1.bstrootcreate();
break;
case 2 : cout<<"Inorder Form Is :"<<endl;
b1.inordernr();
break;
case 3 : cout<<"Preorder Form Is :"<<endl;
b1.preordernr();
break;
case 4 : cout<<"Postorder Form Is :"<<endl;
b1.postordernr();
break;
case 5: cout<<"LevelWise Form Is :"<<endl;
b1.levelnr();
break;
case 6: cout<<"Tree Copied Recursively."<<endl;
n1 = b1.copytreerec();
b1.inordernr(n1);
break;
case 7: cout<<"Mirror Image Created. Call a traversal Function to check."<<endl;
b1.mirrorimage();
break;
case 8: cout<<"Enter Word to Delete."<<endl;
cin>>w;
b1.delnode(w);
break;
default : cout<<"Program Exitted."<<endl;
break;
}
}
}