addToTreeExpandsTreeCorrectly
This commit is contained in:
parent
d56a2f7a03
commit
6b30677698
25
bintree.c
25
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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user