adToTree working again
This commit is contained in:
parent
02e008c03a
commit
72f1d080a0
19
bintree.c
19
bintree.c
@ -41,7 +41,7 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
|
||||
newNode->data = calloc(1, dataSize);
|
||||
newNode->left = NULL;
|
||||
newNode->right = NULL;
|
||||
memcpy(&newNode->data, data, dataSize);
|
||||
memcpy(newNode->data, data, dataSize);
|
||||
|
||||
|
||||
return addToTreeRec(root, newNode, compareFct, isDuplicate);
|
||||
@ -52,26 +52,29 @@ TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType
|
||||
{
|
||||
if ((currentNode == NULL))
|
||||
{
|
||||
// printf("Node == NULL\n");
|
||||
if (isDuplicate == NULL)
|
||||
{
|
||||
return newNode;
|
||||
}
|
||||
// dosn't consider is Duplicate != Null upon root Null
|
||||
else
|
||||
{
|
||||
return currentNode;
|
||||
}
|
||||
}
|
||||
else if ((compareFct(¤tNode->data, &newNode->data) < 0))
|
||||
else if ((compareFct(currentNode->data, newNode->data) < 0))
|
||||
{
|
||||
// printf("<0\n");
|
||||
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate);
|
||||
}
|
||||
else if ((compareFct(¤tNode->data, &newNode->data) > 0))
|
||||
else if ((compareFct(currentNode->data, newNode->data) > 0))
|
||||
{
|
||||
// printf(">0\n");
|
||||
currentNode->right = addToTreeRec(currentNode->right, newNode, compareFct, isDuplicate);
|
||||
}
|
||||
else if ((compareFct(¤tNode->data, &newNode->data) == 0))
|
||||
else if ((compareFct(currentNode->data, newNode->data) == 0))
|
||||
{
|
||||
// printf("==0\n");
|
||||
if (isDuplicate == NULL)
|
||||
{
|
||||
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate);
|
||||
@ -132,7 +135,7 @@ void clearNode(TreeNode *node)
|
||||
// printf("in clearNode\n");
|
||||
// printf("node-> data = %u\n", node->data);
|
||||
// printf("node-> data = %u\n", _ADDRESSOF(node->data));
|
||||
free(&node->data);
|
||||
free(node->data);
|
||||
node->data = NULL;
|
||||
// printf("node-> data = %u\n", &node->data);
|
||||
// printf("data freed \n");
|
||||
@ -144,8 +147,8 @@ void clearNode(TreeNode *node)
|
||||
free(node);
|
||||
// printf("node = %d\n", node);
|
||||
node = NULL;
|
||||
printf("node = %u\n", &node);
|
||||
printf("freed node\n");
|
||||
// printf("node = %u\n", &node);
|
||||
// printf("freed node\n");
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -54,24 +54,25 @@ void test_addToTreeExpandsTreeCorrectly(void)
|
||||
|
||||
// Checking the Tree without Doubles
|
||||
TEST_ASSERT_NOT_NULL(testRoot);
|
||||
TEST_ASSERT_EQUAL_UINT16(score1, testRoot->data);
|
||||
TEST_ASSERT_EQUAL(score1, *(int *)testRoot->data);
|
||||
|
||||
TEST_ASSERT_NOT_NULL(testRoot->left);
|
||||
TEST_ASSERT_EQUAL_UINT16(score2, testRoot->left->data);
|
||||
TEST_ASSERT_EQUAL(score2, *(int *)testRoot->left->data);
|
||||
TEST_ASSERT_NOT_NULL(testRoot->right);
|
||||
TEST_ASSERT_EQUAL_UINT16(score3, testRoot->right->data);
|
||||
TEST_ASSERT_EQUAL(score3, *(int *)testRoot->right->data);
|
||||
TEST_ASSERT_NOT_NULL(testRoot->left->left);
|
||||
TEST_ASSERT_EQUAL_UINT16(score4, testRoot->left->left->data);
|
||||
TEST_ASSERT_EQUAL(score4, *(int *)testRoot->left->left->data);
|
||||
TEST_ASSERT_NOT_NULL(testRoot->left->right);
|
||||
TEST_ASSERT_EQUAL_UINT16(score5, testRoot->left->right->data);
|
||||
TEST_ASSERT_EQUAL(score5, *(int *)testRoot->left->right->data);
|
||||
TEST_ASSERT_NOT_NULL(testRoot->right->left);
|
||||
TEST_ASSERT_EQUAL_UINT16(score6, testRoot->right->left->data);
|
||||
TEST_ASSERT_EQUAL(score6, *(int *)testRoot->right->left->data);
|
||||
TEST_ASSERT_NOT_NULL(testRoot->right->right);
|
||||
TEST_ASSERT_EQUAL_UINT16(score7, testRoot->right->right->data);
|
||||
|
||||
TEST_ASSERT_EQUAL(score7, *(int *)testRoot->right->right->data);
|
||||
|
||||
// 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->data);
|
||||
TEST_ASSERT_EQUAL_UINT16(score4, *(int *)testRoot->left->left->left->data);
|
||||
|
||||
// Trying to add Double while Doubles not Permitted
|
||||
testRoot = addToTree(testRoot, &score7, sizeof(int), compareIntEntries, &testIsDouble);
|
||||
@ -105,7 +106,6 @@ void test_clearTreeworksLikeExpected(void)
|
||||
int score7 = 21;
|
||||
|
||||
|
||||
// Fill Tree
|
||||
testRoot = addToTree(testRoot, &score1, sizeof(int), compareIntEntries, NULL);
|
||||
testRoot = addToTree(testRoot, &score2, sizeof(int), compareIntEntries, NULL);
|
||||
testRoot = addToTree(testRoot, &score3, sizeof(int), compareIntEntries, NULL);
|
||||
@ -113,17 +113,18 @@ void test_clearTreeworksLikeExpected(void)
|
||||
testRoot = addToTree(testRoot, &score5, sizeof(int), compareIntEntries, NULL);
|
||||
testRoot = addToTree(testRoot, &score6, sizeof(int), compareIntEntries, NULL);
|
||||
testRoot = addToTree(testRoot, &score7, sizeof(int), compareIntEntries, NULL);
|
||||
testRoot = addToTree(testRoot, &score4, sizeof(int), compareIntEntries, NULL);
|
||||
printf("Tree Filled\n");
|
||||
|
||||
// Save all Adresses
|
||||
TreeNode *node1 = testRoot->left;
|
||||
void *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);
|
||||
@ -131,11 +132,11 @@ 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->data);
|
||||
// TEST_ASSERT_NULL(node1);
|
||||
|
||||
/*
|
||||
TEST_ASSERT_NULL(node2->data);
|
||||
// TEST_ASSERT_NULL(node2);
|
||||
|
||||
@ -153,7 +154,7 @@ void test_clearTreeworksLikeExpected(void)
|
||||
|
||||
TEST_ASSERT_NULL(node7->data);
|
||||
// TEST_ASSERT_NULL(node7);
|
||||
|
||||
// */
|
||||
}
|
||||
|
||||
|
||||
@ -198,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();
|
||||
|
||||
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user