Repository navigation
Expand file tree
/
Copy pathtrees.c
More file actions
420 lines (375 loc) · 8.76 KB
/
Copy pathtrees.c
File metadata and controls
420 lines (375 loc) · 8.76 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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
* implementazione strutture di dati ad albero
*/
/*
* errori policy:
* WIP
* */
#include <stdlib.h>
#include <stdio.h>
#include "trees.h"
//#include <stddef.h
/*
* BST implemented with struct:
*
* typedef struct node{
* int key;
* int value;
* struct node *left;
* struct node *right;
* struct node *parent;
* }node;
*
* Has the propriety of mantaining all minor keys on left and all magior keys on right
* for each node; value is the payload associated to the key
*
* n is the number of nodes that the tree has
* H is the height of the tree and is calculated as the furthest branch from root
* -> Log n <= H <= n
*
* Costs:
* - research: O(H)
* - insert: O(H) i find the leaf and than insert in O(1) ~ O(H)
* - find min or max: O(H) cos in worst case the tree is ordered so ive to go through the whole
* - find the successor: O(H)
* - remoove a node: 3 cases
* 1) no child -> O(1)
* 2) single child -> O(1)
* 3) has 2 child -> we need to find the successor O(H) and do a replacment O(1) ~ O(H)
* - visits (O(n)):
* 1) pre order ~ node -> left -> right
* 2) in order ~ left -> node -> right
* 3) post order ~ left -> right -> node
* rotation XP wip
*/
/* funzioni helper*/
static node *find_root(node *n);
static int rm_leaf(node * leaf);
static void post_order_free(node *root);
static void post_order(node *root);
static void pre_order(node *root);
static void in_order(node *root);
static node *bst_recursive_search (node *leaf, int key);
static int recursive_size_count(node *leaf);
/* alloc first node and return it, might return null if allocations problems*/
node * mkbst(int key, int value){
node *root = malloc(sizeof(node));
if (!root){
perror("error: malloc failed in the BST root allocation");
return NULL;
}
root->key = key;
root->value = value;
root->left = NULL;
root->right = NULL;
root->parent = NULL;
return root;
}
/*operationo of insertion in a bst
* if insert a already saved key than overwrite the value and return 1
* */
int bst_insert(node *parent, int key, int value){
if (parent == NULL){
fprintf(stderr, "error: node passed is null\n");
return -2;
}
if (parent->key == key){
parent->value = value;
return 1;
}
if (parent->left == NULL && parent->key > key ){
node *leaf = malloc(sizeof(node));
if (!leaf){
perror ("error: malloc failed to allocate BTS leaf allocation in insertion");
return -1;
}
leaf->key = key;
leaf->value = value;
leaf->left = NULL;
leaf->right = NULL;
leaf->parent = parent;
parent->left = leaf;
return 0;
}
if (parent->right == NULL && parent->key < key ){
node *leaf = malloc(sizeof(node));
if (!leaf){
perror ("error: malloc failed to allocate BTS leaf allocation in insertion");
return -1;
}
leaf->key = key;
leaf->value = value;
leaf->left = NULL;
leaf->right = NULL;
leaf->parent = parent;
parent->right = leaf;
return 0;
}
if(parent->key > key ){
return bst_insert(parent->left, key, value);
}
else{
return bst_insert(parent->right, key, value);
}
}
/* tool: removes a leaf if and only if it doesn't hav children */
int rm_leaf(node * leaf){
if (leaf == NULL)
return -1;
if (leaf->left || leaf->right){
return -1;
}
if (leaf->parent == NULL){
free(leaf);
return 0;
}
if (leaf->parent->left == leaf)
leaf->parent->left = NULL;
else
leaf->parent->right = NULL;
free(leaf);
return 0;
}
/*
* i need to ask to the bst to drop the node:
* drop leaf moves the leaf down until it doesn't have any more children and than
* calls the helper rm_leaf
* :) happy refactor
*/
node *bst_drop_leaf(node *targhet){
if (!targhet){
return NULL;
}
while (targhet->left || targhet->right){
if (targhet->right)
L_rotation(targhet);
else
R_rotation(targhet);
}
if(!targhet->parent && !targhet->left && !targhet->right){
if (rm_leaf(targhet) != 0){
fprintf(stderr, "error: rm_leaf failed");
}
return NULL;
}
node *new_root = find_root(targhet);
if (rm_leaf(targhet) != 0){
fprintf(stderr, "error: rm_leaf failed");
}
return new_root;
}
/*trova il sucessore overo il minimo figlio destro,
* nel caso in cui non ci siano figli destri prende
* il primo padre che ha il nodo come sinistro */
node * find_successor(node * leaf){
if (leaf == NULL)
return NULL;
node *succ;
if (leaf->right != NULL){
succ = leaf->right;
while(True){
if (succ->left != NULL)
succ = succ->left;
else
break;
}
return succ;
}
else{
succ = leaf->parent;
int buf = leaf->key;
while (True){
if (succ == NULL)
return NULL;
if (succ->key > buf)
break;
succ = succ->parent;
}
return succ;
}
}
/* risale al root dell'albero a cui appartiene n (NULL se n e' NULL) */
static node *find_root(node *n){
if (n == NULL)
return NULL;
while (n->parent != NULL)
n = n->parent;
return n;
}
/* libera l'intero albero in post-order (foglie -> root) */
int rm_bst_tree(node * leaf){
post_order_free(find_root(leaf));
return 0;
}
static void post_order_free(node *root){
if (!root)
return;
post_order_free(root->left);
post_order_free(root->right);
free(root);
}
int read_post_order(node * leaf){
post_order(find_root(leaf));
return 0;
}
static void post_order(node *root){
if (!root)
return;
post_order(root->left);
post_order(root->right);
printf("%d:%d ",root->key,root->value);
}
int read_pre_order(node * leaf){
pre_order(find_root(leaf));
return 0;
}
static void pre_order(node *root){
if (!root)
return;
printf("%d:%d ",root->key,root->value);
pre_order(root->left);
pre_order(root->right);
}
int read_in_order(node * leaf){
in_order(find_root(leaf));
return 0;
}
static void in_order(node *root){
if (!root)
return;
in_order(root->left);
printf("%d:%d ",root->key,root->value);
in_order(root->right);
}
// ---------------------------------------------------
/*
* min of a BST is naturally the last left chilf from root
*/
node *bst_min(node *leaf){
if (!leaf){
fprintf(stderr, "errore: nodo invalido");
return NULL;
}
node * root = find_root(leaf);
while (root->left != NULL){
root = root->left;
}
return root;
}
/*
* max of a BST is naturally the last right child from root
*/
node *bst_max(node *leaf){
if (!leaf){
fprintf(stderr, "errore: nodo invalido");
return NULL;
}
node * root = find_root(leaf);
while (root->right != NULL){
root = root->right;
}
return root;
}
/*finds the size recurring on the whole ree adding 1 for each node it passes*/
int bst_size(node *leaf){
if (!leaf){
return 0;
}
node *root = find_root(leaf);
return recursive_size_count(root);
}
static int recursive_size_count(node *leaf){
if(!leaf)
return 0;
return recursive_size_count(leaf->left) + recursive_size_count(leaf->right) + 1;
}
//-------------------------------------------------------
/*
* find root and than cakk the recursive research, retourns a node
*/
node *bst_search (node *leaf, int key){
if (!leaf){
fprintf(stderr, "errore: nodo invalido");
return NULL;
}
node *root = find_root(leaf);
return bst_recursive_search(root, key);
}
static node * bst_recursive_search (node *leaf, int key){
if(!leaf){
return NULL;
}
if(leaf->key == key){
return leaf;
}
if(leaf->key > key)
return bst_recursive_search(leaf->left, key);
else
return bst_recursive_search(leaf->right, key);
}
//----------------------------------------------------------
/*
* Rotations
*
* shitty code this make me feel so stupd this is 3 hours of work and 2 refactor :(
*/
int L_rotation (node *leaf){
if (!leaf){
fprintf(stderr, "errore: nodo invalido");
return -1;
}
if (!leaf->right){
fprintf(stderr, "error: node dont has a right son");
return -1;
}
node *y = leaf->right;
if(!leaf->parent){
y->parent = NULL;
}
else{
y->parent = leaf->parent;
if (leaf->parent->key > leaf->key){
leaf->parent->left = y;
}
else{
leaf->parent->right = y;
}
}
leaf->right = y->left;
if (y->left){
y->left->parent = leaf;
}
y->left = leaf;
leaf->parent = y;
return 0;
}
int R_rotation (node *leaf){
if (!leaf){
fprintf(stderr, "errore: nodo invalido");
return -1;
}
if (!leaf->left){
fprintf(stderr, "error: node dont has a left son");
return -1;
}
node *y = leaf->left;
if(!leaf->parent){
y->parent = NULL;
}
else{
y->parent = leaf->parent;
if (leaf->parent->key > leaf->key){
leaf->parent->left = y;
}
else{
leaf->parent->right = y;
}
}
leaf->left= y->right;
if (y->right){
y->right->parent = leaf;
}
y->right = leaf;
leaf->parent = y;
return 0;
}