diff --git a/bintree.c b/bintree.c index dcb6695..ac81d78 100644 --- a/bintree.c +++ b/bintree.c @@ -1,5 +1,6 @@ #include #include +#include #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"); } diff --git a/bintree.o b/bintree.o index 22e8a0e..d098a88 100644 Binary files a/bintree.o and b/bintree.o differ diff --git a/bintreeTests.c b/bintreeTests.c index 6662482..edfa642 100644 --- a/bintreeTests.c +++ b/bintreeTests.c @@ -1,5 +1,6 @@ #include #include +#include #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(); diff --git a/runbintreeTests.exe b/runbintreeTests.exe index 40861af..41bc994 100644 Binary files a/runbintreeTests.exe and b/runbintreeTests.exe differ