diff --git a/bintree.c b/bintree.c index 0f2f187..2cf83f8 100644 --- a/bintree.c +++ b/bintree.c @@ -24,7 +24,7 @@ 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); + int treeSizeRec (const TreeNode *currentNode); // 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; - treeSizeRec(root, &amountOfNodes); + amountOfNodes = treeSizeRec(root); return amountOfNodes; } -void treeSizeRec(const TreeNode *currentNode, unsigned int *nodeCount) + +int treeSizeRec(const TreeNode *currentNode) { + int nodeCount = 0; + + if (currentNode != NULL) { - treeSizeRec(currentNode->left, nodeCount); - *nodeCount++; - treeSizeRec(currentNode->right, nodeCount); + nodeCount += treeSizeRec(currentNode->left); + nodeCount += treeSizeRec(currentNode->right); + return nodeCount + 1; } } diff --git a/bintree.o b/bintree.o index 96c8f39..48f3a9c 100644 Binary files a/bintree.o and b/bintree.o differ diff --git a/bintreeTests.c b/bintreeTests.c index eddc237..c187aaf 100644 --- a/bintreeTests.c +++ b/bintreeTests.c @@ -163,7 +163,6 @@ void test_treeSizeWorkingLikeExpected(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); testTreeSize = treeSize(testRoot); @@ -182,7 +181,7 @@ int main() RUN_TEST(test_addToTreeExpandsTreeCorrectly); // RUN_TEST(test_nextTreeDataReturnsNextDataCorrectly); RUN_TEST(test_clearTreeworksLikeExpected); - // RUN_TEST(test_treeSizeWorkingLikeExpected); + RUN_TEST(test_treeSizeWorkingLikeExpected); return UNITY_END(); } diff --git a/runbintreeTests.exe b/runbintreeTests.exe index 13bdb19..6e09e24 100644 Binary files a/runbintreeTests.exe and b/runbintreeTests.exe differ