-
Notifications
You must be signed in to change notification settings - Fork 111
/
bstfull.c
227 lines (187 loc) · 4.11 KB
/
bstfull.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
216
217
218
219
220
221
222
223
224
225
226
227
#include<stdio.h>
#include<stdlib.h>
struct node{
int val;
int leftsub;
int rightsub;
int size;
struct node *left;
struct node *right;
struct node *parent;
};
struct node *parent;
struct node *newnode(int val){
struct node *temp=(struct node*)malloc(sizeof(struct node));
temp->left=temp->right=NULL;
temp->val=val;
return temp;
}
void inorder(struct node *root){
if(root!=NULL){
inorder(root->left);
printf("%d ", root->val);
inorder(root->right);
}
}
void printPostorder(struct node* root)
{
if (root == NULL)
return;
printPostorder(root->left);
printPostorder(root->right);
printf("%d ",root->val);
}
void pre(struct node* root)
{
if (root == NULL)
return;
printf("%d ",root->val);
pre(root->left);
// then recur on right subtree
pre(root->right);
// now deal with the node
}
struct node *insert(struct node *root, int val){
//printf("inseting")
if(root==NULL)
{
struct node *temp=newnode(val);
temp->size=1; temp->rightsub=0; temp->leftsub=0;
return temp;
}
if(val>root->val)
{
root->rightsub+=1;
root->right=insert(root->right,val);
}
else if(val<root->val)
{
root->leftsub+=1;
root->left=insert(root->left,val);
}
return root;
}
struct node *kthmin (struct node *root, int k){
printf("k=%d root->leftsub %d root->rightsub %d root->size %d\n",k,root->leftsub, root->rightsub, root->size);
if(root!=NULL){
if(root->leftsub>=k)
return kthmin(root->left,k);
else if(k-root->leftsub==1)
{printf("min= %d \n", root->val); return(root);}
else
return kthmin(root->right,k-root->leftsub-1);
}
}
struct node *del(struct node *root, int val){
if(root==NULL) return root;
if(val<root->val)
root->left=del(root->left,val);
else if(val>root->val)
root->right=del(root->right, val);
else
{
if(root->right==NULL)
{
struct node *temp=root->left;
free(root);
return temp;
}
else if(root->left==NULL)
{
struct node *temp=root->right;
free(root);
return temp;
}
struct node *temp=root->right;
while(temp->left!=NULL)
temp=temp->left;
root->val=temp->val;
root->right=del(root->right,temp->val);
}
return root;
}
struct node *suc(struct node *root, int val){
if(val<root->val) {parent=root; root->left=suc(root->left,val);}
else if(val>root->val){parent=root; root->right=suc(root->right,val);}
else{
struct node *temp=root->right;
if(root->right!=NULL)
{
while(temp->left!=NULL)
temp=temp->left;
printf("temp->val %d\n",temp->val);
return temp;
}
else{
if(root->right!=NULL){
while(parent->left->val!=root->val){
root=parent;
}
printf("parent->val %d\n",parent->val);
return parent;
}
}
}
}
int main(void){
struct node *root=NULL;
struct node *temp=NULL;
int a[10]={5,4,6,11,10,15,12,20},op;
for(int i=0;i<8;i++)
{
if(i==0)
{
root=insert(root,a[i]);
}
else
{
temp=insert(root,a[i]);
}
}
printf("Existing BST: \n");
inorder(root);
printf("\n");
printf("\nOPTIONS:\n1.Insert\n2.Kthmin\n3.Delete\n4.Inorder\n5.Postorder\n6.Preorder\n7.Successor\n\n");
scanf("%d",&op);
do{
if(op==1)
{
int n; printf("Enter n: "); scanf("%d", &n);
temp=insert(root,n);
}
else if(op==2)
{
int k; printf("Enter k : "); scanf("%d",&k);
temp=kthmin(root,k);
printf("Kth min is: %d\n",temp->val);
}
else if(op==3)
{
int n; printf("Enter n: "); scanf("%d", &n);
temp=del(root,n);
}
else if(op==4)
{
inorder(root);
printf("\n");
}
else if(op==5)
{
printPostorder(root);
printf("\n");
}
else if(op==6)
{
pre(root);
printf("\n");
}
else if(op==7)
{
struct node *temp2=NULL;
int n; printf("Enter n: "); scanf("%d", &n);
temp2=suc(root,n);
printf("Successor is: %d \n",temp2->val);
}
printf("\nOption? "); scanf("%d",&op);
}while(op!=0);
}