view src/rbtree_test.c @ 11:8b09943f1a70

Fixed return value of list_next and list_prev
author Eris Caffee <discordia@eldalin.com>
date Fri, 28 Sep 2012 19:37:10 -0500
parents
children d359966ed8de
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdbool.h>
5 #include "rbtree.h"
7 void print_val(void * data)
8 {
9 printf("%d", *((int *) data));
10 }
12 bool le(void * a, void * b)
13 {
14 return *((int *) a) <= *((int *) b);
15 }
17 bool eq(void * a, void * b)
18 {
19 return *((int *) a) == *((int *) b);
20 }
22 #define MAX_NUMS 100
24 int main (int argc, char ** argv)
25 {
26 int sorted[100] = {
27 35005211, 42999170, 84353895, 135497281, 137806862, 149798315, 184803526, 233665123, 278722862, 294702567,
28 304089172, 336465782, 356426808, 412776091, 424238335, 468703135, 491705403, 511702305, 521595368, 572660336,
29 596516649, 608413784, 610515434, 628175011, 635723058, 709393584, 719885386, 749241873, 752392754, 756898537,
30 760313750, 783368690, 805750846, 846930886, 855636226, 859484421, 861021530, 939819582, 943947739, 945117276,
31 982906996, 1025202362, 1059961393, 1100661313, 1101513929, 1102520059, 1125898167, 1129566413, 1131176229, 1141616124,
32 1159126505, 1189641421, 1264095060, 1303455736, 1315634022, 1350490027, 1365180540, 1369133069, 1374344043, 1411549676,
33 1424268980, 1433925857, 1469348094, 1474612399, 1477171087, 1540383426, 1548233367, 1585990364, 1632621729, 1649760492,
34 1653377373, 1656478042, 1681692777, 1714636915, 1726956429, 1734575198, 1749698586, 1780695788, 1801979802, 1804289383,
35 1827336327, 1843993368, 1889947178, 1911759956, 1914544919, 1918502651, 1937477084, 1956297539, 1957747793, 1967513926,
36 1973594324, 1984210012, 1998898814, 2001100545, 2038664370, 2044897763, 2053999932, 2084420925, 2089018456, 2145174067,
37 };
39 struct rbtree * rbtree = rbtree_new(le, eq);
40 if (NULL == rbtree)
41 {
42 perror("rbtree_new");
43 exit(EXIT_FAILURE);
44 }
46 for (int i = 0; i < MAX_NUMS; ++i)
47 {
48 int * ip = malloc(sizeof(int));
49 if (NULL == ip)
50 {
51 perror("malloc");
52 exit(EXIT_FAILURE);
53 }
54 *ip = rand();
55 // Be sure to set MAX_NUMS = 100 if you want to use the sorted array
56 // *ip = sorted[i];
57 int * ip2;
58 if (NULL == (ip2 = rbtree_insert(rbtree, ip)))
59 {
60 printf("rbtree_insert failed\n");
61 free(ip);
62 }
63 printf("inserting %d\n", *ip2);
64 }
67 // print the tree structure
68 rbtree_dump(rbtree, print_val);
69 fprintf(stderr, "black depth is %u\n", rbtree_black_depth(rbtree));
71 // Print the tree in value order
72 struct rbtree_iterator * it;
73 struct rbtree_iterator * next;
74 for (next = it = rbtree_begin(rbtree); NULL != next; next = rbtree_next(it))
75 printf("%d\n", *((int *) rbtree_value(it)));
76 free(it);
79 // Free the whole tree
80 for (next = it = rbtree_begin(rbtree); NULL != next; next = rbtree_next(it))
81 free(rbtree_value(it));
82 free(it);
85 rbtree_delete(rbtree);
87 exit(EXIT_SUCCESS);
88 }