-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarysearch tree 10.txt
162 lines (147 loc) · 3.39 KB
/
binarysearch tree 10.txt
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
nclude<stdio.h>
#include<stdlib.h>
struct BSTNode
{
int data;
struct BSTNode *left;
struct BSTNode *right;
};
typedef struct BSTNode node;
int flag=0;
node *root = NULL;
node* createBST(node*, int);
node* insertBST(node *root,int value);
void displayBST(node *);
node* deleteBST(node* root ,int val );
int findMinRST(node *);
node* searchBST(node *root,int key);
int main()
{
int ch, val;
node *temp;
printf("\nBinary Search Tree Operations\n");
while(1){
scanf("%d",&ch);
switch(ch)
{
case 1:
scanf("%d", &val);
root = insertBST(root,val);
break;
case 2:
scanf("%d",&val);
root=deleteBST(root,val);
if(flag==1)
printf("\nCannot delete - %d is not found or BST empty",val);
else
printf("\n%d is found and deleted from BST",val);
break;
case 3: if(root==NULL)
printf("\nBST EMPTY");
else
{
printf("\nINORDER TRAVERSAL OF BST: ");
displayBST(root);
}
break;
case 4:
scanf("%d",&val);
temp=searchBST(root,val);
if(temp == NULL)
printf("\nSearch Unsuccessful %d is not found in BST",val);
else
printf("\nSearch Successful %d is found in BST",val);
break;
case 5:exit(0);
default: printf("\nInvalid Choice");
}
}
}
node* createBST(node *root,int value)
{
node *newNode;
newNode = (node *)malloc(sizeof(node));
newNode->data = value;
newNode->left = newNode->right = NULL;
return(newNode);
}
node* insertBST(node *root,int value)
{
if(root == NULL)
root=createBST(root,value);
else if(value<root->data)
root->left = insertBST(root->left,value);
else
root->right = insertBST(root->right,value);
return root;
}
void displayBST(node *root)
{
if(root != NULL)
{
displayBST(root->left);
printf("%d ",root->data);
displayBST(root->right);
}
}
node *deleteBST(node *root, int val)
{
node* temp;
if(root == NULL)
{
flag=1;
return NULL;
}
if(val > root->data)
root->right = deleteBST(root->right,val);
else if(val < root->data)
root->left = deleteBST(root->left,val);
else
{
flag=0;
if(root->left == NULL && root->right == NULL)
{
temp=root;
free(temp);
root=NULL;
}
else if(root->left == NULL)
{
temp=root;
root=root->right;
free(temp);
}
else if(root->right == NULL)
{
temp = root;
root=root->left;
free(temp);
}
else
{
int rightMin = findMinRST(root->right);
root->data = rightMin;
root->right = deleteBST(root->right,rightMin);
}
}
return root;
}
int findMinRST(node *temp)
{
while(temp->left != NULL)
{
temp = temp->left;
}
return temp->data;
}
node *searchBST(node *root, int val)
{
if(root == NULL)
return NULL;
if(root->data==val)
return root;
if(val > root->data)
return(searchBST(root->right,val));
else if(val < root->data)
return(searchBST(root->left,val));
}