-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathB11.cpp
380 lines (302 loc) · 8.64 KB
/
B11.cpp
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
#include <iostream>
using namespace std;
class node {
public:
string word;
string meaning;
node* left = NULL;
node* right = NULL;
node (string x, string y) {
word = x;
meaning = y;
left = NULL;
right = NULL;
}
friend class Dictionary;
};
class Dictionary {
public:
node* root, *q; //q is parent here
Dictionary () {
root = NULL;
q = NULL;
}
void insert (node*, string, string);
void display_asc (node *);
void display_desc (node *);
void comparisons (node*, string);
void updateWord (node*, string);
void deleteWord (node*, string);
node* min_node (node *);
};
void Dictionary::insert (node* p, string key, string keyMeaning) {
if (key < p -> word) {
if (p -> left != NULL)
insert (p -> left, key, keyMeaning);
else
p -> left = new node (key, keyMeaning);
}
else if (key > p -> word) {
if (p -> right != NULL)
insert (p -> right, key, keyMeaning);
else
p -> right = new node (key, keyMeaning);
}
}
void Dictionary::display_asc (node *p){ //inorder
if (p -> left != NULL)
display_asc (p -> left);
cout << "\n" << p -> word << " \t" << p -> meaning;
if (p -> right != NULL)
display_asc (p -> right);
}
void Dictionary::display_desc (node *p) {
if (p -> right != NULL)
display_desc (p -> right);
cout << "\n" << p -> word << " \t" << p -> meaning;
if (p -> left != NULL)
display_desc (p -> left);
}
void Dictionary::comparisons (node* p, string key) {
static int count = 0;
while (p != NULL) {
if (key < p -> word) {
count++;
p = p -> left;
}
else if (key > p -> word) {
count++;
p = p -> right;
}
else if (key == p -> word) {
count++;
cout << "Number of comparisons to find the word: " << count;
return ;
}
}
cout << "\nWord not found!";
}
void Dictionary::deleteWord (node* p, string key) {
node *s;
while (p != NULL) { //searching for word
if (key < p -> word) {
q = p;
p = p -> left;
}
else if (key > p -> word) {
q = p;
p = p -> right;
}
else if (key == p -> word) { //word found
if (p -> left == NULL && p -> right == NULL) { //no child
if (q -> left == p) {
delete p;
q -> left = NULL;
return;
}
if (q -> right == p) {
delete p;
q -> right = NULL;
return;
}
}
if (p -> right != NULL && p -> left == NULL) { //right child only
if (q -> right == p) {
q -> right = p -> right;
delete p;
return;
}
else if (q -> left == p) {
q -> left = p -> right;
delete p;
return;
}
}
else if (p -> left != NULL && p -> right == NULL) { //left child only
if (q -> right == p) {
q -> right = p -> left;
delete p;
return;
}
else if (q -> left == p) {
q -> left = p -> left;
delete p;
return;
}
}
else if (p -> left != NULL && p -> right != NULL) {
s = min_node (p -> right);
p -> word = s -> word;
p -> meaning = s -> meaning;
deleteWord (s, s -> word);
return;
}
}
}
cout << "\nWord NOT found!";
}
void Dictionary::updateWord (node* p, string key) {
while (p != NULL) {
if (key < p -> word)
p = p -> left;
else if (key > p -> word)
p = p -> right;
else if (key == p -> word) {
cout << "\nEnter its new meaning: ";
cin >> p -> meaning;
return;
}
}
cout << "\nWord not found!";
}
node* Dictionary::min_node (node *p) {
while (p -> left != NULL) {
q = p;
p = p -> left;
}
return p;
}
int main () {
int choice, n;
string newWord, searchWord, newMeaning;
Dictionary d1;
do {
cout << "\n\nDICTIONARY: "
<< "\n\n1. Insert new words"
<< "\n2. Display the dictionary in ascending order"
<< "\n3. Display the dictionary in descending order"
<< "\n4. Search and update a word"
<< "\n5. Delete a word"
<< "\n6. Comparisons"
<< "\n\nEnter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "\nEnter the number of words to insert: ";
cin >> n;
for (int i = 0 ; i < n ; i++) {
cout << "\nEnter the word to be inserted: ";
cin >> newWord;
cout << "\nEnter its meaning: ";
cin >> newMeaning;
if (d1.root == NULL)
d1. root = new node (newWord, newMeaning);
else
d1.insert (d1.root, newWord, newMeaning);
}
break;
case 2:
d1.display_asc (d1.root);
break;
case 3:
d1.display_desc (d1.root);
break;
case 4:
cout << "\nEnter the word to search: ";
cin >> searchWord;
d1.updateWord (d1.root, searchWord);
break;
case 5:
cout << "\nEnter the word to delete: ";
cin >> searchWord;
d1.deleteWord (d1.root, searchWord);
break;
case 6:
cout << "\nEnter the word to find comparisons: ";
cin >> searchWord;
d1.comparisons (d1.root, searchWord);
}
} while (choice < 7);
return 0;
}
/*
DICTIONARY:
1. Insert new words
2. Display the dictionary in ascending order
3. Display the dictionary in descending order
4. Search and update a word
5. Delete a word
6. Comparisons
Enter your choice: 1
Enter the number of words to insert: 2
Enter the word to be inserted: name
Enter its meaning: Pratik
Enter the word to be inserted: roll
Enter its meaning: 56
DICTIONARY:
1. Insert new words
2. Display the dictionary in ascending order
3. Display the dictionary in descending order
4. Search and update a word
5. Delete a word
6. Comparisons
Enter your choice: 2
name Pratik
roll 56
DICTIONARY:
1. Insert new words
2. Display the dictionary in ascending order
3. Display the dictionary in descending order
4. Search and update a word
5. Delete a word
6. Comparisons
Enter your choice: 3
roll 56
name Pratik
DICTIONARY:
1. Insert new words
2. Display the dictionary in ascending order
3. Display the dictionary in descending order
4. Search and update a word
5. Delete a word
6. Comparisons
Enter your choice: 4
Enter the word to search: name
Enter its new meaning: Pingale
DICTIONARY:
1. Insert new words
2. Display the dictionary in ascending order
3. Display the dictionary in descending order
4. Search and update a word
5. Delete a word
6. Comparisons
Enter your choice: 2
name Pingale
roll 56
DICTIONARY:
1. Insert new words
2. Display the dictionary in ascending order
3. Display the dictionary in descending order
4. Search and update a word
5. Delete a word
6. Comparisons
Enter your choice: 5
Enter the word to delete: roll
DICTIONARY:
1. Insert new words
2. Display the dictionary in ascending order
3. Display the dictionary in descending order
4. Search and update a word
5. Delete a word
6. Comparisons
Enter your choice: 2
name Pingale
DICTIONARY:
1. Insert new words
2. Display the dictionary in ascending order
3. Display the dictionary in descending order
4. Search and update a word
5. Delete a word
6. Comparisons
Enter your choice: 6
Enter the word to find comparisons: name
Number of comparisons to find the word: 1
DICTIONARY:
1. Insert new words
2. Display the dictionary in ascending order
3. Display the dictionary in descending order
4. Search and update a word
5. Delete a word
6. Comparisons
Enter your choice: 7
*/