-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbst.c
206 lines (204 loc) · 4.11 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
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *left, *right;
};
struct node *root = NULL;
struct node *create(int data)
{
struct node *new = (struct node *)malloc(sizeof(struct node));
if (new == NULL)
{
printf("insufficient memory");
}
else
{
new->data = data;
new->right = NULL;
new->left = NULL;
}
return new;
}
// insertion
struct node *insert(struct node *root, int data)
{
if (root == NULL)
{
root = create(data);
}
else if (data < root->data)
{
root->left = insert(root->left, data);
}
else if (data >= root->data)
{
root->right = insert(root->right, data);
}
return root;
}
// searching
struct node *search(struct node *root, int data)
{
if (root == NULL || data == root->data)
{
return root;
}
else if (data < root->data)
{
return search(root->left, data);
}
else if (data > root->data)
{
return search(root->right, data);
}
}
// traversals
void inorder(struct node *root)
{
if (root != NULL)
{
inorder(root->left);
printf("%3d", root->data);
inorder(root->right);
}
}
void preorder(struct node *root)
{
if (root != NULL)
{
printf("%3d", root->data);
preorder(root->left);
preorder(root->right);
}
}
void postorder(struct node *root)
{
if (root != NULL)
{
postorder(root->left);
postorder(root->right);
printf("%3d", root->data);
}
}
// findmin
struct node *findmin(struct node *root)
{
if (root == NULL)
{
return root;
}
else if (root->left != NULL)
{
return root->left = findmin(root->left);
}
else
{
return root;
}
}
// deleting
struct node *delete(struct node *root, int data)
{
if (root == NULL)
{
return root;
}
else if (root->data > data)
{
root->left = delete (root->left, data);
}
else if (root->data < data)
{
root->right = delete (root->right, data);
}
else
{
// NO CHILD
if (root->left == NULL && root->right == NULL)
{
free(root);
return NULL;
}
// 1 CHILD
else if (root->left == NULL || root->right == NULL)
{
struct node *temp;
if (root->left == NULL)
{
temp = root->right;
}
else
{
temp = root->left;
}
root = temp;
free(temp);
return root;
}
else
{
struct node *temp = findmin(root->right);
root->data = temp->data;
root->right = delete (root->right, temp->data);
}
}
return root;
}
void main()
{
int data, ch;
printf("enter the root data: ");
scanf("%d", &data);
root = create(data);
do
{
printf("enter ur choice: ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("enter data to be inserted: ");
scanf("%d", &data);
root = insert(root, data);
break;
case 2:
inorder(root);
printf("\n");
break;
default:
printf("invalid");
break;
case 3:
printf("%d", root->data);
break;
case 4:
printf("enter the data to be searched: ");
scanf("%d", &data);
struct node *ptr = search(root, data);
if (ptr == NULL)
{
printf("not found\n");
}
else
{
printf("found\n");
}
break;
case 5:
preorder(root);
printf("\n");
break;
case 6:
postorder(root);
printf("\n");
break;
case 7:
printf("enter the data to be deleted: ");
scanf("%d", &data);
root = delete (root, data);
break;
}
} while (ch != 0);
}