-
Notifications
You must be signed in to change notification settings - Fork 0
/
BST.c
144 lines (129 loc) · 2.85 KB
/
BST.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
//requires stdlib.h
typedef struct __BST{
int v; // value. replace with required data type and modify CoMParisions
int h, c, w; // height, count, weight
struct __BST *l, *r;
}BST;
void BST_insert(BST **, int);
void BST_update(BST **);
int BST_geth(BST *);
int BST_getw(BST *);
void BST_fixit(BST **);
void BST_liftr(BST **);
void BST_liftl(BST **);
BST * BST_search(BST *, int);
void BST_delete(BST **, int);
int BST_lessthan(BST *, int);
void BST_insert(BST **tarp, int v){
BST *t = *tarp;
if(t == NULL){
t=(BST *)malloc(sizeof(BST));
t->v = v;
t->h = 0;
t->l = t->r = NULL;
t->c = 1;
t->w = 1;
*tarp=t;
return ;
}
if(t->v == v){ // cmp
t->c++;
}
else if(t->v > v){ //cmp
BST_insert(&(t->l),v);
}
else{
BST_insert(&(t->r),v);
}
BST_update(tarp);
}
void BST_update(BST **tarp){
if(t == NULL)return ;
BST *t = *tarp;
int lh, rh; // left&right height
t->w = BST_getw(t->l) + BST_getw(t->r) + t->c;
lh = BST_geth(t->l);
rh = BST_geth(t->r);
int mnh, mxh; // min&max of lh,rh
mnh = lh < rh ? lh : rh;
mxh = mnh^lh^rh;
t->h = mxh + 1;
if(mxh > mnh + 1) BST_fixit(tarp);
}
int BST_getw(BST *t){
if(t == NULL) return 0;
return t->w;
}
int BST_geth(BST *t){
if(t == NULL) return -1;
return t->h;
}
void BST_fixit(BST **tarp){
BST *t = *tarp;
if(BST_geth(t->l) > BST_geth(t->r)){
if(BST_geth(t->l->l) > BST_geth(t->l->r)){
BST_liftl(tarp);
BST_update(&((*tarp)->r));
BST_update(tarp);
}
else{
BST_liftr(&(target->left));
BST_liftl(tarp);
target=*tarp;
BST_update(&(target->left));
BST_update(&(target->right));
BST_update(tarp);
}
}
else{
if(BST_geth(target->right->right)>BST_geth(target->right->left)){
BST_liftr(tarp);
BST_update(&((*tarp)->left));
BST_update(tarp);
}
else{
BST_liftl(&(target->right));
BST_liftr(tarp);
target=*tarp;
BST_update(&(target->right));
BST_update(&(target->left));
BST_update(tarp);
}
}
}
void BST_liftr(BST **tarp){
BST *r, *t;
t = *tarp;r = t->r;
*tarp = r;
t->r = r->l;
r->l = t;
}
void BST_liftl(BST **tarp){
BST *l, *t;
t = *tarp;l = t->l;
*tarp = l;
t->l = l->r;
l->r = t;
}
BST * BST_search(BST *t, int v){
if(t == NULL) return NULL;
if(t->v == v) return t; // cmp
else if(t->v > v) return BST_search(t->l, v); // cmp
else return BST_search(t->r, v);
}
void BST_delete(BST *t,int v){
if(t == NULL) return ;
if(t->v == v){
if(t->c > 0){
target->c--;
}
}
else if(t->v > v) BST_delete(t->l, v);
else BST_delete(t->r, v);
BST_update(&t); // only w will be updated. no rotations
}
int BST_lessthan(BST *t, int v){
if(t == NULL) return 0;
if(t->v >= v) return BST_lessthan(t->l, v); // cmp
else return BST_lessthan(t->r, v) + t->c + BST_getw(t->l); // cmp
}