addToTree corrected

This commit is contained in:
Jonas Hofmann 2025-12-09 11:19:56 +01:00
parent 72f1d080a0
commit 144648886f
4 changed files with 13 additions and 13 deletions

View File

@ -21,7 +21,7 @@
unsigned int treeSize (const TreeNode *root);
// 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 clearNode (TreeNode *node);
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);
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))
{
// printf("Node == NULL\n");
if (isDuplicate == NULL)
if ((isDuplicate == NULL) || root)
{
return newNode;
}
@ -65,19 +65,19 @@ TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType
else if ((compareFct(currentNode->data, newNode->data) < 0))
{
// 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))
{
// 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))
{
// printf("==0\n");
if (isDuplicate == NULL)
{
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate);
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate, 0);
}
else
{

BIN
bintree.o

Binary file not shown.

View File

@ -116,15 +116,15 @@ void test_clearTreeworksLikeExpected(void)
printf("Tree Filled\n");
// Save all Adresses
void *node1 = testRoot->left;
/*
TreeNode *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);
@ -132,7 +132,7 @@ 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);
@ -199,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.