treeSize and treeSize test now workingW

This commit is contained in:
Jonas Hofmann 2025-12-09 18:41:02 +01:00
parent 21aa67b165
commit 9ee0f9e281
4 changed files with 11 additions and 8 deletions

View File

@ -24,7 +24,7 @@
TreeNode *addToTreeRec (TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate, const int root); 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); int treeSizeRec (const TreeNode *currentNode);
// Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates // Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates
@ -134,19 +134,23 @@ unsigned int treeSize(const TreeNode *root)
unsigned int amountOfNodes = 0; unsigned int amountOfNodes = 0;
treeSizeRec(root, &amountOfNodes); amountOfNodes = treeSizeRec(root);
return amountOfNodes; return amountOfNodes;
} }
void treeSizeRec(const TreeNode *currentNode, unsigned int *nodeCount)
int treeSizeRec(const TreeNode *currentNode)
{ {
int nodeCount = 0;
if (currentNode != NULL) if (currentNode != NULL)
{ {
treeSizeRec(currentNode->left, nodeCount); nodeCount += treeSizeRec(currentNode->left);
*nodeCount++; nodeCount += treeSizeRec(currentNode->right);
treeSizeRec(currentNode->right, nodeCount); return nodeCount + 1;
} }
} }

BIN
bintree.o

Binary file not shown.

View File

@ -163,7 +163,6 @@ void test_treeSizeWorkingLikeExpected(void)
testRoot = addToTree(testRoot, &score5, sizeof(int), compareIntEntries, NULL); testRoot = addToTree(testRoot, &score5, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score6, sizeof(int), compareIntEntries, NULL); testRoot = addToTree(testRoot, &score6, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score7, sizeof(int), compareIntEntries, NULL); testRoot = addToTree(testRoot, &score7, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score4, sizeof(int), compareIntEntries, NULL);
testTreeSize = treeSize(testRoot); testTreeSize = treeSize(testRoot);
@ -182,7 +181,7 @@ int main()
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.