-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10.txt
269 lines (241 loc) · 4.37 KB
/
10.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
// A Dictionary stores keywords & its meanings. Provide facility for adding new keywords,
deleting keywords, updating values of any entry. Provide facility to display whole data sorted
in ascending/ Descending order. Also find how many maximum comparisons may require for
finding any keyword. Use Height balance tree and find the complexity for finding a keyword
#include<iostream>
#include<string.h>
using namespace std;
typedef struct node
{
char k[20];
char m[20];
class node *left;
class node * right;
}node;
class dict
{
public:
node *root;
void create();
void disp(node *);
void insert(node * root,node *temp);
int search(node *,char []);
int update(node *,char []);
node* del(node *,char []);
node * min(node *);
};
void dict :: create()
{
class node *temp;
int ch;
do
{
temp = new node;
cout<<"\nEnter Keyword:";
cin>>temp->k;
cout<<"\nEnter Meaning:";
cin>>temp->m;
temp->left = NULL;
temp->right = NULL;
if(root == NULL)
{
root = temp;
}
else
{
insert(root, temp);
}
cout<<"\nDo u want to add more (y=1/n=0):";
cin>>ch;
}
while(ch == 1);
}
void dict :: insert(node * root,node *temp)
{
if(strcmp (temp->k, root->k) < 0 )
{
if(root->left == NULL)
root->left = temp;
else
insert(root->left,temp);
}
else
{ if(root->right == NULL)
root->right = temp;
else
insert(root->right,temp);
}
}
void dict:: disp(node * root)
{
if( root != NULL)
{
disp(root->left);
cout<<"\n Key Word :"<<root->k;
cout<<"\t Meaning :"<<root->m;
disp(root->right);
}
}
int dict :: search(node * root,char k[20])
{
int c=0;
while(root != NULL)
{
c++;
if(strcmp (k,root->k) == 0)
{
cout<<"\nNo of Comparisons:"<<c;
return 1;
}
if(strcmp (k, root->k) < 0)
root = root->left;
if(strcmp (k, root->k) > 0)
root = root->right;
}
return -1;
}
int dict :: update(node * root,char k[20])
{
while(root != NULL)
{
if(strcmp (k,root->k) == 0)
{
cout<<"\nEnter New Meaning ofKeyword"<<root->k;
cin>>root->m;
return 1;
}
if(strcmp (k, root->k) < 0)
root = root->left;
if(strcmp (k, root->k) > 0)
root = root->right;
}
return -1;
}
node* dict :: del(node * root,char k[20])
{
node *temp;
if(root == NULL)
{
cout<<"\nElement No Found";
return root;
}
if (strcmp(k,root->k) < 0)
{
root->left = del(root->left, k);
return root;
}
if (strcmp(k,root->k) > 0)
{
root->right = del(root->right, k);
return root;
}
if (root->right==NULL&&root->left==NULL)
{
temp = root;
delete temp;
return NULL;
}
if(root->right==NULL)
{
temp = root;
root = root->left;
delete temp;
return root;
}
else if(root->left==NULL)
{
temp = root;
root = root->right;
delete temp;
return root;
}
temp = min(root->right);
strcpy(root->k,temp->k);
root->right = del(root->right, temp->k);
return root;
}
node * dict :: min(node *q)
{
while(q->left != NULL)
{
q = q->left;
}
return q;
}
int main()
{
int ch;
dict d;
d.root = NULL;
do
{
cout<<"\nMenu\n1.Create\n2.Disp\n3.Search\n4.Update\n5.Delete\nEnter Ur CH:";
cin>>ch;
switch(ch)
{
case 1: d.create();
break;
case 2: if(d.root == NULL)
{
cout<<"\nNo any Keyword";
}
else
{
d.disp(d.root);
}
break;
case 3: if(d.root == NULL)
{
cout<<"\nDictionary is Empty. First add keywords then try again ";
}
else
{
cout<<"\nEnter Keyword which u want to search:";
char k[20];
cin>>k;
if( d.search(d.root,k) == 1)
cout<<"\nKeyword Found";
else
cout<<"\nKeyword Not Found";
}
break;
case 4:
if(d.root == NULL)
{
cout<<"\nDictionary is Empty. First add keywords then try again ";
}
else
{
cout<<"\nEnter Keyword which meaning want to update:";
char k[20];
cin>>k;
if(d.update(d.root,k) == 1)
cout<<"\nMeaning Updated";
else
cout<<"\nMeaning Not Found";
}
break;
case 5:
if(d.root == NULL)
{
cout<<"\nDictionary is Empty. First add keywords then try again ";
}
else
{
cout<<"\nEnter Keyword which u want to delete:";
char k[20];
cin>>k;
if(d.root == NULL)
{
cout<<"\nNo any Keyword";
}
else
{
d.root = d.del(d.root,k);
}
}
}
}
while(ch<=5);
return 0;
}