diff --git a/bintree.c b/bintree.c index c55d660..71ed18a 100644 --- a/bintree.c +++ b/bintree.c @@ -21,7 +21,7 @@ unsigned int treeSize (const TreeNode *root); // self declared functions - TreeNode *addToTreeRec (TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate); + TreeNode *addToTreeRec (TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate, const int root); void clearTreeRec (TreeNode *currentNode); void clearNode (TreeNode *node); void treeSizeRec (const TreeNode *currentNode, unsigned int *nodeCount); @@ -44,16 +44,16 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc memcpy(newNode->data, data, dataSize); - return addToTreeRec(root, newNode, compareFct, isDuplicate); + return addToTreeRec(root, newNode, compareFct, isDuplicate, 1); } -TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate) +TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate, const int root) { if ((currentNode == NULL)) { // printf("Node == NULL\n"); - if (isDuplicate == NULL) + if ((isDuplicate == NULL) || root) { return newNode; } @@ -65,19 +65,19 @@ TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType else if ((compareFct(currentNode->data, newNode->data) < 0)) { // printf("<0\n"); - currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate); + currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate, 0); } else if ((compareFct(currentNode->data, newNode->data) > 0)) { // printf(">0\n"); - currentNode->right = addToTreeRec(currentNode->right, newNode, compareFct, isDuplicate); + currentNode->right = addToTreeRec(currentNode->right, newNode, compareFct, isDuplicate, 0); } else if ((compareFct(currentNode->data, newNode->data) == 0)) { // printf("==0\n"); if (isDuplicate == NULL) { - currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate); + currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate, 0); } else { diff --git a/bintree.o b/bintree.o index 707274c..c1bf070 100644 Binary files a/bintree.o and b/bintree.o differ diff --git a/bintreeTests.c b/bintreeTests.c index 8e33e98..40fdc05 100644 --- a/bintreeTests.c +++ b/bintreeTests.c @@ -116,15 +116,15 @@ void test_clearTreeworksLikeExpected(void) printf("Tree Filled\n"); // Save all Adresses - void *node1 = testRoot->left; - /* + TreeNode *node1 = testRoot->left; + TreeNode *node2 = testRoot->left->left; TreeNode *node3 = testRoot->left->right; TreeNode *node4 = testRoot->right; TreeNode *node5 = testRoot->right->left; TreeNode *node6 = testRoot->right->right; TreeNode *node7 = testRoot->left->left->left; - */ + printf("Adresses Saved\n"); clearTree(testRoot); @@ -132,7 +132,7 @@ void test_clearTreeworksLikeExpected(void) // Check if everything has been set to NULL TEST_ASSERT_NULL(testRoot->data); - //TEST_ASSERT_NULL(testRoot); + TEST_ASSERT_NULL(testRoot); //TEST_ASSERT_NULL(node1->data); // TEST_ASSERT_NULL(node1); @@ -199,9 +199,9 @@ int main() UNITY_BEGIN(); printf("\n============================\nBinary Tree tests\n============================\n"); - RUN_TEST(test_addToTreeExpandsTreeCorrectly); + // RUN_TEST(test_addToTreeExpandsTreeCorrectly); // RUN_TEST(test_nextTreeDataReturnsNextDataCorrectly); - // RUN_TEST(test_clearTreeworksLikeExpected); + RUN_TEST(test_clearTreeworksLikeExpected); // RUN_TEST(test_treeSizeWorkingLikeExpected); return UNITY_END(); diff --git a/runbintreeTests.exe b/runbintreeTests.exe index 1e05d19..a66c8f7 100644 Binary files a/runbintreeTests.exe and b/runbintreeTests.exe differ