Compare commits

..

No commits in common. "9ee0f9e2812f47e8e7b9376447b68c7e007d2bcf" and "2d6f8689f26055ca9ff53ddeaf036d5a9acab7f4" have entirely different histories.

4 changed files with 58 additions and 14 deletions

View File

@ -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);
int treeSizeRec (const TreeNode *currentNode);
void treeSizeRec (const TreeNode *currentNode, unsigned int *nodeCount);
// Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates
@ -52,6 +52,7 @@ TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType
{
if ((currentNode == NULL))
{
// printf("Node == NULL\n");
if ((isDuplicate == NULL) || root)
{
return newNode;
@ -63,14 +64,17 @@ TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType
}
else if ((compareFct(currentNode->data, newNode->data) < 0))
{
// printf("<0\n");
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate, 0);
}
else if ((compareFct(currentNode->data, newNode->data) > 0))
{
// printf(">0\n");
currentNode->right = addToTreeRec(currentNode->right, newNode, compareFct, isDuplicate, 0);
}
else if ((compareFct(currentNode->data, newNode->data) == 0))
{
// printf("==0\n");
if (isDuplicate == NULL)
{
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate, 0);
@ -109,22 +113,45 @@ void clearTreeRec(TreeNode *currentNode)
if (currentNode != NULL)
{
clearTree(currentNode->left);
clearTree(currentNode->right);
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)
{
// 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);
node->data = NULL;
// printf("data = %p\n", node->data);
// printf("node-> data = %u\n", &node->data);
// printf("data freed \n");
node->left = NULL;
node->right = NULL;
// printf("left & right = Null\n");
// printf("node = %p\n", node);
// printf("node = %u\n", _ADDRESSOF(node));
free(node);
// printf("node = %d\n", node);
node = NULL;
// printf("node = %p\n", node);
// printf("node = %u\n", &node);
// printf("freed node\n");
}
@ -134,23 +161,19 @@ unsigned int treeSize(const TreeNode *root)
unsigned int amountOfNodes = 0;
amountOfNodes = treeSizeRec(root);
treeSizeRec(root, &amountOfNodes);
return amountOfNodes;
}
int treeSizeRec(const TreeNode *currentNode)
void treeSizeRec(const TreeNode *currentNode, unsigned int *nodeCount)
{
int nodeCount = 0;
if (currentNode != NULL)
{
nodeCount += treeSizeRec(currentNode->left);
nodeCount += treeSizeRec(currentNode->right);
return nodeCount + 1;
treeSizeRec(currentNode->left, nodeCount);
*nodeCount++;
treeSizeRec(currentNode->right, nodeCount);
}
}

BIN
bintree.o

Binary file not shown.

View File

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

Binary file not shown.