rbtree2.c
3.93 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
#include <stdio.h>
#include "class.h"
#include "commons.h"
#include "utils/memory.h"
#include "tree.h"
#define NVALUES 10
int
insertCompare(const void * tval, const void * search)
{
return *(const int *)tval - *(const int *)search;
}
void
printNode(const void * _node, const int depth)
{
//Tree node = (Tree)_node;
//int value = *(int *)node->data;
int value = *(int *)_node;
int i;
printf("%010d(%02d)", value, depth);
// printf("%s %010d(%02d)",
// (node->color==rbRed)?"R":"B",
// value,
// depth);
for (i=0; i<depth; i++) printf("-");
puts("");
}
/**
* =======================================================================
*/
int
main(int argc, char * argv[])
{
Tree root = NULL;
int * found = NULL;
int values[] = {40, 50, 60, 70, 80, 45, 75, 85};
int new50 = 50;
int new70 = 70;
int new80 = 80;
int search10 = 10;
int search64 = 64;
treeInsert(&root, (void *)&(values[0]), insertCompare);
treeInsert(&root, (void *)&(values[1]), insertCompare);
treeInsert(&root, (void *)&(values[2]), insertCompare);
treeInsert(&root, (void *)&(values[3]), insertCompare);
treeInsert(&root, (void *)&(values[4]), insertCompare);
treeInsert(&root, (void *)&(values[5]), insertCompare);
treeInsert(&root, (void *)&(values[6]), insertCompare);
treeInsert(&root, (void *)&(values[7]), insertCompare);
puts("traverse");
treeWalk(root, printNode);
puts("");
found = treeInsert(&root, (void *)&new70, insertCompare);
printf("insert %p(%d) got %p(%d)\n", &new70, new70, found, *found);
puts("traverse");
treeWalk(root, printNode);
puts("");
found = treeFind(root, &search10, insertCompare);
if (NULL == found) {
printf("can't find segmenet of minimum size: %d\n", 10);
} else {
printf("found %d\n", *found);
}
puts("");
found = treeFind(root, &search64, insertCompare);
if (NULL == found) {
printf("can't find segmenet of minimum size: %d\n", 64);
} else {
printf("found %d\n", *found);
}
puts("");
found = treeFind(root, &new70, insertCompare);
if (NULL == found) {
printf("can't find segmenet of minimum size: %d\n", 70);
} else {
printf("found %d\n", *found);
}
puts("");
found = treeDelete(&root, (void *)&new70, insertCompare);
printf("delete %p(%d) got %p(%d)\n", &new70, new70, found, *found);
puts("traverse");
treeWalk(root, printNode);
puts("");
found = treeInsert(&root, (void *)&new80, insertCompare);
printf("insert %p(%d) got %p(%d)\n", &new80, new80, found, *found);
found = treeInsert(&root, (void *)&new50, insertCompare);
printf("insert %p(%d) got %p(%d)\n", &new50, new50, found, *found);
found = treeInsert(&root, (void *)&new80, insertCompare);
printf("insert %p(%d) got %p(%d)\n", &new80, new80, found, *found);
puts("traverse");
treeWalk(root, printNode);
puts("");
found = treeDelete(&root, (void *)&new80, insertCompare);
printf("delete %p(%d) got %p(%d)\n", &new80, new80, found, *found);
puts("traverse");
treeWalk(root, printNode);
puts("");
found = treeDelete(&root, (void *)&new50, insertCompare);
printf("delete %p(%d) got %p(%d)\n", &new50, new50, found, *found);
puts("traverse");
treeWalk(root, printNode);
puts("");
found = treeDelete(&root, (void *)&new70, insertCompare);
printf("delete %p(%d) got %p(%d)\n", &new70, new70, found, found?*found:-1);
puts("traverse");
treeWalk(root, printNode);
puts("");
found = treeDelete(&root, (void *)&(values[2]), insertCompare);
printf("delete %p(%d) got %p(%d)\n",
&(values[2]),
values[2],
found,
found?*found:-1);
puts("traverse");
treeWalk(root, printNode);
puts("");
//
// post(root, cleanup);
return 0;
}
// vim: set et ts=4 sw=4: