diff --git a/bintree.c b/bintree.c index 5f5fbbc..1201de5 100644 --- a/bintree.c +++ b/bintree.c @@ -34,14 +34,16 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc int cmp = compareFct(data, root->data); if (cmp == 0){ - + if (isDuplicate != NULL) { + *isDuplicate = 1; + } /* if (isDuplicate == NULL){ root->right = addToTree(root->right, data, dataSize, compareFct, NULL); return root; }*/ - *isDuplicate = 1; + return root; } @@ -60,13 +62,39 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc // push the top node and push all its left nodes. void *nextTreeData(TreeNode *root) { - if (root != NULL){ - - - - + static StackNode *stack = NULL; + static TreeNode* currentNode = NULL; + void* result; + if(stack==NULL && root != NULL){ + currentNode = root; + while(currentNode != NULL){ + stack = push(stack,currentNode); + currentNode = currentNode->left; + } } + + if (stack == NULL)return NULL; + + currentNode = top(stack); + stack = pop(stack); + + + result = currentNode->data; + + if(currentNode->right != NULL){ + currentNode = currentNode->right; + + while(currentNode != NULL){ + stack = push(stack,currentNode); + currentNode = currentNode->left; + } + } + + + + + return result; } // Releases all memory resources (including data copies). @@ -103,8 +131,8 @@ int compareFct(const void *a, const void *b){ unsigned int A = *(unsigned int *)a; unsigned int B = *(unsigned int *)b; int result= 0; - if(A>B)result = -1; //logik vertauscht? - if(B>A)result = 1; //logik vertauscht? + if(A>B)result = 1; + if(B>A)result = -1; if(A==B)result = 0; return result; diff --git a/bintreeTests.c b/bintreeTests.c new file mode 100644 index 0000000..83cb0f1 --- /dev/null +++ b/bintreeTests.c @@ -0,0 +1,25 @@ +#include +#include "unity/unity.h" +#include "bintree.h" + +void setUp(void){} +void tearDown(void){} + +void TEST_BINTREE{ + + +} + + + +int main(){ + UNITY_BEGIN(); + + printf("\n============================\nbintree tests\n============================\n"); + + //RUN_TEST(); + //RUN_TEST(); + //RUN_TEST(); + + return UNITY_END(); +} \ No newline at end of file