forked from Haresh1204/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BST.c
215 lines (193 loc) · 4.03 KB
/
BST.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
214
215
#include<stdio.h>
#include<stdlib.h>
struct Node{
int data;
struct Node *left,*right,*parent;
};
struct Node* search(struct Node *root,int val)
{
struct Node *ptr=root;
while(ptr != NULL)
{
if(ptr->data <val)
{
if(ptr->data==val || ptr==NULL)
{
return ptr;
}
else
{
ptr=ptr->right;
}
}
else
{
if(ptr->data==val || ptr==NULL)
{
return ptr;
}
else
{
ptr=ptr->left;
}
}
}
}
struct Node* root=NULL;
void insert(int key){
struct Node*t=root;
struct Node*r,*p;
if(root==NULL){
p=(struct Node*)malloc(sizeof(struct Node));
p->data=key;
p->left=p->right=NULL;
root=p;
return;
}
while(t!=NULL){
r=t;
if(key<t->data){
t=t->left;
}
else if (key>t->data)
{
t=t->right;
}
else
return;
}
p=(struct Node*)malloc(sizeof(struct Node));
p->data=key;
p->left=p->right=NULL;
if(key<r->data) r->left=p;
else r->right=p;
}
// struct Node* insertBST(struct Node*root, int val){
// if(root == NULL){
// root->data=val;
// }
// if (val<root->data){
// root->left = insertBST(root->left,val);
// }
// else{
// root->right = insertBST(root->right,val);
// }
// return root;
// }
void inorder(struct Node * root){
if(root == NULL){
return;
}
inorder(root->left);
printf("%d🫡 ",root->data);
inorder(root->right);
}
struct Node* min(struct Node* root)
{
struct Node* ptr2;
ptr2=root;
while(ptr2->left != NULL)
{
ptr2=ptr2->left;
}
return ptr2;
}
struct Node* max(struct Node* root)
{
struct Node* ptr2;
ptr2=root;
while(ptr2->right != NULL)
{
ptr2=ptr2->right;
}
return ptr2;
}
struct Node* successor(struct Node *root)
{
printf("\nenter the value whose successor has to be find:");
int num;
scanf("%d",&num);
struct Node* find,*pa;
find=search(root,num);
if(find->right != NULL)
{
return min(find->right);
}
pa=find->parent;
while(pa != NULL && find==pa->right)
{
find=pa;
pa=pa->parent;
}
return pa;
}
struct Node* predecessor(struct Node* root)
{
printf("\nenter the value whose predecessor has to be found:");
int x;
scanf("%d",&x);
struct Node* find,*pa;
find=search(root,x);
if(find->left != NULL)
{
return max(find->left);
}
pa=find->parent;
while(pa!= NULL && pa->left==find)
{
find=pa;
pa=pa->parent;
}
return pa;
}
int Height( struct Node *p) {
int x;
int y;
if (p == NULL){
return 0;
}
x = Height(p->left);
y = Height(p->right);
return x > y ? x + 1 : y + 1;
}
struct Node*Delete(struct Node *p, int key) {
struct Node* q;
if (p == NULL){
return NULL;
}
if (p->left == NULL && p->right== NULL){
p = NULL;
free(p);
return NULL;
}
if (key < p->data){
p->left = Delete(p->left, key);
} else if (key > p->data){
p->right= Delete(p->right, key);
} else {
if (Height(p->left) > Height(p->right)){
q = predecessor(p->left);
p->data = q->data;
p->left = Delete(p->left, q->data);
} else {
q = successor(p->right);
p->data = q->data;
p->right = Delete(p->right, q->data);
}
}
return p;
}
int main(){
int n;
int size;
printf("Enter the number of values you wanna add:");
scanf("%d",&size);
printf("Enter the numbers to be instered:");
for(int i=0;i<size;i++){
scanf("%d",&n);
insert(n);
}
inorder(root);
root = min(root);
printf("%d",root->data);
}