-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinary_Search_Tree_Operations.c
179 lines (179 loc) · 3.66 KB
/
Binary_Search_Tree_Operations.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
#include <stdio.h>
#include <stdlib.h>
struct node
{
int element;
struct node *left;
struct node *right;
};
typedef struct node node;
node *insert(node *tree, int x)
{
if (tree == NULL)
{
node *newnode = malloc(sizeof(node));
newnode->right = newnode->left = NULL;
newnode->element = x;
tree = newnode;
}
else if (x < tree->element)
{
tree->left = insert(tree->left, x);
}
else if (x > tree->element)
{
tree->right = insert(tree->right, x);
}
return tree;
}
void inorder(node *tree)
{
if (tree != NULL)
{
inorder(tree->left);
printf("%d ", tree->element);
inorder(tree->right);
}
}
node *search(node *tree, int x)
{
if (tree == NULL)
{
return NULL;
}
else if (x < tree->element)
{
return search(tree->left, x);
}
else if (x > tree->element)
{
return search(tree->right, x);
}
else
{
return tree;
}
}
node *searchMin(node *tree)
{
if (tree == NULL)
{
return NULL;
}
else if (tree->left == NULL)
{
return tree;
}
else
{
return searchMin(tree->left);
}
}
void searchMax(node *tree)
{
if (tree == NULL)
{
printf("Tree is empty\n");
}
else
{
while (tree->right != NULL)
{
tree = tree->right;
}
printf("%d is the maximum element\n", tree->element);
}
}
node *delete (node *tree, int x)
{
node *temp;
if (x < tree->element)
{
tree->left = delete (tree->left, x);
}
else if (x > tree->element)
{
tree->right = delete (tree->right, x);
}
else if (tree->left && tree->right)
{
temp = searchMin(tree->right);
tree->element = temp->element;
tree->right = delete (tree->right, tree->element);
}
else
{
if (tree->left == NULL)
{
tree = tree->right;
}
else if (tree->right == NULL)
{
tree = tree->left;
}
free(temp);
}
return tree;
}
int main()
{
node *tree = NULL;
int ch, x, n;
do
{
printf("\nEnter your choice: \n");
printf("1.Insert Elements\n2.Traverse (inorder)\n3.Delete Element\n4.Search\n5.Search Minimum\n6.Search Maximum\n7.Exit\n");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter number of elements: \n");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d", &x);
tree = insert(tree, x);
}
break;
case 2:
inorder(tree);
break;
case 3:
printf("Enter the element to be deleted: \n");
scanf("%d", &x);
delete (tree, x);
break;
case 4:
printf("Enter the element to search for: \n");
scanf("%d", &x);
if (search(tree, x) != NULL)
{
printf("Element Found!!\n");
}
else
{
printf("Element Not Found\n");
}
break;
case 5:
if (searchMin(tree) != NULL)
{
printf("%d is the minimum element\n", searchMin(tree)->element);
}
else
{
printf("Tree is empty\n");
}
break;
case 6:
searchMax(tree);
break;
case 7:
break;
default:
printf("Invalid Choice!\n");
break;
}
} while (ch != 7);
return 0;
}