forked from oops-aman/Data-Structures-in-C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbst.c
224 lines (205 loc) · 5.01 KB
/
bst.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
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
struct node
{
int data;
struct node *left;
struct node *right;
}*root = NULL;
struct node* createNode(int item)
{
struct node *newNode=(struct node*)malloc(sizeof(struct node*));
newNode->data = item;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
void insert(int item)
{
struct node *newNode = createNode(item);
if(root == NULL)
{
root=newNode;
return;
}
else
{
struct node *current = root,*parent = NULL;
while(true)
{
parent = current;
if(item < current->data)
{
current = current->left;
if(current == NULL)
{
parent->left = newNode;
return;
}
}
else
{
current = current->right;
if(current == NULL)
{
parent->right = newNode;
return;
}
}
}
}
}
struct node* minNode(struct node *root)
{
if(root->left != NULL)
{
return minNode(root->left);
}
else
{
return root;
}
}
struct node* delete(struct node *current, int item)
{
if(current == NULL)
{
return NULL;
}
else
{
if(item < current->data)
{
current->left = delete(current->left, item);
}
else if(item >current->data)
{
current->right = delete(current->right, item);
}
else
{
if(current->left == NULL && current->right == NULL)
{
current = NULL;
}
else if(current->left == NULL)
{
current = current->right;
}
else if(current->right == NULL)
{
current = current->left;
}
else
{
struct node *temp = minNode(current->right);
current->data = temp->data;
current->right = delete(current->right, temp->data);
}
}
return current;
}
}
void inorder(struct node *temp)
{
if(root == NULL)
{
printf("\nTree is empty !");
return;
}
if(temp != NULL)
{
inorder(temp->left);
printf("%d ",temp->data);
inorder(temp->right);
}
}
void preorder(struct node *temp)
{
if(root == NULL)
{
printf("\nTree is empty !");
return;
}
if(temp != NULL)
{
printf("%d ",temp->data);
preorder(temp->left);
preorder(temp->right);
}
}
void postorder(struct node *temp)
{
if(root == NULL)
{
printf("\nTree is empty !");
return;
}
if(temp != NULL)
{
postorder(temp->left);
postorder(temp->right);
printf("%d ",temp->data);
}
}
void display(struct node *temp, int level)
{
int i;
if(temp != NULL)
{
display(temp->right, level+1);
printf("\n");
for(i = 0; i < level; i++)
{
printf(" ");
}
printf("%d",temp->data);
display(temp->left, level+1);
}
}
int main()
{
int ch1 = 1,ch2,ch3,num;
while(ch1 == 1)
{
printf("1.Insertion \n2.Deletion \n3.Traversal \n4.Display \n5.Exit");
printf("\nEnter your choice (1-5) : ");
scanf("%d",&ch2);
switch(ch2)
{
case 1 : printf("\nEnter data to be inserted : ");
scanf("%d",&num);
insert(num);
break;
case 2 : printf("Enter data to be deleted : ");
scanf("%d",&num);
delete(root, num);
break;
case 3 : printf("\n1.Inorder \n2.Preorder \n3.Postorder");
printf("\nEnter your choice (1-3) : ");
scanf("%d",&ch3);
switch(ch3)
{
case 1 : inorder(root);
break;
case 2 : preorder(root);
break;
case 3 : postorder(root);
break;
default : printf("\nInvalide Choice !");
break;
}
break;
case 4 : display(root,1);
break;
case 5 : exit(0);
break;
default : printf("\nInvalid Choice !");
break;
}
printf("\nWant to continue ? (1-Yes/0-No) : ");
scanf("%d",&ch1);
}
return 0;
}