Compare commits

...

2 Commits

Author SHA1 Message Date
9ee0f9e281 treeSize and treeSize test now workingW 2025-12-09 18:41:02 +01:00
21aa67b165 removed no longer needed comments 2025-12-09 18:04:58 +01:00
4 changed files with 14 additions and 58 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
@ -52,7 +52,6 @@ TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType
{ {
if ((currentNode == NULL)) if ((currentNode == NULL))
{ {
// printf("Node == NULL\n");
if ((isDuplicate == NULL) || root) if ((isDuplicate == NULL) || root)
{ {
return newNode; return newNode;
@ -64,17 +63,14 @@ 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");
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate, 0); 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");
currentNode->right = addToTreeRec(currentNode->right, newNode, compareFct, isDuplicate, 0); 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");
if (isDuplicate == NULL) if (isDuplicate == NULL)
{ {
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate, 0); currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate, 0);
@ -113,45 +109,22 @@ void clearTreeRec(TreeNode *currentNode)
if (currentNode != NULL) if (currentNode != NULL)
{ {
clearTree(currentNode->left); clearTree(currentNode->left);
clearTree(currentNode->right); clearTree(currentNode->right);
clearNode(currentNode); clearNode(currentNode);
/*
printf("1\n");
free(&currentNode->data);
currentNode->data = NULL;
printf("2\n");
// free(currentNode);
// currentNode = NULL;
printf("3\n");
// */
} }
} }
void clearNode(TreeNode *node) void clearNode(TreeNode *node)
{ {
// printf("in clearNode\n");
// printf("node-> data = %u\n", node->data);
// printf("node-> data = %u\n", _ADDRESSOF(node->data));
// printf("data = %p\n", node->data);
free(node->data); free(node->data);
node->data = NULL; node->data = NULL;
// printf("data = %p\n", node->data);
// printf("node-> data = %u\n", &node->data);
// printf("data freed \n");
node->left = NULL; node->left = NULL;
node->right = NULL; node->right = NULL;
// printf("left & right = Null\n");
// printf("node = %p\n", node);
// printf("node = %u\n", _ADDRESSOF(node));
free(node); free(node);
// printf("node = %d\n", node);
node = NULL; node = NULL;
// printf("node = %p\n", node);
// printf("node = %u\n", &node);
// printf("freed node\n");
} }
@ -161,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

@ -11,6 +11,7 @@ void setUp(void) {
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden // Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
} }
void tearDown(void) { void tearDown(void) {
// Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden // Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
} }
@ -92,7 +93,6 @@ void test_nextTreeDataReturnsNextDataCorrectly(void)
} }
// test if clear Tree frees all node.name and node memory AND sets them to zero // test if clear Tree frees all node.name and node memory AND sets them to zero
// aditionally tests if the memoryspaces have been cleared // aditionally tests if the memoryspaces have been cleared
void test_clearTreeworksLikeExpected(void) void test_clearTreeworksLikeExpected(void)
@ -114,7 +114,6 @@ void test_clearTreeworksLikeExpected(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);
// printf("Tree Filled\n");
// Save all Adresses // Save all Adresses
TreeNode *node1 = testRoot; TreeNode *node1 = testRoot;
@ -124,37 +123,18 @@ void test_clearTreeworksLikeExpected(void)
TreeNode *node5 = testRoot->right; TreeNode *node5 = testRoot->right;
TreeNode *node6 = testRoot->right->left; TreeNode *node6 = testRoot->right->left;
TreeNode *node7 = testRoot->right->right; TreeNode *node7 = testRoot->right->right;
// printf("Adresses Saved\n");
clearTree(testRoot); clearTree(testRoot);
// printf("Tree Cleared\n");
// Check if everything has been set to NULL // Check if everything has been set to NULL
TEST_ASSERT_NULL(node1->data); TEST_ASSERT_NULL(node1->data);
//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->data);
// TEST_ASSERT_NULL(node2);
TEST_ASSERT_NULL(node3->data); TEST_ASSERT_NULL(node3->data);
// TEST_ASSERT_NULL(node3);
TEST_ASSERT_NULL(node4->data); TEST_ASSERT_NULL(node4->data);
// TEST_ASSERT_NULL(node4);
TEST_ASSERT_NULL(node5->data); TEST_ASSERT_NULL(node5->data);
// TEST_ASSERT_NULL(node5);
TEST_ASSERT_NULL(node6->data); TEST_ASSERT_NULL(node6->data);
// TEST_ASSERT_NULL(node6);
TEST_ASSERT_NULL(node7->data); TEST_ASSERT_NULL(node7->data);
// TEST_ASSERT_NULL(node7);
// */
} }
@ -183,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);
@ -202,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.