addToTree corrected
This commit is contained in:
parent
72f1d080a0
commit
144648886f
14
bintree.c
14
bintree.c
@ -21,7 +21,7 @@
|
|||||||
unsigned int treeSize (const TreeNode *root);
|
unsigned int treeSize (const TreeNode *root);
|
||||||
|
|
||||||
// self declared functions
|
// 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 clearTreeRec (TreeNode *currentNode);
|
||||||
void clearNode (TreeNode *node);
|
void clearNode (TreeNode *node);
|
||||||
void treeSizeRec (const TreeNode *currentNode, unsigned int *nodeCount);
|
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);
|
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))
|
if ((currentNode == NULL))
|
||||||
{
|
{
|
||||||
// printf("Node == NULL\n");
|
// printf("Node == NULL\n");
|
||||||
if (isDuplicate == NULL)
|
if ((isDuplicate == NULL) || root)
|
||||||
{
|
{
|
||||||
return newNode;
|
return newNode;
|
||||||
}
|
}
|
||||||
@ -65,19 +65,19 @@ TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType
|
|||||||
else if ((compareFct(currentNode->data, newNode->data) < 0))
|
else if ((compareFct(currentNode->data, newNode->data) < 0))
|
||||||
{
|
{
|
||||||
// printf("<0\n");
|
// 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))
|
else if ((compareFct(currentNode->data, newNode->data) > 0))
|
||||||
{
|
{
|
||||||
// printf(">0\n");
|
// 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))
|
else if ((compareFct(currentNode->data, newNode->data) == 0))
|
||||||
{
|
{
|
||||||
// printf("==0\n");
|
// printf("==0\n");
|
||||||
if (isDuplicate == NULL)
|
if (isDuplicate == NULL)
|
||||||
{
|
{
|
||||||
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate);
|
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate, 0);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@ -116,15 +116,15 @@ void test_clearTreeworksLikeExpected(void)
|
|||||||
printf("Tree Filled\n");
|
printf("Tree Filled\n");
|
||||||
|
|
||||||
// Save all Adresses
|
// Save all Adresses
|
||||||
void *node1 = testRoot->left;
|
TreeNode *node1 = testRoot->left;
|
||||||
/*
|
|
||||||
TreeNode *node2 = testRoot->left->left;
|
TreeNode *node2 = testRoot->left->left;
|
||||||
TreeNode *node3 = testRoot->left->right;
|
TreeNode *node3 = testRoot->left->right;
|
||||||
TreeNode *node4 = testRoot->right;
|
TreeNode *node4 = testRoot->right;
|
||||||
TreeNode *node5 = testRoot->right->left;
|
TreeNode *node5 = testRoot->right->left;
|
||||||
TreeNode *node6 = testRoot->right->right;
|
TreeNode *node6 = testRoot->right->right;
|
||||||
TreeNode *node7 = testRoot->left->left->left;
|
TreeNode *node7 = testRoot->left->left->left;
|
||||||
*/
|
|
||||||
printf("Adresses Saved\n");
|
printf("Adresses Saved\n");
|
||||||
|
|
||||||
clearTree(testRoot);
|
clearTree(testRoot);
|
||||||
@ -132,7 +132,7 @@ void test_clearTreeworksLikeExpected(void)
|
|||||||
|
|
||||||
// Check if everything has been set to NULL
|
// Check if everything has been set to NULL
|
||||||
TEST_ASSERT_NULL(testRoot->data);
|
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(node1);
|
||||||
@ -199,9 +199,9 @@ int main()
|
|||||||
UNITY_BEGIN();
|
UNITY_BEGIN();
|
||||||
|
|
||||||
printf("\n============================\nBinary Tree tests\n============================\n");
|
printf("\n============================\nBinary Tree tests\n============================\n");
|
||||||
RUN_TEST(test_addToTreeExpandsTreeCorrectly);
|
// RUN_TEST(test_addToTreeExpandsTreeCorrectly);
|
||||||
// RUN_TEST(test_nextTreeDataReturnsNextDataCorrectly);
|
// RUN_TEST(test_nextTreeDataReturnsNextDataCorrectly);
|
||||||
// RUN_TEST(test_clearTreeworksLikeExpected);
|
RUN_TEST(test_clearTreeworksLikeExpected);
|
||||||
// RUN_TEST(test_treeSizeWorkingLikeExpected);
|
// RUN_TEST(test_treeSizeWorkingLikeExpected);
|
||||||
|
|
||||||
return UNITY_END();
|
return UNITY_END();
|
||||||
|
|||||||
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user