taskrambler  v0.1.8
Web server and task management solution.
rbtree2.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include "class.h"
#include "commons.h"
#include "utils/memory.h"
#include "tree.h"
+ Include dependency graph for rbtree2.c:

Go to the source code of this file.

Macros

#define NVALUES   10
 

Functions

int insertCompare (const void *tval, const void *search)
 
void freeNode (const void *data, const int depth)
 
void printNode (const void *_node, const int depth)
 
void * newEle (int value)
 
int main (int argc, char *argv[])
 

Macro Definition Documentation

#define NVALUES   10

Definition at line 11 of file rbtree2.c.

Function Documentation

void freeNode ( const void *  data,
const int  depth 
)

Definition at line 20 of file rbtree2.c.

References MEM_FREE.

Referenced by main().

21 {
22  printf("now free %d at %p\n", *(int*)data, data);
23  MEM_FREE(data);
24 }
#define MEM_FREE(seg)
Definition: memory.h:28
int data
Definition: binarytree.c:7

+ Here is the caller graph for this function:

int insertCompare ( const void *  tval,
const void *  search 
)

Definition at line 14 of file rbtree2.c.

Referenced by main().

15 {
16  return *(const int *)tval - *(const int *)search;
17 }

+ Here is the caller graph for this function:

int main ( int  argc,
char *  argv[] 
)

=======================================================================

Definition at line 57 of file rbtree2.c.

References freeNode(), insertCompare(), MEM_FREE, memCleanup(), newEle(), printNode(), treeDelete(), treeDestroy(), treeFind(), treeInsert(), and treeWalk().

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 }
#define MEM_FREE(seg)
Definition: memory.h:28
void printNode(const void *_node, const int depth)
Definition: rbtree2.c:27
void * treeInsert(Tree *, const void *, TreeComp)
Definition: insert.c:29
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

+ Here is the call graph for this function:

void* newEle ( int  value)

Definition at line 45 of file rbtree2.c.

References memMalloc().

Referenced by main().

46 {
47  void * val = memMalloc(sizeof(int));
48 
49  *(int*)val = value;
50  return val;
51 }
void * memMalloc(size_t)
Definition: memory.c:783

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void printNode ( const void *  _node,
const int  depth 
)

Definition at line 27 of file rbtree2.c.

Referenced by main().

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 }

+ Here is the caller graph for this function: