-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinarySearchTree.c
163 lines (149 loc) · 3.55 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
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *left, *right;
} ;
typedef struct node *BSTNODE;
BSTNODE newNodeInBST(int item) {
BSTNODE temp = (BSTNODE)malloc(sizeof(struct node));
temp->data = item;
temp->left = temp->right = NULL;
return temp;
}
void inorderInBST(BSTNODE root) {
if (root != NULL)
{
inorderInBST(root->left);
printf("%d ", root->data);
inorderInBST(root->right);
}
}
void preorderInBST(BSTNODE root) {
if(root != NULL){
printf("%d ", root->data);
preorderInBST(root->left);
preorderInBST(root->right);
}
}
void postorderInBST(BSTNODE root) {
if(root != NULL){
postorderInBST(root->left);
postorderInBST(root->right);
printf("%d ", root->data);
}
}
BSTNODE insertNodeInBST(BSTNODE node, int ele) {
if(node == NULL){
printf("Successfully inserted,\n");
return newNodeInBST(ele);
}
else{
if(ele <= node->data){
node->left = insertNodeInBST(node->left, ele);
}
else if(ele > node->data){
node->right = insertNodeInBST(node->right, ele);
}
else
printf("Element already exists in BST");
}
return node;
}
BSTNODE deleteNodeInBST(BSTNODE root, int ele){
if(root = NULL){
printf("Cannot find %d in the binary search tree.\n",ele);
return root;
}
if(ele < root->data){
root->left = deleteNodeInBST(root->left, ele);
}
else if(ele > root->data){
root->right = deleteNodeInBST(root->right, ele);
}
else if(ele == root->data){
if(root->left == NULL){
BSTNODE temp = root->right;
printf("Deleted %d from binary search tree.\n", ele);
free(root);
return temp;
}
else if(root->right == NULL){
BSTNODE temp = root->left;
printf("Deleted %d from binary search tree.\n", ele);
free(root);
return temp;
}
BSTNODE temp = root->left;
root = temp->data;
temp->data = ele;
root->right = deleteNodeInBST(root->right, ele);
}
return root;
}
BSTNODE searchNodeInBST(BSTNODE root, int ele){
if(root == NULL || root->data == ele)
return root;
else if(ele > root->data)
return searchNodeInBST(root->right, ele);
else
return searchNodeInBST(root->left, ele);
}
int main(){
int x, op;
BSTNODE root = NULL;
while(1) {
printf("1.Insert 2.Inorder Traversal 3.Preorder Traversal 4.Postorder Traversal 5.Delete a Node 6.Search 9.Exit\n");
printf("Enter your option :");
scanf("%d", &op);
switch(op) {
case 1: printf("Enter an element to be inserted : ");
scanf("%d", &x);
root = insertNodeInBST(root,x);
break;
case 2:
if(root == NULL)
printf("Binary Search Tree is empty.\n");
else {
printf("Elements of the BST (in-order traversal): ");
inorderInBST(root);
printf("\n");
}
break;
case 3:
if(root == NULL)
printf("Binary Search Tree is empty.\n");
else {
printf("Elements of the BST (pre-order traversal): ");
preorderInBST(root);
printf("\n");
}
break;
case 4:
if(root == NULL)
printf("Binary Search Tree is empty.\n");
else {
printf("Elements of the BST (post-order traversal): ");
postorderInBST(root);
printf("\n");
}
break;
case 5:
printf("Enter an element to be deleted : ");
scanf("%d",&x);
root = deleteNodeInBST(root,x);
break;
case 6:
printf("Enter an element to be searched : ");
scanf("%d",&x);
if(searchNodeInBST(root, x) == NULL)
printf("Element not found in the binary search tree.\n");
else
printf("Element found in the binary search tree.\n");
break;
case 9:
exit(0);
}
}
return 0;
}