freeing node->data works, freeing node itself does not

This commit is contained in:
Jonas Hofmann 2025-12-09 08:09:59 +01:00
parent 14fa122f20
commit f7549910eb
4 changed files with 46 additions and 15 deletions

View File

@ -1,5 +1,6 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "stack.h"
#include "bintree.h"
@ -108,22 +109,32 @@ void clearTreeRec(TreeNode *currentNode)
if (currentNode != NULL)
{
clearTree(currentNode->left);
clearNode(currentNode);
clearTree(currentNode->right);
clearNode(currentNode);
}
}
void clearNode(TreeNode *node)
{
free(node->data);
// printf("in clearNode\n");
printf("node-> data = %u\n", &node->data);
// printf("node-> data = %u\n", _ADDRESSOF(node->data));
free(&node->data);
node->data = NULL;
printf("node-> data = %u\n", &node->data);
// printf("data freed \n");
node->left = NULL;
node->right = NULL;
free(node);
// printf("left & right = Null\n");
printf("node = %u\n", &node);
// printf("node = %u\n", _ADDRESSOF(node));
// free(node);
// printf("node = %d\n", node);
node = NULL;
printf("freed node\n");
}

BIN
bintree.o

Binary file not shown.

View File

@ -1,5 +1,6 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "unity.h"
#include "bintree.h"
@ -113,6 +114,7 @@ void test_clearTreeworksLikeExpected(void)
testRoot = addToTree(testRoot, &score6, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score7, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score4, sizeof(int), compareIntEntries, NULL);
printf("Tree Filled\n");
// Save all Adresses
TreeNode *node1 = testRoot->left;
@ -122,18 +124,36 @@ void test_clearTreeworksLikeExpected(void)
TreeNode *node5 = testRoot->right->left;
TreeNode *node6 = testRoot->right->right;
TreeNode *node7 = testRoot->left->left->left;
printf("Adresses Saved\n");
clearTree(testRoot);
printf("Tree Cleared\n");
// Check if everything has been set to NULL
TEST_ASSERT_NULL(testRoot);
TEST_ASSERT_NULL(node1);
TEST_ASSERT_NULL(node2);
TEST_ASSERT_NULL(node3);
TEST_ASSERT_NULL(node4);
TEST_ASSERT_NULL(node5);
TEST_ASSERT_NULL(node6);
TEST_ASSERT_NULL(node7);
TEST_ASSERT_NULL(testRoot->data);
// TEST_ASSERT_NULL(testRoot);
TEST_ASSERT_NULL(node1->data);
// TEST_ASSERT_NULL(node1);
TEST_ASSERT_NULL(node2->data);
// TEST_ASSERT_NULL(node2);
TEST_ASSERT_NULL(node3->data);
// TEST_ASSERT_NULL(node3);
TEST_ASSERT_NULL(node4->data);
// TEST_ASSERT_NULL(node4);
TEST_ASSERT_NULL(node5->data);
// TEST_ASSERT_NULL(node5);
TEST_ASSERT_NULL(node6->data);
// TEST_ASSERT_NULL(node6);
TEST_ASSERT_NULL(node7->data);
// TEST_ASSERT_NULL(node7);
}
@ -178,9 +198,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.