2 Commits
Author SHA1 Message Date
Simon May a3b951d0a9 aufräumen 2025-12-02 14:53:46 +01:00
hallerni98888 f8c297f84d test_bintree edited 2025-12-02 14:47:52 +01:00
4 changed files with 43 additions and 13 deletions
+3
View File
@@ -6,3 +6,6 @@ runNumbersTest.exe
numbers.o numbers.o
.vscode/launch.json .vscode/launch.json
.vscode/settings.json .vscode/settings.json
*.o
*.exe
runBintreeTest
+29 -11
View File
@@ -2,11 +2,11 @@
#include "stack.h" #include "stack.h"
#include "bintree.h" #include "bintree.h"
//TODO: binären Suchbaum implementieren // TODO: binären Suchbaum implementieren
/* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv), /* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv),
* `clearTree`: gibt den gesamten Baum frei (rekursiv), * `clearTree`: gibt den gesamten Baum frei (rekursiv),
* `treeSize`: zählt die Knoten im Baum (rekursiv), * `treeSize`: zählt die Knoten im Baum (rekursiv),
* `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. */ * `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. */
// 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
// if isDuplicate is NULL, otherwise ignores duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added). // if isDuplicate is NULL, otherwise ignores duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added).
@@ -45,18 +45,36 @@ void *nextTreeData(TreeNode *root)
// Releases all memory resources (including data copies). // Releases all memory resources (including data copies).
void clearTree(TreeNode *root) void clearTree(TreeNode *root)
{ {
if (root == NULL)
{
return;
}
if (root->left != NULL)
{
clearTree(root->left);
free(root->left);
}
else if (root->right != NULL)
{
clearTree(root->right);
free(root->right);
}
root->data = NULL;
} }
// Returns the number of entries in the tree given by root. // Returns the number of entries in the tree given by root.
unsigned int treeSize(const TreeNode *root) unsigned int treeSize(const TreeNode *root)
{ {
int counter = 0; int counterL, counterR = 0;
if (root->left != NULL)
if(root != NULL)
{ {
treeSize(root->left); counterL = treeSize(root->left) + 1;
counter++;
treeSize(root->right);
} }
else if (root->right != NULL)
{
counterR = treeSize(root->right) + 1;
}
return counterL + counterR;
} }
+5
View File
@@ -136,6 +136,11 @@ unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
unsigned int temp[len]; unsigned int temp[len];
unsigned int duplicate = 0; unsigned int duplicate = 0;
/*if(numbers == NULL || (sizeof(numbers) / sizeof(typeof(numbers)) != len))
{
return 0;S
}*/
for (int i = 0; i < len; i++) for (int i = 0; i < len; i++)
{ {
temp[i] = numbers[i]; temp[i] = numbers[i];
+6 -2
View File
@@ -4,7 +4,7 @@
#include "unity.h" #include "unity.h"
void sizeTest() void treeTest()
{ {
TreeNode root; TreeNode root;
TreeNode node1; TreeNode node1;
@@ -31,6 +31,10 @@ void sizeTest()
TEST_ASSERT_EQUAL_INT(3,treeSize(&root)); TEST_ASSERT_EQUAL_INT(3,treeSize(&root));
clearTree(&root);
TEST_ASSERT_EQUAL_INT(0,treeSize(&root));
} }
@@ -50,7 +54,7 @@ int main()
printf("============================\nNumbers tests\n============================\n"); printf("============================\nNumbers tests\n============================\n");
RUN_TEST(sizeTest); RUN_TEST(treeTest);
return UNITY_END(); return UNITY_END();
} }