Compare commits

...

3 Commits

3 changed files with 48 additions and 38 deletions

View File

@ -50,41 +50,50 @@ 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)
// printf("entered addRec\n");
if ((currentNode == NULL))
{
printf("currentNode == NULL\n");
if (isDuplicate != NULL)
// printf("currentNode == NULL\n");
if (isDuplicate == NULL)
{
*isDuplicate = 0;
}
return newNode;
return newNode;
}
//else if (isDuplicate != 1)
//{
// *isDuplicate = 0;
// return newNode;
//}
else
{
return currentNode;
}
}
else if (compareFct(&currentNode->data, &newNode->data) < 0)
else if ((compareFct(&currentNode->data, &newNode->data) < 0))
{
printf("compareFct(currentNode->data, newNode->data) < 0\n");
// printf("compareFct(currentNode->data, newNode->data) < 0\n");
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate);
}
else if (compareFct(&currentNode->data, &newNode->data) > 0)
else if ((compareFct(&currentNode->data, &newNode->data) > 0))
{
printf("compareFct(currentNode->data, newNode->data) > 0\n");
// printf("compareFct(currentNode->data, newNode->data) > 0\n");
currentNode->right = addToTreeRec(currentNode->right, newNode, compareFct, isDuplicate);
}
else if (compareFct(&currentNode->data, &newNode->data) == 0)
else if ((compareFct(&currentNode->data, &newNode->data) == 0))
{
//duplicate
printf("duplicate\n");
// printf("duplicate\n");
if (isDuplicate == NULL)
{
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate);
}
else
{
// printf("setDuplicate\n");
*isDuplicate = 1;
}
}
printf("passed everything\n");
// printf("passed everything\n");
return currentNode;
}

BIN
bintree.o

Binary file not shown.

View File

@ -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;
@ -48,69 +48,70 @@ void test_addToTreeExpandsTreeCorrectly(void)
testRoot = addToTree(testRoot, &score1, sizeof(int), compareIntEntries, NULL);
printf("add1passed\n");
// printf("add1passed\n");
testRoot = addToTree(testRoot, &score2, sizeof(int), compareIntEntries, NULL);
printf("add2passed\n");
// printf("add2passed\n");
testRoot = addToTree(testRoot, &score3, sizeof(int), compareIntEntries, NULL);
printf("add3passed\n");
// printf("add3passed\n");
testRoot = addToTree(testRoot, &score4, sizeof(int), compareIntEntries, NULL);
printf("add4passed\n");
// printf("add4passed\n");
testRoot = addToTree(testRoot, &score5, sizeof(int), compareIntEntries, NULL);
printf("add5passed\n");
// printf("add5passed\n");
testRoot = addToTree(testRoot, &score6, sizeof(int), compareIntEntries, NULL);
printf("add6passed\n");
/*
// printf("add6passed\n");
testRoot = addToTree(testRoot, &score7, sizeof(int), compareIntEntries, NULL);
printf("add7passed\n");
*/
// printf("add7passed\n");
// Checking the Tree without Doubles
TEST_ASSERT_NOT_NULL(testRoot);
TEST_ASSERT_EQUAL_UINT16(score1, testRoot->data);
printf("node1passed\n");
// printf("node1passed\n");
TEST_ASSERT_NOT_NULL(testRoot->left);
TEST_ASSERT_EQUAL_UINT16(score2, testRoot->left->data);
printf("node2passed\n");
// printf("node2passed\n");
TEST_ASSERT_NOT_NULL(testRoot->right);
TEST_ASSERT_EQUAL_UINT16(score3, testRoot->right->data);
printf("node3passed\n");
// printf("node3passed\n");
TEST_ASSERT_NOT_NULL(testRoot->left->left);
TEST_ASSERT_EQUAL_UINT16(score4, testRoot->left->left->data);
printf("node4passed\n");
// printf("node4passed\n");
TEST_ASSERT_NOT_NULL(testRoot->left->right);
TEST_ASSERT_EQUAL_UINT16(score5, testRoot->left->right->data);
printf("node5passed\n");
// printf("node5passed\n");
TEST_ASSERT_NOT_NULL(testRoot->right->left);
TEST_ASSERT_EQUAL_UINT16(score6, testRoot->right->left->data);
printf("node6passed\n");
/*
// printf("node6passed\n");
TEST_ASSERT_NOT_NULL(testRoot->right->right);
TEST_ASSERT_EQUAL_UINT16(score7, testRoot->right->right->data);
printf("node7passed\n");
/*
// printf("node7passed\n");
// Adding Double
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);
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);
clearTree(testRoot);
// */
//clearTree(testRoot);
}