-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbst.c
More file actions
261 lines (210 loc) · 5.05 KB
/
bst.c
File metadata and controls
261 lines (210 loc) · 5.05 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
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
/***
* Author : Pandhari
* **/
typedef struct node{
int data;
struct node *left;
struct node *right;
}bst;
//Insert node into bst
bst* insert_node(int adata){
bst *tree_node = (bst *)malloc(sizeof(bst));
tree_node->left = 0;
tree_node->right = 0;
tree_node->data = adata;
return (tree_node);
}
void make_bst(bst **root,int data){
bst *temp = *root;
if(*root == 0) {
bst* temp_node = insert_node(data);
*root = temp_node;
}else if(temp->data < data) { //bigger data always go to right
make_bst(&temp->right,data); //semantics &(temp->left)
}else {
make_bst(&temp->left,data);
}
}
void do_preorder(bst *root) {
if(root != 0) {
printf("%d\t",root->data);
do_preorder(root->left);
do_preorder(root->right);
}
}
void do_inorder(bst *root) {
if(root != 0) {
do_inorder(root->left);
printf("%d\t",root->data);
do_inorder(root->right);
}
}
int max(int i,int j) {
if(i > j) {
return i;
}else {
return j;
}
}
/**
* find max height of the bst
* Always give unit cost to each node
* 1.root 2.left 3.right
* **/
int findMaxHeight(bst *root) {
int rt = 0;
int lt = 0;
if(root == 0) {
return 1;
}else {
lt = findMaxHeight(root->left);
rt = findMaxHeight(root->right);
//The reason to add 1 is to just make sure that you have traversed from top
//and you are 1 height below from the top.
if(lt < rt) {
return rt+1;
}
return lt+1;
}
}
int is_balanced(bst *root) {
if(root == 0) return 0;
int lt = is_balanced(root->left);
int rt = is_balanced(root->right);
if (abs(lt - rt) > 1 || lt < 0 || rt < 0)
return 0;
return max(lt,rt) + 1; //add root weight = 1;
}
void getkth_max(bst *root,int k) {
if(root == 0 || k < 0) {
return ;
}
//printf ("\n%d\t",k);
static int index = 0; //to ensure between stack excution we get latest/updated value
getkth_max(root->left,k);
++index;
if( k == index) {
printf ("\nthe largest element = %d\n",root->data);
//return root->data;
}
getkth_max(root->right,k);
}
void getkth_max_1(bst *root,int* k) {
if(root == 0 || *k < 0) {
return 1;
}
getkth_max(root->left,*k);
-- (*k);
if( *k == 0) {
printf ("\nthe largest element = %d\n",root->data);
return root->data;
}
getkth_max(root->right,*k);
}
//left sided doubling or one sided doubling
void double_tree(bst *root) {
bst *oldptr;
if(root == 0) return;
//NOTE : Here we do not return anything we utilize stack
//feature not to avoid communication
double_tree(root->left);
double_tree(root->right);
//store the in oldptr
oldptr = root->left;
//Update load new
root->left = insert_node(root->data);
//store old again for consistency purpose restore the old value
root->left->left = oldptr;
//we cannot use above tric in faltten as we know that we have to suplicate the strucutre only once but in flatten example we have to make count about how many levels/count we should maintain in order to append the data i.e. right pointer/old_ptr
}
void flatten(bst *root) {
if (!root) return;
bst* left = root->left;
bst* right = root->right;
if (left) {
root->right = left;
root->left = NULL;
bst* rightmost = left;
while(rightmost->right) {
rightmost = rightmost->right;
}
rightmost->right = right;
// point the right most to the original right child
}
flatten(root->right);
}
void flatten_bt(bst *root) {
bst *old_ptr;
bst *dummy_ptr;
if(0 == root) return;
//Verifed : order make no sense i.e you can use root->left or root->right 1st or 2nd
flatten_bt(root->left);
flatten_bt(root->right);
//flatten_bt(root->left);
//if(0 == root->left) return;
//else {
if(root->left) {
old_ptr = root->right;
//TODO : Do we have to do insert_node here
//NO NEED OF ABOVE
//root->right = insert_node(root->left->data);
root->right = (root->left);
dummy_ptr = root->right;
//To maintain consistency
while(dummy_ptr->right) {
dummy_ptr = dummy_ptr->right;
}
if (0 != old_ptr)
dummy_ptr->right = old_ptr;
//Zero out left part
root->left = 0;
}
}
//Use sorted array / linked list to compute balanced bst
//Complexity of recursion = # recusrion calls
//so runtime = O(logn)
bst* arr_convert_to_bst(int a[],int start,int end) {
if(start < end)
{
//use recursion (Top down approach)
int mid = start + (end - start)/2;
bst *root = insert_node(a[mid]);
root->left = arr_convert_to_bst(a,start,mid-1);
root->right = arr_convert_to_bst(a,mid+1,end);
return root;
} else {
return 0;
}
}
int main () {
int tree_bag[] = {23,12,4,89,34,8,0,67};
int i =0;
bst *root = 0;
for (;i<7;i++) {
make_bst(&root,tree_bag[i]);
}
printf ("Preorder : \t");
do_preorder(root);
printf("\n");
printf ("Inorder : \t");
do_inorder(root);
printf("\n");
int ht;
ht = findMaxHeight(root);
printf("the max height of the tree = %d\n",ht);
int no;
int k = 3;
getkth_max(root,k);
printf ("Inorder Before flattening: \t");
do_inorder(root);
printf("\n");
flatten_bt(root);
//flatten(root);
printf ("Inorder After flattening: \t");
do_inorder(root);
//do_inorder(t);
printf("\n");
}