-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBST.c
More file actions
269 lines (214 loc) · 5.08 KB
/
BST.c
File metadata and controls
269 lines (214 loc) · 5.08 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *left;
struct node *right;
}*root=NULL;
struct node* insert(struct node* root,int data)
{
struct node* newNode = (struct node *) malloc(sizeof(struct node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
if(root == NULL)
root = newNode;
else if(data > root->data)
root->right = insert(root->right,data);
else if(data < root->data)
root->left = insert(root->left,data);
return root;
}
struct node* findNode(struct node* root, int data)
{
if(data == root->data)
return root;
else if(data > root->data)
return findNode(root->right,data);
else if(data < root->data)
return findNode(root->left,data);
}
int findMin(struct node* root)
{
if(root->left == NULL)
{printf(" min is %d\n",root->data );
return root->data;}
else
return findMin(root->left);
}
int findMax(struct node* root)
{
if(root->right == NULL)
{
return root->data;}
else
return findMax(root->right);
}
int GetSuccessor(int data)
{
// printf("entered sus\n");
struct node* current = findNode(root,data);
//printf("\n %d find node -> ",current->data);
//printf("entered sus2\n");
if(current == NULL)
return -999; // no successsor
if(current->right != NULL)
return findMin(current->right);
else
{
struct node* ancestor = root;
struct node* successsor = NULL;
while(current != ancestor)
{
if(current->data < ancestor->data)
{
successsor = ancestor;
ancestor = ancestor->left;
}
else
{
ancestor = ancestor->right;
}
}
printf(" suss final %d\n",successsor->data );
return successsor->data;
}
}
int GetPredecessor(int data)
{
struct node* current = findNode(root,data);
//printf("\n %d find node -> ",current->data);
//printf("entered sus2\n");
if(current == NULL)
return -999; // no successsor
if(current->left != NULL)
return findMax(current->left);
else
{
struct node* ancestor = root;
struct node* successsor = NULL;
while(current != ancestor)
{
if(current->data > ancestor->data)
{
successsor = ancestor;
ancestor = ancestor->right;
}
else
{
ancestor = ancestor->left;
}
}
printf(" suss final %d\n",successsor->data );
return successsor->data;
}
}
void inorder(struct node* root)
{
if(root == NULL)
return ;
inorder(root->left);
printf(" --> %d\n",root->data );
inorder(root->right);
}
struct node* deleteNode(struct node* root, int key)
{
// base case
if (root == NULL) return root;
// If the key to be deleted is smaller than the root's key,
// then it lies in left subtree
if (key < root->data)
root->left = deleteNode(root->left, key);
// If the key to be deleted is greater than the root's key,
// then it lies in right subtree
else if (key > root->data)
root->right = deleteNode(root->right, key);
// if key is same as root's key, then This is the node
// to be deleted
else
{
// node with only one child or no child
if (root->left == NULL)
{
struct node *temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL)
{
struct node *temp = root->left;
free(root);
return temp;
}
// node with two children: Get the inorder successor (smallest
// in the right subtree)
struct node* temp = findNode(root,findMin(root->right));
// Copy the inorder successor's content to this node
root->data = temp->data;
// Delete the inorder successor
root->right = deleteNode(root->right, temp->data);
}
return root;
}
int findHeight(struct node* root)
{
if(root == NULL)
return -1;
else
{
int left = findHeight(root->left);
int right = findHeight(root->right);
int height = (left>=right)?left:right;
return height+1;
}
}
void main()
{
char ch = 'i';
int data;
printf("\nMenu \n \n press \n i to insert \n d to display \n r to remove \n p to get Predecessor \n s to get successor \n q to quit\n\n");
while(ch != 'q'){
printf("\nCommand : "); scanf(" %c",&ch);
switch(ch)
{
case 'i' :
printf("\nEnter the element to be inserted :");
scanf(" %d",&data);
root = insert(root,data);
break;
case 'd' :
printf("\n\n\n");
inorder(root);
break;
case 's':
printf("\nSuccessor of what ??? : ");
scanf(" %d",&data);
int d = GetSuccessor(data);
printf("\n The successsor is %d" , d);
break;
case 'p':
printf("\n Predecessor of what ??? : ");
scanf(" %d",&data);
d = GetPredecessor(data);
printf("\n The Predecessor is %d" , d);
break;
case 'r':
printf("\n what do you want to delete: ");
scanf(" %d",&data);
deleteNode(root,data);
break;
case 'h':
printf("\n Height of which element ");
scanf(" %d",&data);
int h = findHeight(findNode(root,data));
printf("\n The height of this node is %d ",h);
break;
case 'q':
exit(0);
break;
default :
printf("\n invalid entry\n");
}
}
}