diff --git a/bintree.c b/bintree.c index 6cf26f9..a7846be 100644 --- a/bintree.c +++ b/bintree.c @@ -51,27 +51,35 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate) { printf("entered addRec\n"); - if (currentNode == NULL) + if ((currentNode == NULL)) { printf("currentNode == NULL\n"); - if (isDuplicate != NULL) + if (isDuplicate == NULL) { - *isDuplicate = 0; + + return newNode; + } + //else if (isDuplicate != 1) + //{ + // *isDuplicate = 0; + // return newNode; + //} + else + { + return currentNode; } - - return newNode; } - else if (compareFct(¤tNode->data, &newNode->data) < 0) + else if ((compareFct(¤tNode->data, &newNode->data) < 0)) { printf("compareFct(currentNode->data, newNode->data) < 0\n"); currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate); } - else if (compareFct(¤tNode->data, &newNode->data) > 0) + else if ((compareFct(¤tNode->data, &newNode->data) > 0)) { printf("compareFct(currentNode->data, newNode->data) > 0\n"); currentNode->right = addToTreeRec(currentNode->right, newNode, compareFct, isDuplicate); } - else if (compareFct(¤tNode->data, &newNode->data) == 0) + else if ((compareFct(¤tNode->data, &newNode->data) == 0)) { //duplicate printf("duplicate\n"); @@ -81,6 +89,7 @@ TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType } else { + printf("setDuplicate\n"); *isDuplicate = 1; } } diff --git a/bintree.o b/bintree.o index e53f36e..d193ca1 100644 Binary files a/bintree.o and b/bintree.o differ diff --git a/bintreeTests.c b/bintreeTests.c index f8b61ba..9f5d834 100644 --- a/bintreeTests.c +++ b/bintreeTests.c @@ -37,7 +37,7 @@ static int compareIntEntries(const void *arg1, const void *arg2) void test_addToTreeExpandsTreeCorrectly(void) { TreeNode *testRoot = NULL; - int *testIsDouble = 0; + int testIsDouble = 0; int score1 = 12; int score2 = 6; int score3 = 18; @@ -102,9 +102,10 @@ void test_addToTreeExpandsTreeCorrectly(void) testRoot = addToTree(testRoot, &score4, sizeof(int), compareIntEntries, NULL); TEST_ASSERT_NOT_NULL(testRoot->left->left->left); TEST_ASSERT_EQUAL_UINT16(score4, testRoot->left->left->left->data); + printf("double passed\n"); // Trying to add Double while Doubles not Permitted - testRoot = addToTree(testRoot, &score7, sizeof(int), compareIntEntries, testIsDouble); + testRoot = addToTree(testRoot, &score7, sizeof(int), compareIntEntries, &testIsDouble); TEST_ASSERT_NULL(testRoot->right->right->left); TEST_ASSERT_EQUAL_UINT16(1, testIsDouble);