5 int compare(void *a, void *b) {
6 return *(int *)a - *(int *)b;
9 int main(int argc, char **argv) {
15 // process 2000 records
21 maxnum = atoi(argv[1]);
23 // obtain handle to red-black tree
26 printf("maxnum = %d\n", maxnum);
27 for (ct = maxnum; ct; ct--) {
28 int key = rand() % 90 + 1;
30 if ((i = rbtFind(h, &key)) != rbtEnd(h)) {
31 // found an existing node
34 // get key-value pointers
35 rbtKeyValue(h, i, &keyp, &valuep);
37 // check to see they contain correct data
38 if (*(int *)keyp != key) printf("fail keyp\n");
39 if (*(int *)valuep != 10*key) printf("fail valuep\n");
41 // erase node in red-black tree
42 status = rbtErase(h, i);
43 if (status) printf("fail: status = %d\n", status);
45 // free the pointers allocated by main
46 free(keyp); free(valuep);
52 // allocate key/value data
53 keyp = (int *)malloc(sizeof(int));
54 valuep = (int *)malloc(sizeof(int));
56 // initialize with values
60 // insert in red-black tree
61 status = rbtInsert(h, keyp, valuep);
62 if (status) printf("fail: status = %d\n", status);
66 // output nodes in order
67 for (i = rbtBegin(h); i != rbtEnd(h); i = rbtNext(h, i)) {
69 rbtKeyValue(h, i, &keyp, &valuep);
70 printf("%d %d\n", *(int *)keyp, *(int *)valuep);
73 // delete my allocated memory
74 for (i = rbtBegin(h); i != rbtEnd(h); i = rbtNext(h, i)) {
76 rbtKeyValue(h, i, &keyp, &valuep);
77 free(keyp); free(valuep);
80 // delete red-black tree