7 #define CALLOC(type, num) ((type*)calloc(num, sizeof(type)))
11 KAVL_HEAD(struct my_node
) head
;
14 #define my_cmp(p, q) (((p)->key > (q)->key) - ((p)->key < (q)->key))
15 KAVL_INIT(my
, struct my_node
, head
, my_cmp
)
17 int check(struct my_node
*p
, int *hh
)
19 int c
= 1, h
[2] = {0, 0};
22 if (p
->head
.p
[0]) c
+= check(p
->head
.p
[0], &h
[0]);
23 if (p
->head
.p
[1]) c
+= check(p
->head
.p
[1], &h
[1]);
24 *hh
= (h
[0] > h
[1]? h
[0] : h
[1]) + 1;
25 if (h
[1] - h
[0] != (int)p
->head
.balance
)
26 fprintf(stderr
, "%d - %d != %d at %c\n", h
[1], h
[0], p
->head
.balance
, p
->key
);
27 if (c
!= (int)p
->head
.size
)
28 fprintf(stderr
, "%d != %d at %c\n", p
->head
.size
, c
, p
->key
);
33 int print_tree(const struct my_node *p)
37 if (p->head.p[0] || p->head.p[1]) {
39 if (p->head.p[0]) c += print_tree(p->head.p[0]);
42 if (p->head.p[1]) c += print_tree(p->head.p[1]);
50 void check_and_print(struct my_node *root)
58 void shuffle(int n
, char a
[])
61 for (i
= n
; i
> 1; --i
) {
63 j
= (int)(drand48() * i
);
64 tmp
= a
[j
]; a
[j
] = a
[i
-1]; a
[i
-1] = tmp
;
72 struct my_node
*root
= 0;
73 struct my_node
*p
, *q
, t
;
77 for (i
= 33, n
= 0; i
<= 126; ++i
)
78 if (i
!= '(' && i
!= ')' && i
!= '.' && i
!= ';')
81 for (i
= 0; i
< n
; ++i
) {
82 p
= CALLOC(struct my_node
, 1);
84 q
= kavl_insert(my
, &root
, p
, &cnt
);
89 for (i
= 0; i
< n
/2; ++i
) {
91 q
= kavl_erase(my
, &root
, &t
, 0);
96 kavl_itr_first(my
, root
, &itr
);
98 const struct my_node
*r
= kavl_at(&itr
);
101 } while (kavl_itr_next(my
, &itr
));