-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathctree.c
More file actions
486 lines (457 loc) · 11.5 KB
/
Copy pathctree.c
File metadata and controls
486 lines (457 loc) · 11.5 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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#include "ctree.h"
#include "cmem.h"
static inline int __is_right( cnode_t * x){
return x->parent->childs[1] == x;
}
static void __left_rotate(cnode_t * x){
cnode_t * y = x->childs[1];
x->childs[1] = y->childs[0];
if(y->childs[0]){
y->childs[0]->parent = x;
}
y->parent = x->parent;
if (x->parent){
x->parent->childs[__is_right(x)] = y;
}
y->childs[0] = x;
x->parent = y;
}
static void __right_rotate(cnode_t * x){
cnode_t * y = x->childs[0];
x->childs[0] = y->childs[1];
if(y->childs[1]){
y->childs[1]->parent = x;
}
y->parent = x->parent;
if (x->parent){
x->parent->childs[__is_right(x)] = y;
}
y->childs[1] = x;
x->parent = y;
}
static void __splay(cnode_t * x){
while(x->parent != NULL){
if(x->parent->parent == NULL){
if (x == x->parent->childs[0]){
// zig rotation
__right_rotate(x->parent);
}else{
// zag rotation
__left_rotate(x->parent);
}
}else{
int p_is_right = __is_right(x->parent);
if(__is_right(x)){
if(p_is_right){
// zag-zag rotation
__left_rotate(x->parent->parent);
__left_rotate(x->parent);
}else{
// zig-zag rotation
__left_rotate(x->parent);
__right_rotate(x->parent);
}
}else{
if(p_is_right){
// zag-zig rotation
__right_rotate(x->parent);
__left_rotate(x->parent);
}else{
// zig-zig rotation
__right_rotate(x->parent->parent);
__right_rotate(x->parent);
}
}
}
}
}
void ctree_splay(cnode_t * n)
{
__splay(n);
}
cnode_t * _ctree_splay_add (cnode_t ** proot, ctree_compare_fn* comparator, cnode_t * n, int replace)
{
// insert node
cnode_t * x = *proot;
cnode_t * y = NULL;
int l;
while (x != NULL) {
if (x == n)
return n;
y = x;
l = comparator(y, n);
if (l == 0) { // equal.
__splay(x);
if (replace) {
*n = *x;
if(x->childs[1]){
x->childs[1]->parent = n;
}
if (x->childs[0]){
x->childs[0]->parent = n;
}
n->parent = NULL;
*proot = n;
}else{
*proot = x;
}
// return previous value
return x;
} else {
l = (l<0) ? 1 : 0;
}
x = x->childs[l];
}
// y is parent of x
n->parent = y;
n->childs[0] = n->childs[1] = NULL;
if(y){
y->childs[l] = n;
__splay(n);
}
*proot = n;
return n;
}
static cnode_t * __find(cnode_t * x, ctree_compare_fn* comparator, const void * w)
{
while(x) {
if(x == w)
break;
int l = comparator(x, w);
if(l == 0)
break;
l = (l<0) ? 1 : 0;
x = x->childs[l];
};
return x;
}
cnode_t * _ctree_splay_find (cnode_t ** proot, ctree_compare_fn* comparator, const void * w)
{
cnode_t * x;
x = __find(*proot, comparator, w);
if(x){
__splay(x);
*proot = x;
}
return x;
}
static inline cnode_t * __lastElement(cnode_t * x, int is_right)
{
while(x->childs[is_right]){
x = x->childs[is_right];
}
return x;
}
/*
#define __cmin(x) __lastElement(x, 0)
#define __cmax(x) __lastElement(x, 1)
*/
static cnode_t * __join(cnode_t * s, cnode_t * t)
{
s = __lastElement(s, 1);
__splay(s);
s->childs[1] = t;
t->parent = s;
return s;
}
static cnode_t * __join_childs(cnode_t * x)
{
cnode_t * o;
if(NULL == x->childs[1]){
if(NULL == x->childs[0]) return NULL;
o = x->childs[0];
x->childs[0] = NULL;
}else if(NULL == x->childs[0]){
o = x->childs[1];
x->childs[1] = NULL;
}else{
x->childs[0]->parent = NULL;
x->childs[1]->parent = NULL;
o = __join(x->childs[0], x->childs[1]);
x->childs[0] = x->childs[1] = NULL;
}
o->parent = NULL;
return o;
}
cnode_t * _ctree_splay_del (cnode_t ** proot, ctree_compare_fn* comparator, const void * w)
{
cnode_t * x = __find(*proot, comparator, w);
if(x) {
__splay(x);
*proot = __join_childs(x);
}
return x;
}
cnode_t * ctree_splay_del_node (cnode_t ** proot, cnode_t *x)
{
cnode_t * p = x->parent;
cnode_t * j = __join_childs(x);
if(p == NULL){
*proot = j;
}else {
if(j) j->parent = p;
if(p->childs[0] == x) p->childs[0] = j;
else p->childs[1] = j;
}
return x;
}
static int __inorder(cnode_t * x, int height, ctree_walk_fn * cb, void * const user)
{
int rc = 0;
if(x) {
int rc = cb(x, height, user);
if(rc >= 0){
int n = __inorder(x->childs[0], height + 1, cb, user);
if (n >=0) {
rc += n;
n = __inorder(x->childs[1], height + 1, cb, user);
if (n >=0) {
return rc + n;
}
}
rc = n;
}
}
return rc;
}
static int __preorder(cnode_t * x, int height, ctree_walk_fn * cb, void * const user)
{
int rc = 0;
if(x) {
int rc = __preorder(x->childs[0], height + 1, cb, user);
if(rc >= 0){
int n = cb(x, height, user);
if (n >=0) {
rc += n;
n = __preorder(x->childs[1], height + 1, cb, user);
if (n >=0) {
return rc + n;
}
}
rc = n;
}
}
return rc;
}
int _ctree_splay_walk_preorder(cnode_t * x, ctree_walk_fn * cb, void * const user)
{
return __preorder(x, 0, cb, user);
}
int _ctree_splay_walk_inorder(cnode_t * x, ctree_walk_fn * cb, void * const user)
{
return __inorder(x, 0, cb, user);
}
/*
inline static cnode_t * __swap( cnode_t ** p, cnode_t * n)
{
cnode_t * ret = *p;
*p = n;
return ret;
}
*/
#define node_swap(P, N) cmem_swap(P, N)
void _ctree_clean(cnode_t ** root, ctree_walk_fn * cb, void * const user)
{
cnode_t * x = *root;
int height = 0;
while(x){
if(x->childs[0]){
height ++;
x = node_swap(&x->childs[0], NULL);
}else if(x->childs[1]){
height ++;
x = node_swap(&x->childs[1], NULL);
}else{
cnode_t * n = x;
x = node_swap(&x->parent, NULL);
cb(n, height, user);
height --;
}
}
*root = NULL;
}
void _ctree_filter(cnode_t ** root, ctree_walk_fn * filter, ctree_walk_fn * free, void * const user)
{
cnode_t * x = *root;
int height = 0;
cnode_t *o = x;
while(x){
if(o == x){
if(x->childs[0]){
o = x = x->childs[0];
height++;
continue;
}
if(x->childs[1]){
o = x = x->childs[1];
height++;
continue;
}
}else{
if(o == x->childs[0]){
// back from left
if(x->childs[1]){
o = x = x->childs[1];
height++;
continue;
}
}
o = x;
}
// call cb
x = x->parent;
if(0 == filter(o, height, user)){
cnode_t * j = __join_childs(o);
if(j) j->parent = x;
if(x){
if(x->childs[0] == o)
x->childs[0] = j;
else
x->childs[1] = j;
}else{
*root = j;
}
if(free){
free(o, height, user);
}
o = NULL;
}
height--;
}
}
const cnode_t * _ctree_next_node_up(const cnode_t * l, const cnode_t ** px)
{
const cnode_t * x = *px;
if (x == NULL){
return NULL;
}
while (x) {
if (l == x){
if (x->childs[0]) {
l = x = x->childs[0];
continue;
}
if (x->childs[1]) {
l = x = x->childs[1];
continue;
}
}else{
if (l == x->childs[0]){
// back from left
if (x->childs[1]){
l = x = x->childs[1];
continue;
}
}
l = x;
}
*px = x->parent;
break;
}
return l;
}
void _ctree_filter_down (cnode_t ** root, ctree_walk_fn * filter, ctree_walk_fn * free, void * const user)
{
cnode_t * x = *root;
int height = 0;
cnode_t *o = x;
while(x){
if(o == x){
// check this node
int rc = filter(x, height, user);
if(rc > 0){
// keep node
// continue traverse
if(x->childs[0]){
// go down left
o = x = x->childs[0];
height++;
continue;
}
if(x->childs[1]){
// go down right
o = x = x->childs[1];
height++;
continue;
}
// no more childs
// go up but keep 'o' pointing to the node
}else{
//remove node or subtree
cnode_t * j = NULL;
if(rc == 0){
// remove node
j = __join_childs(o);
}
x = x->parent;
if(x){
x->childs[__is_right(o)] = j;
}else{
*root = j;
}
o->parent = NULL;
if(free){
if(rc==0){
free(o, height, user);
}else{
_ctree_clean(&o, free, user);
}
}
o = NULL;
continue;
}
}else{
if(o == x->childs[0]){
// back from left
if(x->childs[1]){
o = x = x->childs[1];
height++;
continue;
}
}
o = x;
}
x = x->parent;
height--;
}
}
const cnode_t * _ctree_next_node_down(const cnode_t * l, const cnode_t ** px)
{
// TODO: implement it
return _ctree_next_node_up(l, px);
}
const cnode_t * ctree_check_consistence(const cnode_t * root)
{
const cnode_t * x = root;
const cnode_t *o = x;
while(x){
if(o == x){
// continue traverse
if(x->childs[0]){
// go down left
o = x = x->childs[0];
continue;
}
if(x->childs[1]){
// go down right
o = x = x->childs[1];
continue;
}
// no more childs
// go up but keep 'o' pointing to the node
}else{
if(o == x->childs[0]){
// back from left
if(x->childs[1]){
o = x = x->childs[1];
continue;
}
}else if(o != x->childs[1]){
return o;
}
o = x;
}
x = x->parent;
}
return NULL;
}