-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzad11jc.cpp
83 lines (74 loc) · 1.92 KB
/
zad11jc.cpp
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
#include<stdio.h>
#include<stdlib.h>
typedef struct node *bin_tree;
struct node {
int val;
bin_tree left, right;
};
bin_tree newNode(int val){
bin_tree t = (node*)malloc(sizeof(node));
t->val = val;
t->left = t->right = NULL;
return t;
}
void insert(bin_tree el, bin_tree *t){ //szuka miejsca w slabszym BST
if(!*t) *t = el;
else{
bin_tree *k = t;
while(*k){
if(el->val <= (*k)->val){
k = &((*k)->left);
} else {
k = &((*k)->right);
}
}
*k = el;
}
}
void _bestify(bin_tree t){
if(!t) return;
//tylko lwey za duzy
if(t->left && t->left->val > t->val && !t->right){
t->right = t->left;
t->left = NULL;
} else if(t->right && t->right->val < t->val && !t->left){
t->left = t->right;
t->right = NULL;
} else if(t->left && t->right){
if(t->left->val > t->val && t->right->val < t->val){
bin_tree temp = t->right;
t->right = t->left;
t->left = temp;
} else if(t->left->val >= t->val && t->right->val >= t->val){
insert(t->left, &(t->right));
t->left = NULL;
} else if(t->left->val <= t->val && t->right->val <= t->val){
insert(t->right, &(t->left));
t->left = NULL;
}
}
}
void bestify(bin_tree t){
if(t){
bestify(t->left);
bestify(t->right);
_bestify(t);
}
}
int main(){
bin_tree t = newNode(1);
t->left = newNode(2);
t->left->left = newNode(4);
t->left->left->left = newNode(5);
t->right = newNode(3);
t->right->left = newNode(6);
t->right->right = newNode(7);
t->left->right = newNode(11);
t->right->left->right = newNode(12);
// bin_tree t = newNode(2);
// t->left = newNode(4);
// t->left->right = newNode(5);
// t->right = newNode(11);
bestify(t);
return 0;
}