|
|
@ -1,10 +1,12 @@ |
|
|
#include <stdio.h> |
|
|
#include <stdio.h> |
|
|
|
|
|
#include <stdlib.h> |
|
|
|
|
|
|
|
|
#include "class.h" |
|
|
#include "class.h" |
|
|
#include "commons.h" |
|
|
#include "commons.h" |
|
|
#include "utils/memory.h" |
|
|
#include "utils/memory.h" |
|
|
|
|
|
|
|
|
#include "tree.h" |
|
|
#include "tree.h" |
|
|
|
|
|
#include "utils/memory.h" |
|
|
|
|
|
|
|
|
#define NVALUES 10 |
|
|
#define NVALUES 10 |
|
|
|
|
|
|
|
|
@ -14,54 +16,75 @@ insertCompare(const void * tval, const void * search) |
|
|
return *(const int *)tval - *(const int *)search; |
|
|
return *(const int *)tval - *(const int *)search; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void |
|
|
|
|
|
freeNode(const void * data, const int depth) |
|
|
|
|
|
{ |
|
|
|
|
|
printf("now free %d at %p\n", *(int*)data, data); |
|
|
|
|
|
MEM_FREE(data); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
void |
|
|
void |
|
|
printNode(const void * _node, const int depth) |
|
|
printNode(const void * _node, const int depth) |
|
|
{ |
|
|
{ |
|
|
//Tree node = (Tree)_node; |
|
|
|
|
|
//int value = *(int *)node->data; |
|
|
|
|
|
int value = *(int *)_node; |
|
|
|
|
|
|
|
|
Tree node = (Tree)_node; |
|
|
|
|
|
int value = *(int *)node->data; |
|
|
int i; |
|
|
int i; |
|
|
|
|
|
|
|
|
printf("%010d(%02d)", value, depth); |
|
|
|
|
|
|
|
|
for (i=1; i<7; i++) i<=depth?printf("-"):printf(" "); |
|
|
|
|
|
printf("%p:%d p:%p l:%p r:%p\n", |
|
|
|
|
|
node, value, node->parent, node->left, node->right); |
|
|
|
|
|
|
|
|
// printf("%s %010d(%02d)", |
|
|
// printf("%s %010d(%02d)", |
|
|
// (node->color==rbRed)?"R":"B", |
|
|
// (node->color==rbRed)?"R":"B", |
|
|
// value, |
|
|
// value, |
|
|
// depth); |
|
|
// depth); |
|
|
for (i=0; i<depth; i++) printf("-"); |
|
|
|
|
|
puts(""); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void * |
|
|
|
|
|
newEle(int value) |
|
|
|
|
|
{ |
|
|
|
|
|
void * val = memMalloc(sizeof(int)); |
|
|
|
|
|
|
|
|
|
|
|
*(int*)val = value; |
|
|
|
|
|
return val; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* ======================================================================= |
|
|
* ======================================================================= |
|
|
*/ |
|
|
*/ |
|
|
int |
|
|
int |
|
|
main(int argc, char * argv[]) |
|
|
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); |
|
|
|
|
|
|
|
|
Tree root = NULL; |
|
|
|
|
|
int * found = NULL; |
|
|
|
|
|
int * element = NULL; |
|
|
|
|
|
|
|
|
|
|
|
int search10 = 10; |
|
|
|
|
|
int search64 = 64; |
|
|
|
|
|
int search70 = 70; |
|
|
|
|
|
int search80 = 80; |
|
|
|
|
|
int search50 = 50; |
|
|
|
|
|
|
|
|
|
|
|
treeInsert(&root, newEle(40), insertCompare); |
|
|
|
|
|
treeInsert(&root, newEle(50), insertCompare); |
|
|
|
|
|
treeInsert(&root, newEle(60), insertCompare); |
|
|
|
|
|
treeInsert(&root, newEle(70), insertCompare); |
|
|
|
|
|
treeInsert(&root, newEle(80), insertCompare); |
|
|
|
|
|
treeInsert(&root, newEle(45), insertCompare); |
|
|
|
|
|
treeInsert(&root, newEle(75), insertCompare); |
|
|
|
|
|
treeInsert(&root, newEle(85), insertCompare); |
|
|
puts("traverse"); |
|
|
puts("traverse"); |
|
|
treeWalk(root, printNode); |
|
|
treeWalk(root, printNode); |
|
|
puts(""); |
|
|
puts(""); |
|
|
|
|
|
|
|
|
found = treeInsert(&root, (void *)&new70, insertCompare); |
|
|
|
|
|
printf("insert %p(%d) got %p(%d)\n", &new70, new70, found, *found); |
|
|
|
|
|
|
|
|
element = newEle(70); |
|
|
|
|
|
found = treeInsert(&root, element, insertCompare); |
|
|
|
|
|
printf("insert %p(%d) got %p(%d)\n", element, *element, found, *found); |
|
|
|
|
|
if (found != element) { |
|
|
|
|
|
printf("remove duplicate"); |
|
|
|
|
|
MEM_FREE(element); |
|
|
|
|
|
} |
|
|
puts("traverse"); |
|
|
puts("traverse"); |
|
|
treeWalk(root, printNode); |
|
|
treeWalk(root, printNode); |
|
|
puts(""); |
|
|
puts(""); |
|
|
@ -82,7 +105,7 @@ main(int argc, char * argv[]) |
|
|
} |
|
|
} |
|
|
puts(""); |
|
|
puts(""); |
|
|
|
|
|
|
|
|
found = treeFind(root, &new70, insertCompare); |
|
|
|
|
|
|
|
|
found = treeFind(root, &search70, insertCompare); |
|
|
if (NULL == found) { |
|
|
if (NULL == found) { |
|
|
printf("can't find segmenet of minimum size: %d\n", 70); |
|
|
printf("can't find segmenet of minimum size: %d\n", 70); |
|
|
} else { |
|
|
} else { |
|
|
@ -90,53 +113,62 @@ main(int argc, char * argv[]) |
|
|
} |
|
|
} |
|
|
puts(""); |
|
|
puts(""); |
|
|
|
|
|
|
|
|
found = treeDelete(&root, (void *)&new70, insertCompare); |
|
|
|
|
|
printf("delete %p(%d) got %p(%d)\n", &new70, new70, found, *found); |
|
|
|
|
|
|
|
|
found = treeDelete(&root, (void *)&search70, insertCompare); |
|
|
|
|
|
printf("delete %p(%d) got %p(%d)\n", &search70, search70, found, *found); |
|
|
|
|
|
MEM_FREE(found); |
|
|
puts("traverse"); |
|
|
puts("traverse"); |
|
|
treeWalk(root, printNode); |
|
|
treeWalk(root, printNode); |
|
|
puts(""); |
|
|
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); |
|
|
|
|
|
|
|
|
found = treeInsert(&root, (void *)&search80, insertCompare); |
|
|
|
|
|
printf("insert %p(%d) got %p(%d)\n", &search80, search80, found, *found); |
|
|
|
|
|
found = treeInsert(&root, (void *)&search50, insertCompare); |
|
|
|
|
|
printf("insert %p(%d) got %p(%d)\n", &search50, search50, found, *found); |
|
|
|
|
|
found = treeInsert(&root, (void *)&search80, insertCompare); |
|
|
|
|
|
printf("insert %p(%d) got %p(%d)\n", &search80, search80, found, *found); |
|
|
|
|
|
|
|
|
puts("traverse"); |
|
|
puts("traverse"); |
|
|
treeWalk(root, printNode); |
|
|
treeWalk(root, printNode); |
|
|
puts(""); |
|
|
puts(""); |
|
|
|
|
|
|
|
|
found = treeDelete(&root, (void *)&new80, insertCompare); |
|
|
|
|
|
printf("delete %p(%d) got %p(%d)\n", &new80, new80, found, *found); |
|
|
|
|
|
|
|
|
found = treeDelete(&root, (void *)&search80, insertCompare); |
|
|
|
|
|
printf("delete %p(%d) got %p(%d)\n", &search80, search80, found, *found); |
|
|
|
|
|
MEM_FREE(found); |
|
|
puts("traverse"); |
|
|
puts("traverse"); |
|
|
treeWalk(root, printNode); |
|
|
treeWalk(root, printNode); |
|
|
puts(""); |
|
|
puts(""); |
|
|
|
|
|
|
|
|
found = treeDelete(&root, (void *)&new50, insertCompare); |
|
|
|
|
|
printf("delete %p(%d) got %p(%d)\n", &new50, new50, found, *found); |
|
|
|
|
|
|
|
|
found = treeDelete(&root, (void *)&search50, insertCompare); |
|
|
|
|
|
printf("delete %p(%d) got %p(%d)\n", &search50, search50, found, *found); |
|
|
|
|
|
MEM_FREE(found); |
|
|
puts("traverse"); |
|
|
puts("traverse"); |
|
|
treeWalk(root, printNode); |
|
|
treeWalk(root, printNode); |
|
|
puts(""); |
|
|
puts(""); |
|
|
|
|
|
|
|
|
found = treeDelete(&root, (void *)&new70, insertCompare); |
|
|
|
|
|
printf("delete %p(%d) got %p(%d)\n", &new70, new70, found, found?*found:-1); |
|
|
|
|
|
|
|
|
found = treeDelete(&root, (void *)&search70, insertCompare); |
|
|
|
|
|
printf("delete %p(%d) got %p(%d)\n", &search70, search70, found, found?*found:-1); |
|
|
|
|
|
MEM_FREE(found); |
|
|
puts("traverse"); |
|
|
puts("traverse"); |
|
|
treeWalk(root, printNode); |
|
|
treeWalk(root, printNode); |
|
|
puts(""); |
|
|
puts(""); |
|
|
|
|
|
|
|
|
found = treeDelete(&root, (void *)&(values[2]), insertCompare); |
|
|
|
|
|
|
|
|
element = newEle(60); |
|
|
|
|
|
found = treeDelete(&root, element, insertCompare); |
|
|
printf("delete %p(%d) got %p(%d)\n", |
|
|
printf("delete %p(%d) got %p(%d)\n", |
|
|
&(values[2]), |
|
|
|
|
|
values[2], |
|
|
|
|
|
|
|
|
element, |
|
|
|
|
|
*element, |
|
|
found, |
|
|
found, |
|
|
found?*found:-1); |
|
|
found?*found:-1); |
|
|
|
|
|
if (found != element) { |
|
|
|
|
|
MEM_FREE(element); |
|
|
|
|
|
} |
|
|
|
|
|
MEM_FREE(found); |
|
|
puts("traverse"); |
|
|
puts("traverse"); |
|
|
treeWalk(root, printNode); |
|
|
treeWalk(root, printNode); |
|
|
puts(""); |
|
|
puts(""); |
|
|
|
|
|
|
|
|
// |
|
|
|
|
|
// post(root, cleanup); |
|
|
|
|
|
|
|
|
treeDestroy(&root, freeNode); |
|
|
|
|
|
memCleanup(); |
|
|
|
|
|
|
|
|
return 0; |
|
|
return 0; |
|
|
} |
|
|
} |
|
|
|