-
Notifications
You must be signed in to change notification settings - Fork 0
/
ck.c
57 lines (56 loc) · 2.45 KB
/
ck.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
#ifdef BST_ELL_LESS
# include "test/floorLg.c"
#endif
#define BST_E(...) printf("[34;7m0x%lx: [m",(long)p),printf(__VA_ARGS__),\
puts(""), ++*nE /* auto-newline and error bump */
BST_LKG int _(_ck)(BST_P p, int *nE) { /** VERIFY TREE PROPERTIES **/
int h0 = 0, h1 = 0;
if (!p)
return 0; /* null tree always correct, height 0 */
if (BST_wt(p) != BST_wt(BST_ln(p,0)) + BST_wt(BST_ln(p,1)) + 1)
BST_E("wt %d != %d + %d + 1", BST_wt(p),
BST_wt(BST_ln(p,0)), BST_wt(BST_ln(p,1)));
/* NOTE: assume native struct field access to 'key' and that '>' works */
if (BST_ln(p, 0) != BST_0) {
if (BST_NODE(BST_ln(p, 0)).key > BST_NODE(p).key) /* check 0-order */
BST_E("%d > %d", BST_NODE(BST_ln(p, 0)).key, BST_NODE(p).key);
h0 = _(_ck)(BST_ln(p, 0), nE); /* check 0-kid */
#ifdef BST_PARENT
if (BST_up(BST_ln(p, 0)) != p)
BST_E("up[kid0] == 0x%lx", (long)BST_up(BST_ln(p, 0)));
#endif
}
if (BST_ln(p, 1) != BST_0) {
if (BST_NODE(BST_ln(p, 1)).key < BST_NODE(p).key) /* check 1-order */
BST_E("%d < %d", BST_NODE(BST_ln(p, 1)).key, BST_NODE(p).key);
h1 = _(_ck)(BST_ln(p, 1), nE); /* check 1-kid */
#ifdef BST_PARENT
if (BST_up(BST_ln(p, 1)) != p)
BST_E("up[kid1] == 0x%lx", (long)BST_up(BST_ln(p, 1)));
#endif
}
#if defined BST_AVL /* AVL properties */
if (abs(h0 - h1) > 1)
BST_E("|%d - %d| > 1", h0, h1);
if (BST_bal(p) != h1 - h0)
BST_E("bal != %d", h1 - h0);
return (h0 > h1 ? h0 : h1) + 1; /* accum height going up */
#elif defined BST_RB /* Red-Black properties */
if (h0 != h1)
BST_E("BlackHeight %d != %d", h0, h1);
if (BST_red(p)) {
if (BST_ln(p, 0) != BST_0 && BST_red(BST_ln(p, 0)))
BST_E("Red Node Has Red 0-Kid");
if (BST_ln(p, 1) != BST_0 && BST_red(BST_ln(p, 1)))
BST_E("Red Node Has Red 1-Kid");
}
return h0 + !BST_red(p); /* black height going up */
#else
#ifdef BST_ELL_LESS /* ELL-balance properties */
if (abs(floorLg(BST_wt(BST_ln(p, 0))) - floorLg(BST_wt(BST_ln(p, 1)))) > 1)
BST_E("|%d - %d| > 1",
floorLg(BST_wt(BST_ln(p,0))), floorLg(BST_wt(BST_ln(p,1))));
#endif
return 0;
#endif
}