-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBST.c
205 lines (197 loc) · 4.14 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
// Program to implement BST (not BTS)
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
// char data[20];
struct node *left;
struct node *right;
};
// creation
struct node *create(int num)
{
struct node *newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = num;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// searching and checking
struct node *search(struct node *root, int key)
{
if (root == NULL || root->data == key)
{
return root;
}
else if (key < root->data)
{
search(root->left, key);
}
else
{
search(root->right, key);
}
}
// insertion
struct node *insert(struct node *root, int num)
{
if (root == NULL)
{
return create(num);
}
else if (num < root->data)
root->left = insert(root->left, num);
else
root->right = insert(root->right, num);
return root;
}
// traversal
void preorder(struct node *root)
{
if (root != NULL)
{
printf("%d | ", root->data);
preorder(root->left);
preorder(root->right);
}
}
void inorder(struct node *root)
{
if (root != NULL)
{
inorder(root->left);
printf("%d | ", root->data);
inorder(root->right);
}
}
void postorder(struct node *root)
{
if (root != NULL)
{
postorder(root->left);
postorder(root->right);
printf("%d | ", root->data);
}
}
// inorder predecessor
struct node *inorderPre(struct node *root)
{
if (root == NULL || root->left == NULL)
{
return NULL;
}
root = root->left;
while (root->right != NULL)
root = root->right;
return root;
}
// deletion
struct node *deleteNode(struct node *root, int d)
{
if (root == NULL)
{
return NULL;
}
if (root->data > d)
root->left = deleteNode(root->left, d);
else if (root->data < d)
root->right = deleteNode(root->right, d);
else
{
if (root->left == NULL)
{
struct node *temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL)
{
struct node *temp = root->left;
free(root);
return temp;
}
struct node *temp = inorderPre(root);
root->data = temp->data;
root->left = deleteNode(root->left, temp->data);
}
return root;
}
int main()
{
struct node *root = NULL;
int n, num;
// insertion
printf("Enter the number of items: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
printf("Enter the data: ");
scanf("%d", &num);
root = insert(root, num);
}
// Traversal
printf("\nBefore deletion:\n");
printf("Preorder:\n");
preorder(root);
printf("\nInorder:\n");
inorder(root);
printf("\nPostoder:\n");
postorder(root);
// Searching
int srNum;
struct node *srItem;
printf("\n\n\tSearching part\n");
printf("\nEnter the data for which you want to search: ");
scanf("%d", &srNum);
srItem = search(root, srNum);
if (srItem != NULL){
printf("Element found");
}
else{
printf("Element not found in the tree");
}
// Deletion
int delNum;
printf("\n\n\tDeletion part\n");
printf("\nEnter the data for which you want to delete: ");
scanf("%d", &delNum);
root = deleteNode(root, delNum);
// Traversal
printf("\nAfter deletion:\n");
printf("Preorder:\n");
preorder(root);
printf("\nInorder:\n");
inorder(root);
printf("\nPostoder:\n");
postorder(root);
return 0;
}
// Output:
/*
Enter the number of items: 5
Enter the data: 30
Enter the data: 20
Enter the data: 10
Enter the data: 25
Enter the data: 23
Before deletion:
Preorder:
30 | 20 | 10 | 25 | 23 |
Inorder:
10 | 20 | 23 | 25 | 30 |
Postoder:
10 | 23 | 25 | 20 | 30 |
Searching part
Enter the data for which you want to searh: 45
Element not found in the tree
Deletion part
Enter the data for which you want to delete: 10
After deletion:
Preorder:
30 | 20 | 25 | 23 |
Inorder:
20 | 23 | 25 | 30 |
Postoder:
23 | 25 | 20 | 30 |
*/