-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram10
More file actions
164 lines (151 loc) · 3 KB
/
program10
File metadata and controls
164 lines (151 loc) · 3 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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
char info[20];
struct node *left;
struct node *right;
};
typedef struct node *NODEPTR;
NODEPTR maketree(char word[]);
NODEPTR createtree(char word[]);
void setleft(NODEPTR p,char word[]);
void setright(NODEPTR p,char word[]);
void intrav(NODEPTR p);
void pretrav(NODEPTR p);
void posttrav(NODEPTR p);
void search(NODEPTR p,char key[]);
void main()
{
NODEPTR ptree;
NODEPTR p,q;
char word[20],key[20];
int opt,f;
do{
printf("\n1->CREATE DICTIONARY 2->TRAVERSE 3->SEARCH 4->EXIT ");
printf("\nEnter your option:");
scanf("%d",& opt);
switch(opt)
{
case 1: printf("\nEnter a word :");
scanf("%s",word);
ptree=maketree(word);
while(strcmp(word,"END")!=0)
{
printf("\nEnter a word(Type END to stop):");
scanf("%s",word);
if(strcmp(word,"END")==0)
break;
p=q=ptree;
while((strcmp(word,p->info)!=0) && q!=NULL)
{
p=q;
if(strcmp(word,p->info)<0)
q=p->left;
else
q=p->right;
}
if(strcmp(word,p->info)<0)
setleft(p,word);
else if(strcmp(word,p->info)>=0)
setright(p,word);
}
printf("\n DICTIONARY CREATED SUCCESSFULLY");
break;
case 2 : printf("\n PREORDER TRAVERSAL OF THE DICTIONARY IS:");
pretrav(ptree);
printf("\n INORDER TRAVERSAL OF THE DICTIONARY IS:");
intrav(ptree);
printf("\n POSTORDER TRAVERSAL OF THE DICTIONARY IS:");
posttrav(ptree);
break;
case 3: printf("\n Enter the key to search in the dictionary:");
scanf("%s",key);
search(ptree,key);
//if(f==-1)
// printf("\n Key %s is not found in the dictionary");
//else
// printf("\n Key %s is found in the dictionary");
break;
case 4: printf("\nEXITING BINARY TREE");
exit(1);
}
}while(opt!=4);
}
NODEPTR maketree(char w[])
{
NODEPTR t;
t=(NODEPTR)malloc(sizeof(struct node));
if(t==NULL)
{
printf("\n Node allocation failed");
exit(0);
}
strcpy(t->info,w);
t->left=NULL;
t->right=NULL;
return(t);
}
void setleft(NODEPTR p,char w[])
{
if(p==NULL)
printf("Void Insertion");
else if(p->left!=NULL)
printf("Invalid Insertion");
else
p->left=maketree(w);
}
void setright(NODEPTR p,char w[])
{
if(p==NULL)
printf("Void Insertion");
else if(p->right!=NULL)
printf("Invalid Insertion");
else
p->right=maketree(w);
}
void intrav(NODEPTR tree)
{
if(tree!=NULL)
{
intrav(tree->left);
printf("%s\t",tree->info);
intrav(tree->right);
}
}
void pretrav(NODEPTR tree)
{
if(tree!=NULL)
{
printf("%s\t",tree->info);
pretrav(tree->left);
pretrav(tree->right);
}
}
void posttrav(NODEPTR tree)
{
if(tree!=NULL)
{
posttrav(tree->left);
posttrav(tree->right);
printf("%s\t",tree->info);
}
}
void search(NODEPTR tree,char key[])
{
NODEPTR p,q;
p=q=tree;
while((strcmp(key,p->info)!=0) && q!=NULL)
{
p=q;
if(strcmp(key,p->info)<0)
q=p->left;
else
q=p->right;
}
if(strcmp(key,p->info)==0)
printf("\n Key %s is found in the dictionary",key);
else
printf("\n Key %s is not found in the dictionary",key);
}