-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdictionary.c
331 lines (298 loc) · 6.98 KB
/
dictionary.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
typedef struct avlnode* position;
typedef struct avlnode* avltree;
// declaring avl tree
struct avlnode{
avltree l;
avltree r;
int ht;
char eng[100],meaning[100];
};
//function to return height
static int ht(position p){
if(p == NULL)
return -1;
else
return p->ht;
}
// function to return the maximum of two numbers
int max(int a,int b){
if(a>b)
return a;
else
return b;
}
// function for single rotate with left
position Rl(position k2){
position k1;
k1 = k2->l;
k2->l = k1->r;
k1->r = k2;
k2->ht = max(ht(k2->l),ht(k2->r))+1;
k1->ht = max(ht(k1->l),k2-> ht)+1;
return k1;
}
// function for single rotate with right
position Rr(position k2){
position k1;
k1 = k2->r;
k2->r = k1->l;
k1->l = k2;
k2->ht = max(ht(k2->l),ht(k2->r))+1;
k1->ht = max(ht(k1->r),k2-> ht)+1;
return k1;
}
// function for double rotate with left
position RRl(position k3){
k3->l = Rr(k3->l);
k3 = Rl(k3);
return k3;
}
//function for double rotate with right
position RRr(position k3){
k3->r = Rl(k3->r);
k3 = Rr(k3);
return k3;
}
// function to insert into avl tree
avltree insert(avltree t, char eng[], char meaning[]){
if(t==NULL){
t = (avltree) malloc(sizeof(struct avlnode));
strcpy(t->eng,eng);
strcpy(t->meaning,meaning);
t->ht = 0;
t->l = t->r = NULL;
}
else if(strcasecmp(eng,t->eng)<0){
t->l = insert(t->l,eng,meaning);
if(ht(t->l)-ht(t->r)==2){
if(strcasecmp(eng,t->l->eng)<0)
t = Rl(t);
else
t = RRl(t);
}
}
else if(strcasecmp(eng,t->eng)>0){
t->r = insert(t->r,eng,meaning);
if(ht(t->r)-ht(t->l)==2){
if(strcasecmp(eng,t->r->eng)>0)
t = Rr(t);
else
t = RRr(t);
}
}
t->ht = max(ht(t->l),ht(t->r)) + 1;
return t;
}
// function to balanceavl tree
int getBalance(avltree N){
if (N == NULL)
return 0;
return ht(N->l) - ht(N->r);
}
// function that returns minimum value
avltree minValueNode(avltree node){
avltree curr = node;
while (curr->l != NULL)
curr = curr->l;
return curr;
}
// function to delete node
avltree deleteNode(avltree root, char key[]){
if (root == NULL)
return root;
if (strcasecmp(key,root->eng)<0)
root->l = deleteNode(root->l, key);
else if(strcasecmp(key,root->eng)>0)
root->r = deleteNode(root->r, key);
else{
if( (root->l == NULL) || (root->r == NULL) ) {
avltree temp = root->l ? root->l : root->r;
if (temp == NULL){
temp = root;
root = NULL;
}
else
*root = *temp;
free(temp);
}
else{
avltree temp = minValueNode(root->r);
strcpy(root->eng,temp->eng);
strcpy(root->meaning,temp->meaning);
root->r = deleteNode(root->r, temp->eng);
}
}
if (root == NULL)
return root;
root->ht = 1 + max(ht(root->l),ht(root->r));
int balance = getBalance(root);
if (balance > 1 && getBalance(root->l) >= 0)
return Rr(root);
if (balance > 1 && getBalance(root->l) < 0){
root->l = Rl(root->l);
return Rr(root);
}
if (balance < -1 && getBalance(root->r) <= 0)
return Rl(root);
if (balance < -1 && getBalance(root->r) > 0){
root->r = Rr(root->r);
return Rl(root);
}
return root;
}
// function to find word
position find(avltree a,char word[]){
if(a==NULL)
return NULL;
else if(strcasecmp(word,a->eng)<0)
return find(a->l,word);
else if(strcasecmp(word,a->eng)>0)
return find(a->r,word);
else
return a;
}
// function to print inorder
void inorder(avltree t,char op){
if(t!=NULL){
inorder(t->l,op);
if(t->eng[0] == op)
printf("%s : %s\n",t->eng,t->meaning);
inorder(t->r,op);
}
}
// function to find meaning
int findMeaning(avltree t, char s[]){
position p = find(t,s);
if(p!=NULL){
printf("%s : %s\n",s,p->meaning);
return 0;
}
else
printf( "Sorry %s is not in the dictionary...\n",s);
return -1;
}
// function to add element to avl tree
avltree add(avltree t, char word[]){
position f = find(t,word);
if(f==NULL){
printf("\nEnter the meaning of the given word : ");
char meaning[100];
scanf(" %[^\n]",meaning);
t = insert(t,word,meaning);
printf("\nThe word was successfully added.\n");
}
else{
printf("\nSorry the word already exists...\n");
}
return t;
}
// function to search in avl tree
avltree search(avltree t){
char word[100];
printf("Enter the word to be searched for : ");
scanf("%s",word);
int ch = findMeaning(t,word);
if(ch == -1){
printf("\n\nDo you want to add the word to the dictionary? (Yes-1/No-0)\n");
scanf("%d",&ch);
if(ch == 1)
t = add(t,word);
}
return t;
}
// view avl tree
void view(avltree t){
char op;
printf("Enter the starting alphabet : ");
scanf(" %c",&op);
system("clear");
inorder(t,op);
}
void viewall(avltree t){
getchar();
for(char op = 'a'; op <='z'; op++){
system("clear");
inorder(t,op);
printf("\nthe words starting with %c are over",op);
printf("\n\nPRESS ENTER TO CONTINUE...");
getchar();
}
}
avltree createdict(avltree t){
FILE *fp;
fp = fopen("dict.txt","r");
char word[100],meaning[100];
while(fscanf(fp,"%s",word) != EOF){
fscanf(fp," %[^\n]",meaning);
t = insert(t,word,meaning);
}
fclose(fp);
return t;
}
void writeout(avltree t,FILE *fp){
if(t!=NULL){
writeout(t->l,fp);
fprintf(fp,"%s %s\n",t->eng,t->meaning);
writeout(t->r,fp);
}
}
int main(void){
avltree tree = NULL;
tree = createdict(tree);
int op = 1;
while(op != 0){
printf("\nMENU:");
printf("\n1. Add a word\n2. Delete a word\n3. Search for a word\n");
printf("4. Display words starting with a given alphabet\n5. Display all the words\n0. Exit the dictionary\n");
printf("Choose an option : ");
scanf("%d",&op);
char word[100];
printf("\n");
switch(op){
case 1 :
printf("Enter the word to be added : ");
scanf("%s",word);
tree = add(tree,word);
break;
case 2 :
printf("Enter the word to be deleted : ");
scanf("%s",word);
position f = find(tree,word);
if(f!=NULL){
tree = deleteNode(tree,word);
printf("\nThe word was successfully deleted.\n");
}
else{
printf("Sorry %s is not in the dictionary...\n",word);
}
break;
case 3 :
tree = search(tree);
break;
case 4 :
view(tree);
break;
case 5 :
viewall(tree);
break;
case 0 :
break;
default : printf("INCORRECT OPTION... CHOOSE AGAIN : ");
}
if(op !=5 ){
printf("PRESS ENTER TO CONTINUE...");
getchar();
getchar();
}
}
FILE *fp;
fp = fopen("dict.txt","w");
writeout(tree,fp);
printf("\nTHE DICTIONARY IS SAVED...\n");
printf("Thank you...");
fclose(fp);
}