-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBST.cpp
More file actions
341 lines (323 loc) · 6.35 KB
/
Copy pathBST.cpp
File metadata and controls
341 lines (323 loc) · 6.35 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
/*
* File: BST.cpp
* Author: Manish
*
* Created on 21 January, 2019, 9:02 PM
*/
#include<iostream>
using namespace std;
struct node
{
node * left;
string word;
string meaning;
node * right;
node();
~node();
};
node::node()
{
left=NULL;
word.clear();
meaning.clear();
right=NULL;
}
node::~node()
{
left=NULL;
word.clear();
meaning.clear();
right=NULL;
}
class Dictionary
{
private:
node * root;
public:
Dictionary();
~Dictionary();
void create();
void inOrderRec(node *);
void preOrderRec(node *);
void postOrderRec(node *);
void display();
node * search();
void modifyDictionary();
void deleteWord();
void findMaxInLeftSubtreeAndDeleteNode(node *);
};
Dictionary::Dictionary()
{
root=NULL;
}
Dictionary::~Dictionary()
{
root=NULL;
}
void Dictionary::create()
{
node * nn=NULL;
nn=new node;
cout<<"Enter word : ";
cin>>nn->word;
cin.ignore(100,'\n');
cout<<"Enter meaning : ";
getline(std::cin,nn->meaning);
if(root==NULL)
{
root=nn;
}
else
{
node * temp=NULL,* parent=NULL;
temp=root;
while(temp!=NULL)
{
parent=temp;
if(nn->word > temp->word)
temp=temp->right;
else if(nn->word < temp->word)
temp=temp->left;
else
{
cout<<"Duplicate enters not allowed !!!\n";
delete nn;
nn=NULL;
return;
}
}
if(nn->word > parent->word)
parent->right=nn;
else
parent->left=nn;
}
}
void Dictionary::display()
{
cout<<"Displaying data in InOrder traversal\n";
inOrderRec(root);
cout<<endl;
cout<<"Displaying data in PreOrder traversal\n";
preOrderRec(root);
cout<<endl;
cout<<"Displaying data in PostOrder traversal\n";
postOrderRec(root);
cout<<endl;
}
void Dictionary::inOrderRec(node * temp)
{
if(temp!=NULL)
{
inOrderRec(temp->left);
cout<<temp->word<<" : "<<temp->meaning<<endl;
inOrderRec(temp->right);
}
}
void Dictionary::preOrderRec(node * temp)
{
if(temp!=NULL)
{
cout<<temp->word<<" : "<<temp->meaning<<endl;
preOrderRec(temp->left);
preOrderRec(temp->right);
}
}
void Dictionary::postOrderRec(node * temp)
{
if(temp!=NULL)
{
postOrderRec(temp->left);
postOrderRec(temp->right);
cout<<temp->word<<" : "<<temp->meaning<<endl;
}
}
node * Dictionary::search()
{
node * temp=root;
string tempData;
cout<<"Enter word : ";
cin>>tempData;
int flag=0;
while(temp!=NULL)
{
if(tempData==temp->word)
{
flag=1;break;
}
else
{
if(tempData > temp->word)
temp=temp->right;
else
temp=temp->left;
}
}
// if(flag==1)
// return temp;
// else
// {
// temp=NULL;
// return temp;
// }
return temp;
}
void Dictionary::modifyDictionary()
{
node * x=NULL;
x=search();
if(x!=NULL)
{
cin.ignore(100,'\n');
cout<<"Enter meaning : ";
getline(std::cin,x->meaning);
cout<<"Meaning updated\n";
}
else
cout<<"Word not Found!!!\n";
}
void Dictionary::findMaxInLeftSubtreeAndDeleteNode(node * temp)
{
node * t;
node *p;
t=p=temp;
t=t->left;
while(t->right)
{
p=t;
t=t->right;
}
if(p==temp)
{
temp->word=t->word;
temp->meaning=t->meaning;
temp->left=t->left;
delete t;
t=NULL;
}
else
{
temp->word=t->word;
temp->meaning=t->meaning;
p->right=t->left;
delete t;
t=NULL;
}
}
void Dictionary::deleteWord()
{
node * temp=root;
node * parent=NULL;
string tempData;
cout<<"Enter word : ";
cin>>tempData;
int flag=0;
parent=temp;
while(temp!=NULL)
{
if(tempData==temp->word)
{
flag=1;
break;
}
else
{
parent=temp;
if(tempData > temp->word)
temp=temp->right;
else
temp=temp->left;
}
}
if(flag==1)
{
if(temp->left==NULL and temp->right==NULL) //leaf node deletion
{
if(temp==root)
{
delete root;
root=NULL;
}
else
{
if(temp->word > parent->word)
parent->right=NULL;
else
parent->left=NULL;
delete temp;
temp=NULL;
}
}
else if(temp->left==NULL and temp->right!=NULL)
{
if(temp==root)
root=temp->right;
else
{
if(temp->word > parent->word)
parent->right=temp->right;
else
parent->left=temp->right;
}
delete temp;
temp=NULL;
}
else if(temp->left!=NULL and temp->right==NULL)
{
if(temp==root)
root=temp->left;
else
{
if(temp->word > parent->word)
parent->right=temp->left;
else
parent->left=temp->left;
}
delete temp;
temp=NULL;
}
else if(temp->left!=NULL and temp->right!=NULL)
findMaxInLeftSubtreeAndDeleteNode(temp);
}
else
cout<<"Word not found!!!\n";
}
int main()
{
Dictionary aDictionary;
int exit=0;
node * x=NULL;
char choice;
do
{
cout<<"Enter: \n1]To add word to Dictionary\n2]To display\n3]To Search\n4]To modify\n5]To delete Word\n6]To exit\nchoice=";
cin>>choice;
switch(choice)
{
case '1':
aDictionary.create();
break;
case '2':
aDictionary.display();
break;
case '3':
x=NULL;
x=aDictionary.search();
if(x!=NULL)
cout<<"Word Found\n";
else
cout<<"Word not Found\n";
break;
case '4':
aDictionary.modifyDictionary();
break;
case '5':
aDictionary.deleteWord();
break;
case '6':
exit=1;
break;
default:
cout<<"Wrong choice Entered!!!\n";
}
}while(exit==0);
}