-
Notifications
You must be signed in to change notification settings - Fork 168
/
Binary_search_tree.c
250 lines (244 loc) · 6.32 KB
/
Binary_search_tree.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
#include <stdio.h>//Header file for input and output
#include <stdlib.h>
// Here structure is created with a name node
struct node
{
int info;// It will represent the info part of node which is of integer type
struct node *left;// It is a ponter of type struct which will point left node
struct node *right;// It is a ponter of type struct which will point right node
};
// Here structure is created with a name stack
struct stack
{
struct node *data;
struct stack *next;
int flag;
};
struct node *root = NULL;
struct node *createbst(struct node *, int);
struct node *preorder(struct node *);
struct node *inorder(struct node *root);
struct node *postorder(struct node *root);
static int item;
int main()
{
int x;
do
{
printf("\n----Main Menu-----\n\n");
printf("1. Create a binary search tree\n");
printf("2. In - order Traversal\n");
printf("3. Pre - order Traversal\n");
printf("4. Post - order Traversal\n");
printf("5. Exit prom the program\n");
printf("Enter your option\n");
scanf("%d", &x);
switch (x)
{
case 1:
printf("Enter - 1 to terminate\n");
printf("Enter the data item:\n");
scanf("%d", &item);
while (item != -1) // termination criteria
{
root = createbst(root, item);
printf("Enter the data item:\n");
scanf("%d", &item);
}
printf("\n**Binary tree is created**\n\n");
break;
case 2:
if (root == NULL)
printf("In - order Traversal: Binary tree is empty\n");
else
{
printf("In - order Traversal\n");
root = inorder(root);
break;
}
break;
case 3:
if (root == NULL)
printf("pre - order Traversal: Binary tree is empty\n");
else
{
printf("\n** Pre - order Traversal **\n");
root = preorder(root);
break;
}
break;
case 4:
if (root == NULL)
printf("Post - order Traversal: Binary tree is empty\n");
else
{
printf("\n ** Post - order Traversal ** \n\n");
root = postorder(root);
break;
}
break;
}
} while (x <= 4);
}
struct node *createbst(struct node *root, int item)
{
struct node *newnode, *ptr;
int value;
newnode = (struct node *)malloc(sizeof(struct node));
if (root == NULL)
{
root = newnode;
root->info = item;
newnode->left = NULL;
newnode->right = NULL;
return root;
}
ptr = root;
do
{
if (item < ptr->info && ptr->left != NULL)
{
ptr = ptr->left;
value = 1;
}
else if (item >= ptr->info && ptr->right != NULL)
{
ptr = ptr->right;
value = 1;
}
else
{
value = 0;
}
} while (value == 1);
if (item < ptr->info)
{
ptr->left = newnode;
}
else
{
ptr->right = newnode;
}
newnode->info = item;
newnode->left = NULL;
newnode->right = NULL;
return root;
}
struct node *preorder(struct node *root)
{
struct node *ptr;
struct stack *ptr1, *top;
ptr = root;
top = (struct stack *)malloc(sizeof(struct stack));
top->data = NULL;
top->next = NULL;
while (ptr != NULL)
{
printf("%d\n", ptr->info);
if (ptr->right != NULL)
{
ptr1 = (struct stack *)malloc(sizeof(struct stack));
ptr1->data = ptr->right;
ptr1->next = top;
top = ptr1;
}
if (ptr->left != NULL)
{
ptr = ptr->left;
}
else
{
ptr = top->data;
ptr1 = top;
top = top->next;
}
}
return root;
}
struct node *inorder(struct node *root)
{
int value;
struct node *ptr;
struct stack *ptr1, *top;
ptr = root;
top = (struct stack *)malloc(sizeof(struct stack));
top->data = NULL;
top->next = NULL;
do
{
while (ptr != NULL)
{
ptr1 = (struct stack *)malloc(sizeof(struct stack));
ptr1->next = top;
top = ptr1;
top->data = ptr;
ptr = ptr->left;
}
ptr = top->data;
top = top->next;
while (ptr != NULL)
{
printf("%d\n", ptr->info);
if (ptr->right != NULL)
{
ptr = ptr->right;
value = 1;
break;
}
else
{
ptr = top->data;
top = top->next;
value = 0;
}
}
} while (value == 1);
return root;
}
struct node *postorder(struct node *root)
{
int oper, value;
struct node *ptr;
struct stack *ptr1, *top;
ptr = root;
top = (struct stack *)malloc(sizeof(struct stack));
top->data = NULL;
top->next = NULL;
do
{
while (ptr != NULL)
{
ptr1 = (struct stack *)malloc(sizeof(struct stack));
ptr1->data = ptr;
ptr1->next = top;
top = ptr1;
top->flag = 0;
if (ptr->right != NULL)
{
ptr1 = (struct stack *)malloc(sizeof(struct stack));
ptr1->data = ptr->right;
ptr1->next = top;
top = ptr1;
top->flag = -1;
}
ptr = ptr->left;
}
ptr = top->data;
oper = top->flag;
top = top->next;
while (oper == 0)
{
printf("%d\n", ptr->info);
oper = top->flag;
ptr = top->data;
top = top->next;
value = 0;
}
if (oper == -1)
{
oper = 0;
value = 1;
}
} while (value == 1);
return root;
}