-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbstfun.h
351 lines (286 loc) · 6.12 KB
/
bstfun.h
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
struct node //Defining the node structure
{
int key;
struct node*parent;
struct node*right;
struct node*left;
};
void insert(struct node**head,int key11);
void printtreepre(struct node*currentno);
void printtreein(struct node*currentno);
void printtreepost(struct node*currentno);
void printpre(struct node**head);
void printin(struct node**head);
void printin(struct node**head);
struct node *deletetree(struct node*head,int key1);
struct node *find(struct node*head,int key1);
void imbalance(struct node*head,int key1);
struct node *inserttree(struct node*fipa,int key1)//Finding the node to which new node will be attached
{
struct node*prev=fipa;
if(key1>fipa->key)
fipa=fipa->right;
else
fipa=fipa->left;
if(fipa==NULL)
return prev;
else
inserttree(fipa,key1);
}
void insert(struct node**head,int key11)//To insert a node with a given key in a given tree
{
struct node*temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->key=key11;
temp->left=NULL;
temp->right=NULL;
temp->parent=NULL;
if(*head==NULL)
*head=temp;
else
{
struct node*temp1=*head;
struct node*temp2=inserttree(temp1,key11);
if(key11>temp2->key)
{
temp2->right=temp;//Attaching the new node
temp->parent=temp2;
}
else
{
temp2->left=temp;
temp->parent=temp2;
}
}
printf("True");
}
//Preorder-first printing the parent and then its children.
void printtreepre(struct node*currentno)//To print the sub-tree with a given node in preorder
{
if(currentno==NULL)
return;
else
{
printf("%d ",currentno->key);
struct node*templ=currentno;
struct node*tempr=currentno;
templ=templ->left;
tempr=tempr->right;
if(tempr!=NULL&&templ!=NULL)
{
printtreepre(templ);
printtreepre(tempr);
}
else if(tempr!=NULL&&templ==NULL)
printtreepre(tempr);
else if(tempr==NULL&&templ!=NULL)
printtreepre(templ);
else
return;
}
}
//Inorder-first printing the left child,then the parent and then the right child
void printtreein(struct node*currentno)//To print the sub-tree with a given node in inorder
{
if(currentno==NULL)
return;
else
{
struct node*templ=currentno->left;
struct node*tempr=currentno->right;
if(tempr!=NULL&&templ!=NULL)
{
printtreein(templ);
printf("%d ",currentno->key);
printtreein(tempr);
}
else if(tempr!=NULL&&templ==NULL)
{
printf("%d ",currentno->key);
printtreein(tempr);
}
else if(tempr==NULL&&templ!=NULL)
{
printtreein(templ);
printf("%d ",currentno->key);
}
else
printf("%d ",currentno->key);
}
}
//Postorder-first printing the childrena and then the parent
void printtreepost(struct node*currentno)//To print the sub-tree with a given node in postorder
{
if(currentno==NULL)
return;
else
{
struct node*templ=currentno->left;
struct node*tempr=currentno->right;
if(tempr!=NULL&&templ!=NULL)
{
printtreepost(templ);
printtreepost(tempr);
printf("%d ",currentno->key);
}
else if(tempr!=NULL&&templ==NULL)
{
printtreepost(tempr);
printf("%d ",currentno->key);
}
else if(tempr==NULL&&templ!=NULL)
{
printtreepost(templ);
printf("%d ",currentno->key);
}
else
printf("%d ",currentno->key);
}
}
//To print the whole tree in preorder,inorder,postorder
void printpre(struct node**head)
{
printtreepre(*head);
}
void printin(struct node**head)
{
printtreein(*head);
}
void printpost(struct node**head)
{
printtreepost(*head);
}
//To find node with the least key in a sub-tree with a given node as the head
struct node *minkey(struct node*temp)
{
struct node*present=temp;
while(present!=NULL&&present->left!=NULL)
present=present->left;
return present;
}
//To delete a node with a given key
struct node *deletetree(struct node*head,int key1)
{
if(head==NULL)
{
printf("False");
return head;
}
if(key1>head->key)//Continue to find the node
head->right=deletetree(head->right,key1);
else if(key1<head->key)
head->left=deletetree(head->left,key1);
else
{
//On finding the needed node
printf("True");
//If the node has atmost only one child
if(head->left==NULL)
{
struct node*temp1=head->right;
free(head);
return temp1;
}
else if(head->right==NULL)
{
struct node*temp1=head->left;
return temp1;
free(head);
}
//If the node has two children
else
{
struct node*temp1=minkey(head->right);//Replacing the deleted node with the least element in its right subtree
head->key=temp1->key;
head->right=deletetree(head->right,temp1->key);
}
return head;
}
}
int ri=0;
int le=0;
struct node *find(struct node*head,int key1)//To find the node with a given key and it depth
{
if(head==NULL)
{
printf("False");
return head;
}
if(key1>head->key)
{
ri++;
find(head->right,key1);
}
else if(key1<head->key)
{
le++;
find(head->left,key1);
}
else
{
printf("True,");
printf("depth = %d",ri+le);
return head;
}
}
struct node *find1(struct node*head,int key1)//To find the node with a given key and it depth
{
if(head==NULL)
return head;
if(key1>head->key)
find1(head->right,key1);
else if(key1<head->key)
find1(head->left,key1);
else
return head;
}
int ri2=0;
int le2=0;
void numnodes(struct node*currentno)//Returns the number of nodes attached to a particular node
{
if(currentno==NULL)
return;
else
{
struct node*temp1=currentno->left;
struct node*temp2=currentno->right;
numnodes(temp1);
if(temp1!=NULL)
ri2++;
numnodes(temp2);
if(temp2!=NULL)
le2++;
}
}
void imbalance(struct node*head,int key1)
{
ri=0;
le=0;
struct node*currentno=find1(head,key1);//Finding the required node
if(currentno==NULL)
{
printf("False");
return;
}
else
{
numnodes(currentno->left);//Number of nodes in its left sub-tree
int left=ri2+le2;
if(currentno->left!=NULL)
left=left+1;
ri2=0;
le2=0;
numnodes(currentno->right);//Number of nodes in its right subtree
int right=ri2+le2;
if(currentno->right!=NULL)
right=right+1;
if(left>right)
printf("Imbalance: %d",left-right);
else
printf("Imbalance: %d",right-left);
}
ri2=0;
le2=0;
ri=0;
le=0;
}