taskrambler  0.1.9
Web server and task management solution.
rbtree2.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 
4 #include "class.h"
5 #include "commons.h"
6 #include "utils/memory.h"
7 
8 #include "tree.h"
9 #include "utils/memory.h"
10 
11 #define NVALUES 10
12 
13 int
14 insertCompare(const void * tval, const void * search)
15 {
16  return *(const int *)tval - *(const int *)search;
17 }
18 
19 void
20 freeNode(const void * data, const int depth)
21 {
22  printf("now free %d at %p\n", *(int*)data, data);
23  MEM_FREE(data);
24 }
25 
26 void
27 printNode(const void * _node, const int depth)
28 {
29  Tree node = (Tree)_node;
30  int value = *(int *)node->data;
31  int i;
32 
33  for (i=1; i<7; i++) i<=depth?printf("-"):printf(" ");
34  printf("%p:%d p:%p l:%p r:%p\n",
35  node, value, node->parent, node->left, node->right);
36 
37  // printf("%s %010d(%02d)",
38  // (node->color==rbRed)?"R":"B",
39  // value,
40  // depth);
41 }
42 
43 
44 void *
45 newEle(int value)
46 {
47  void * val = memMalloc(sizeof(int));
48 
49  *(int*)val = value;
50  return val;
51 }
52 
53 /**
54  * =======================================================================
55  */
56 int
57 main(int argc, char * argv[])
58 {
59  Tree root = NULL;
60  int * found = NULL;
61  int * element = NULL;
62 
63  int search10 = 10;
64  int search64 = 64;
65  int search70 = 70;
66  int search80 = 80;
67  int search50 = 50;
68 
69  treeInsert(&root, newEle(40), insertCompare);
70  treeInsert(&root, newEle(50), insertCompare);
71  treeInsert(&root, newEle(60), insertCompare);
72  treeInsert(&root, newEle(70), insertCompare);
73  treeInsert(&root, newEle(80), insertCompare);
74  treeInsert(&root, newEle(45), insertCompare);
75  treeInsert(&root, newEle(75), insertCompare);
76  treeInsert(&root, newEle(85), insertCompare);
77  puts("traverse");
78  treeWalk(root, printNode);
79  puts("");
80 
81  element = newEle(70);
82  found = treeInsert(&root, element, insertCompare);
83  printf("insert %p(%d) got %p(%d)\n", element, *element, found, *found);
84  if (found != element) {
85  printf("remove duplicate");
86  MEM_FREE(element);
87  }
88  puts("traverse");
89  treeWalk(root, printNode);
90  puts("");
91 
92  found = treeFind(root, &search10, insertCompare);
93  if (NULL == found) {
94  printf("can't find segmenet of minimum size: %d\n", 10);
95  } else {
96  printf("found %d\n", *found);
97  }
98  puts("");
99 
100  found = treeFind(root, &search64, insertCompare);
101  if (NULL == found) {
102  printf("can't find segmenet of minimum size: %d\n", 64);
103  } else {
104  printf("found %d\n", *found);
105  }
106  puts("");
107 
108  found = treeFind(root, &search70, insertCompare);
109  if (NULL == found) {
110  printf("can't find segmenet of minimum size: %d\n", 70);
111  } else {
112  printf("found %d\n", *found);
113  }
114  puts("");
115 
116  found = treeDelete(&root, (void *)&search70, insertCompare);
117  printf("delete %p(%d) got %p(%d)\n", &search70, search70, found, *found);
118  MEM_FREE(found);
119  puts("traverse");
120  treeWalk(root, printNode);
121  puts("");
122 
123  found = treeInsert(&root, (void *)&search80, insertCompare);
124  printf("insert %p(%d) got %p(%d)\n", &search80, search80, found, *found);
125  found = treeInsert(&root, (void *)&search50, insertCompare);
126  printf("insert %p(%d) got %p(%d)\n", &search50, search50, found, *found);
127  found = treeInsert(&root, (void *)&search80, insertCompare);
128  printf("insert %p(%d) got %p(%d)\n", &search80, search80, found, *found);
129 
130  puts("traverse");
131  treeWalk(root, printNode);
132  puts("");
133 
134  found = treeDelete(&root, (void *)&search80, insertCompare);
135  printf("delete %p(%d) got %p(%d)\n", &search80, search80, found, *found);
136  MEM_FREE(found);
137  puts("traverse");
138  treeWalk(root, printNode);
139  puts("");
140 
141  found = treeDelete(&root, (void *)&search50, insertCompare);
142  printf("delete %p(%d) got %p(%d)\n", &search50, search50, found, *found);
143  MEM_FREE(found);
144  puts("traverse");
145  treeWalk(root, printNode);
146  puts("");
147 
148  found = treeDelete(&root, (void *)&search70, insertCompare);
149  printf("delete %p(%d) got %p(%d)\n", &search70, search70, found, found?*found:-1);
150  MEM_FREE(found);
151  puts("traverse");
152  treeWalk(root, printNode);
153  puts("");
154 
155  element = newEle(60);
156  found = treeDelete(&root, element, insertCompare);
157  printf("delete %p(%d) got %p(%d)\n",
158  element,
159  *element,
160  found,
161  found?*found:-1);
162  if (found != element) {
163  MEM_FREE(element);
164  }
165  MEM_FREE(found);
166  puts("traverse");
167  treeWalk(root, printNode);
168  puts("");
169 
170  treeDestroy(&root, freeNode);
171  memCleanup();
172 
173  return 0;
174 }
175 
176 // vim: set et ts=4 sw=4:
#define MEM_FREE(seg)
Definition: memory.h:28
void * memMalloc(size_t)
Definition: memory.c:783
void printNode(const void *_node, const int depth)
Definition: rbtree2.c:27
int main(int argc, char *argv[])
Definition: rbtree2.c:57
void * treeInsert(Tree *, const void *, TreeComp)
Definition: insert.c:29
int data
Definition: binarytree.c:7
void freeNode(const void *data, const int depth)
Definition: rbtree2.c:20
void * newEle(int value)
Definition: rbtree2.c:45
int insertCompare(const void *tval, const void *search)
Definition: rbtree2.c:14
void * treeFind(Tree, const void *, TreeComp)
Definition: find.c:26
void * treeDelete(Tree *, const void *, TreeComp)
Definition: tree/delete.c:31
void memCleanup()
Definition: memory.c:862
void treeWalk(Tree, TreeAction)
Definition: walk.c:26
void treeDestroy(Tree *, TreeAction)
Definition: destroy.c:26