-
Notifications
You must be signed in to change notification settings - Fork 0
/
BST Dictionary.cpp
141 lines (139 loc) · 2.66 KB
/
BST Dictionary.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
#include <stdio.h>
#include <stdlib.h>
struct dict
{
struct dict *left,*right;
char word[10],meaning[20];
}
*Root=NULL;
typedef struct dict dictionary;
int check(char[],char[]);
void insert(dictionary *);
void Search();
void view(dictionary *);
int check(char a[],char b[])
{
int i,j,c;
for(i=0,j=0 ; a[i]!='\0'&&b[j]!='\0' ; i++,j++)
{
if(a[i]>b[j])
{
c=1;
break;
}
else if(b[j]>a[i])
{
c=-1;
break;
}
else
c=0;
}
if(c==1)
return 1;
else if(c==-1)
return -1;
else
return 0;
}
void Search()
{
int flag=0;
dictionary *ptr;
ptr=Root;
char w[10];
printf("\nEnter word");
scanf("%s",w);
while(ptr!=NULL && flag==0)
{
if(check(w,ptr->word)>0)
ptr=ptr->right;
else if(check(w,ptr->word)<0)
ptr=ptr->left;
else if(check(w,ptr->word)==0)
{
flag=1;
printf("\n%s",ptr->meaning);
}
}
if(flag==0)
printf("\nWord not found");
}
void insert(dictionary *temp)
{
int flag=0;
dictionary *ptr,*par;
ptr=Root;
if(Root==NULL)
Root=temp;
else
{
while(ptr!=NULL )
{
if(check(temp->word,ptr->word)>0)
{
par=ptr;
ptr=ptr->right;
}
else if(check(temp->word,ptr->word)<0)
{
par=ptr;
ptr=ptr->left;
}
else if(check(temp->word,ptr->word)==0)
{
flag=1;
printf("\nWord exists!!");
break;
}
}
if(flag==0 && ptr==NULL)
{
if(check(par->word,temp->word)==1)
par->left=temp;
else if(check(par->word,temp->word)==-1)
par->right=temp;
}
}
}
void view(dictionary *ptr)
{
if(Root==NULL)
printf("\nEmpty dictionary\n");
else if(ptr !=NULL)
{
view(ptr->left);
printf("\nWord:%s\n",ptr->word);
printf("\nMeaning:%s\n",ptr->meaning);
view(ptr->right);
}
}
int main(int argc, char const *argv[])
{
int ch;
dictionary *temp;
while(ch!=4)
{
printf("\n1.Search\n2.Insert\n3.View\n4.Exit\nYour choice please..");
scanf("%d",&ch);
switch (ch)
{
case 1: Search();break;
case 2:
temp=(dictionary*)malloc(sizeof(dictionary));
temp->left=NULL;
temp->right=NULL;
printf("\nInsert word:\n");
scanf("%s",temp->word);
printf("\nInsert meaning:\n");
scanf("%s",temp->meaning);
insert(temp);
break;
case 3:
view(Root);
break;
case 4:exit(0);
}
}
return 0;
}