-
Notifications
You must be signed in to change notification settings - Fork 449
/
BinarySearchTree.c
213 lines (194 loc) · 4.82 KB
/
BinarySearchTree.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
/*
This program implements the insertion of a Binary Search Tree and its functions(Searching, Finding MIN & MAX, Traversal) as well.
*/
#include <stdio.h>
#include <stdlib.h>
struct node {
struct node* left;
int data;
struct node* right;
}*root = NULL, *newNode, *current;
//creating a new node for the tree
struct node* create(int item)
{
//create node
newNode = (struct node*)malloc(sizeof(struct node*));
newNode->data = item;
newNode->left = newNode->right = NULL;
return newNode;
}
//inserting the new node to the tree
void insert(struct node* current, struct node* newNode)
{
//insert as a leaf to the node
if(root == NULL)
{
root = newNode;
return;
}
else if (current->data > newNode->data)
{
if (current->left == NULL)
current->left = newNode;
else
insert(current->left, newNode);
}
else
{
if (current->right == NULL)
current->right = newNode;
else
insert(current->right, newNode);
}
}
//finding the node with the MIN key value
void find_MIN(struct node *current)
{
if(current->left != NULL)
find_MIN(current->left);
else
printf("\nMIN node key: %d\n", current->data);
return;
}
//finding the node with the MAX key value
void find_MAX(struct node *current)
{
if(current->right != NULL)
find_MIN(current->right);
else
printf("\nMIN node key: %d\n", current->data);
return;
}
//This function traverses the tree in preorder fashion
void preorder(struct node *node)
{
if(node != NULL)
{
printf("\n%d", node->data);
preorder(node->left);
preorder(node->right);
}
}
//This function traverses the tree in inorder fashion
void inorder(struct node *node)
{
if(node != NULL)
{
inorder(node->left);
printf("\n%d", node->data);
inorder(node->right);
}
}
//This function traverses the tree in postorder fashion
void postorder(struct node *node)
{
if(node != NULL)
{
postorder(node->left);
postorder(node->right);
printf("\n%d", node->data);
}
}
//Searching whether an Node exists or not
int search(struct node *current, int se)
{
if(current == NULL)
{
return 0;
}
if(se == current->data)
{
printf("\nThe key %d is found in the tree\n", se);
return 1;
}
else if(se < current->data)
search(current->left, se);
else
search(current->right, se);
}
void main()
{
int choice, item, se, result;
struct node* new;
while(1)
{
printf("\n---------ENTER YOUR CHOICE--------------\n");
printf(" 1.BST insertion\n 2.Find MIN\n 3.Find MAX\n 4.Search for the key\n 5.Traversal(Inorder)\n 5.Traversal(Preorder)\n");
printf(" 6.Traversal(Inorder)\n 7.Traversal(Postorder)\n 8.Exit\n");
scanf("%d", &choice);
switch(choice)
{
case 1:{
printf("\nEnter the item you want to insert: ");
scanf("%d", &item);
new = create(item);
insert(root, new);
break;
}
case 2:{
if(!root)
{
printf("\nTree is Empty. Please enter nodes first\n");
break;
}
find_MIN(root);
break;
}
case 3:{
if(!root)
{
printf("\nTree is Empty. Please enter nodes first\n");
break;
}
find_MAX(root);
break;
}
case 4:{
if(!root)
{
printf("\nTree is Empty. Please enter nodes first\n");
break;
}
printf("Enter the key you want to search");
scanf("%d", &se);
result = search(root, se);
if(result == 0)
printf("Node doesnt exist");
break;
}
case 5:{
if(!root)
{
printf("\nTree is Empty. Please enter nodes first\n");
break;
}
printf("\n-------Traversal(PREORDER)-------\n");
preorder(root);
break;
}
case 6:{
if(!root)
{
printf("\nTree is Empty. Please enter nodes first\n");
break;
}
printf("\n-------Traversal(INORDER)-------\n");
inorder(root);
break;
}
case 7:{
if(!root)
{
printf("\nTree is Empty. Please enter nodes first\n");
break;
}
printf("\n-------Traversal(POSTORDER)-------\n");
postorder(root);
break;
}
case 8:{
exit(0);
}
}
}
}