-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathbinary_search_tree.c
199 lines (178 loc) · 4.42 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
/*
* Date: 2017-11-07
*
* Description:
* - Insert in Binary search tree(BST)
* - Inorder, Preorder, Postorder
* - Search in BST
* - Delete from BST
*
* Reference:
* http://www.geeksforgeeks.org/binary-search-tree-set-1-search-and-insertion/
* http://www.geeksforgeeks.org/binary-search-tree-set-2-delete/
*
* Complexity:
* Insert, search, delete - O(logn)
* Traversal - O(n)
*
* Limitation:
* If BST is not balanced (or skwed trees), it becomes a linked list and all
* operations take O(n) complexity. This problem is handled by balancing trees.
* AVL, Red-black, B trees are examples of balanced trees.
*/
#include "stdio.h"
#include "stdlib.h"
typedef struct node {
int key;
struct node *left;
struct node *right;
}node;
void print_array(int arr[], int n, char *msg) {
int i = 0;
printf("*********** %s *****************\n", msg);
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n\n");
}
node* new(int k) {
node *ptr = (node *)malloc(sizeof(node));
ptr->key = k;
ptr->left = NULL;
ptr->right = NULL;
return ptr;
}
node* insert(node *root, int k) {
if (root == NULL)
return new(k);
else if (root->key > k)
root->left = insert(root->left, k);
else if (root->key < k)
root->right = insert(root->right, k);
else {
printf("[ERROR]: Duplicate key[%d] not allowed in BST\n", k);
return root;
}
return root;
}
void preorder(node *root) {
if (root != NULL) {
printf ("%d ", root->key);
preorder(root->left);
preorder(root->right);
}
}
void inorder(node *root) {
if (root != NULL) {
inorder(root->left);
printf ("%d ", root->key);
inorder(root->right);
}
}
void postorder(node *root) {
if (root != NULL) {
postorder(root->left);
postorder(root->right);
printf ("%d ", root->key);
}
}
node* search(node *root, int k) {
if (root == NULL)
return NULL;
else if (root->key == k)
return root;
else if(root->key > k)
return search(root->left, k);
else
return search(root->right, k);
}
/*
* Finds minimum element in the sub tree rooted with node passed.
*
* Args:
* node: Root of sub tree(whose min element is required).
*
* Returns: Reference to min element.
*/
node* find_min_element_in_bst(node *node) {
while (node->left != NULL)
node = node->left;
return node;
}
node* delete(node *root, int key) {
node *tmp = NULL;
if (root == NULL)
return root;
else if (root->key > key)
root->left = delete(root->left, key);
else if (root->key < key)
root->right = delete(root->right, key);
else
{
// Node to be deleted has 1 or 0 child nodes - Copy the leaf node to
// current node and deleted copied node.
if (root->left == NULL) {
tmp = root->right;
free(root);
return tmp;
}
else if (root->right == NULL) {
tmp = root->left;
free(root);
return tmp;
}
// Node to be deleted has 2 child nodes - find in-order successor(smallest
// element) in right sub tree and copy data of in-order successor to current
// node and delete in-order successor.
else {
tmp = find_min_element_in_bst(root->right);
root->key = tmp->key;
root->right = delete(root->right, tmp->key);
}
}
return root;
}
int main() {
int i = 0;
int n = 0;
int *a = NULL;
int element = 0;
node *root = NULL;
node *curr_node = NULL;
printf("Enter number of elements: ");
scanf("%d",&n);
a = (int *)malloc(sizeof(int)*n);
for (i = 0; i < n; i++) {
printf("Enter element [%d]: ", i);
scanf("%d",&a[i]);
}
print_array(a, n, "Inserted Array");
// Driver loop to insert in BST
for (i = 0; i < n; i++) {
root = insert(root, a[i]);
}
printf("*********** PreOrder Traversal *****************\n");
preorder(root);
printf("\n\n");
printf("*********** Inorder Traversal *****************\n");
inorder(root);
printf("\n\n");
printf("*********** Postorder Traversal *****************\n");
postorder(root);
printf("\n\n");
// Search in a BST
printf("Enter element to be searched in BST: ");
scanf("%d", &element);
curr_node = search(root, element);
if (curr_node)
printf("%d found in BST\n\n", element);
else
printf("%d not found in BST\n\n", element);
// Delete from BST
printf("Enter element to be deleted from BST: ");
scanf("%d", &element);
delete(root, element);
printf("*********** Inorder traversal after deletion *****************\n");
inorder(root);
printf("\n\n");
return 0;
}