-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbinarysearchtree.c
197 lines (184 loc) · 4.87 KB
/
binarysearchtree.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
//To implement operations on a Binary Search Tree
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *llink;
struct node *rlink;
};
typedef struct node NODE;
NODE *insert(int item,NODE *root)
{
NODE *temp,*cur,*prev;
temp=(NODE *)malloc(sizeof(NODE));
temp->info=item;
temp->llink=NULL;
temp->rlink=NULL;
if(root==NULL)
return temp;
prev=NULL;
cur=root;
while(cur!=NULL)
{
prev=cur;
if(cur->info < item )
cur=cur->rlink;
else
cur=cur->llink;
}
if(item<prev->info)
prev->llink=temp;
else
prev->rlink=temp;
return root;
}
NODE *construct_BST(NODE *root)
{
int a,n,i;
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("Enter the elements to be inserted in the tree\n");
for (i=0;i<n;i++)
{
scanf("%d",&a);
root=insert(a,root);
}
printf("Tree Constructed Successfully!!!!!!\n");
return root;
}
void preorder(NODE *root)
{
if(root!=NULL)
{
printf("%d\t",root->info);
preorder(root->llink);
preorder(root->rlink);
}
}
void inorder(NODE *root)
{
if(root!=NULL)
{
inorder(root->llink);
printf("%d\t",root->info);
inorder(root->rlink);
}
}
void postorder(NODE *root)
{
if(root==NULL)
return ;
else
{
postorder(root->llink);
postorder(root->rlink);
printf("%d\t",root->info);
}
}
int search_element(NODE *root,int key)
{
NODE *cur;
int n=0;
cur=root;
if (cur!=NULL)
{
if (key==cur->info)
n=1;
else if (key<cur->info)
return search_element(cur->llink,key);
else
return search_element(cur->rlink,key);
}
else
return n;
}
NODE *minValueNode(NODE* node)
{
NODE *current = node;
while (current->llink != NULL)
current = current->llink;
return current;
}
NODE *delete_element(NODE *root,int key)
{
if (root == NULL)
return root;
if (key < root->info)
root->llink = delete_element(root->llink, key);
else if (key > root->info)
root->rlink = delete_element(root->rlink, key);
else
{
if (root->llink == NULL)
{
NODE *temp = root->rlink;
free(root);
return temp;
}
else if (root->rlink == NULL)
{
NODE *temp = root->llink;
free(root);
return temp;
}
else
{
NODE *temp = minValueNode(root->rlink);
root->info = temp->info;
root->rlink = delete_element(root->rlink, temp->info);
}
}
return root;
}
void main()
{
int item,ch,key,n;
NODE *root;
root=NULL;
while (1)
{
printf("\nEnter the choice\n1.ConstructBST\n2.Preorder\n3.Inorder\n4.Postorder\n5.Search an Element\n6.Delete an Element\n7:Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1: root=construct_BST(root);
break;
case 2: preorder(root);
break;
case 3: inorder(root);
break;
case 4: postorder(root);
break;
case 5: if (root==NULL)
printf("List Empty\n");
else
{
printf("Enter the element\n");
scanf("%d",&key);
n=search_element(root,key);
if(n)
printf("Key found\n");
else
printf("Not found\n");
}
break;
case 6:
if (root==NULL)
printf("List Empty\n");
else
{
printf("Enter the element\n");
scanf("%d",&key);
n=search_element(root,key);
if(n)
root=delete_element(root,key);
else
printf("Not found\n");
}
break;
case 7: exit(0);
default: printf("Wrong Choice\n");
}
}
}