-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinary Search Tree on Employees
183 lines (182 loc) · 3.75 KB
/
Binary Search Tree on Employees
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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MALLOC(p,s)\
if(!(p=malloc(s)))\
{\
printf("Insufficient Memory\n");\
exit(0);\
}
struct node
{
struct node *llink;
char name[20];
char id[20];
float sal;
struct node *rlink;
};
typedef struct node ep;
void inorder(ep *r)
{
if(r)
{
inorder(r->llink);
printf("%s %s %f\n",r->name,r->id,r->sal);
inorder(r->rlink);
}
}
ep *insert(ep *root,ep *t)
{
ep *cur=root,*prev=0;
if(!cur)
return t;
while(cur)
{
if(!strcmp(t->name,cur->name))
{
printf("Reduepancy\n");
free(t);
return root;
}
prev=cur;
if(strcmp(t->name,cur->name)<1)
cur=cur->llink;
else
cur=cur->rlink;
}
if(strcmp(t->name,prev->name)<1)
prev->llink=t;
else
prev->rlink=t;
return root;
}
void iterSearch(ep *root)
{
if(!root)
{
printf("BST empty\n");
return;
}
char name[20];
printf("Enter the name to searched = ");
scanf("%s",name);
while(root)
{
if(!strcmp(name,root->name))
{
printf("Search successful\n");
return;
}
if(strcmp(name,root->name)<1)
root=root->llink;
else
root=root->rlink;
}
printf("Unsuccessful search\n");
}
void recurSearch(ep *root,char name[])
{
if(!root)
{
printf("Unsuccessful search\n");
return;
}
if(!strcmp(name,root->name))
{
printf("Successful search\n");
return;
}
if(strcmp(name,root->name)<1)
return recurSearch(root->llink,name);
return recurSearch(root->rlink,name);
}
ep *deletenode(ep *root,char name[])
{
ep *cur,*parent,*q,*suc;
parent=0;
cur=root;
while(cur)
{
if(!strcmp(name,cur->name))
{
printf("Employee has been Successfully Deleted\n");
break;
}
parent=cur;
if(strcmp(name,cur->name)<1)
cur=cur->llink;
else
cur=cur->rlink;
}
if(!cur)
{
printf("Employee not found.\nTry searching again!\n");
return root;
}
if(!cur->llink)
q=cur->rlink;
else if(!cur->rlink)
q=cur->llink;
else
{
suc = cur->rlink;
while(suc->llink)
suc=suc->llink;
suc->llink=cur->llink;
q=cur->rlink;
}
if(!parent)
return q;
if(cur == parent->llink)
parent->llink = q;
else
parent->rlink = q;
free(cur);
return root;
}
int main()
{
ep *root=0;
int ch,i;
FILE *fp;
fp=fopen("data2","r");
if(fp==0)
{
printf("File Open Unsuccessfull!\n");
exit(0);
}
for(;;)
{
printf("\n1. Insert\n2. Iterative Search\n3. Recursive Search\n");
printf("4. Inorder\n5. Delete a node\n6. Exit\n");
printf("Enter choice = ");
scanf("%d",&ch);
switch(ch)
{
case 1: fscanf(fp,"%d",&ch);
for(i=0;i<ch;i++)
{
ep *t;
MALLOC(t,sizeof(ep));
fscanf(fp,"%s%s%f",t->name,t->id,&(t->sal));
t->llink=t->rlink=0;
root=insert(root,t);
}
break;
case 2: iterSearch(root);
break;
case 3: printf("Enter the name to searched = ");
char name[20];
scanf("%s",name);
recurSearch(root,name);
break;
case 4: inorder(root);
break;
case 5: printf("Enter the name to deleted = ");
scanf("%s",name);
root = deletenode(root,name);
break;
case 6: return 0;
}
}
}