Author SHA1 Message Date
hallerni98888 35b09294bc fixed filling the array 2025-12-09 15:30:54 +01:00
Simon May fc3508140a simons wirre gedanken 2025-12-02 17:06:00 +01:00
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
hallerni98888 5b576589e3 Merge branch 'main' into temp 2025-12-02 14:16:05 +01:00
hallerni98888 bf0a0b3f40 ??? 2025-12-02 13:51:42 +01:00
hallerni98888 d54dd3eb6f added test_bintree 2025-12-02 13:43:25 +01:00
8 changed files with 138 additions and 33 deletions
+3
View File
@@ -6,3 +6,6 @@ runNumbersTest.exe
numbers.o
.vscode/launch.json
.vscode/settings.json
*.o
*.exe
runBintreeTest
+23 -13
View File
@@ -12,8 +12,26 @@
// if isDuplicate is NULL, otherwise ignores duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added).
TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate)
{
TreeNode newNode = {data, NULL, NULL};
if(root == NULL)
{
return &newNode;
}
if (data < root->data)
{
root->left = addToTree(root->left, data, dataSize,compareFct, isDuplicate);
}
else if(data > root->data)
{
root->right = addToTree(root->right, data, dataSize,compareFct, isDuplicate);
}
return root;
}
// Iterates over the tree given by root. Follows the usage of strtok. If tree is NULL, the next entry of the last tree given is returned in ordering direction.
@@ -36,27 +54,19 @@ void clearTree(TreeNode *root)
{
clearTree(root->left);
free(root->left);
root->left = NULL;
}
else if (root->right != NULL)
if (root->right != NULL)
{
clearTree(root->right);
free(root->right);
root->right = NULL;
}
root->data = NULL;
root = NULL;
}
// Returns the number of entries in the tree given by root.
unsigned int treeSize(const TreeNode *root)
{
int counterL, counterR = 0;
if (root->left != NULL)
{
counterL = treeSize(root->left) + 1;
}
else if (root->right != NULL)
{
counterR = treeSize(root->right) + 1;
}
return counterL + counterR;
return root == NULL ? 0 : treeSize(root->left) + treeSize(root->right) + 1;
}
-1
View File
@@ -1 +0,0 @@
player1;3999
+6
View File
@@ -44,6 +44,12 @@ unitTests: stack.o test_stack.c $(unityfolder)/unity.c
numbersTests: numbers.o test_numbers.c $(unityfolder)/unity.c
$(CC) $(FLAGS) -I$(unityfolder) -o runNumbersTest test_numbers.c numbers.o $(unityfolder)/unity.c
# --------------------------
# bintree.c Tests
# --------------------------
bintreeTests: bintree.o test_bintree.c $(unityfolder)/unity.c
$(CC) $(FLAGS) -I$(unityfolder) -o runBintreeTest test_bintree.c bintree.o $(unityfolder)/unity.c
# --------------------------
# Clean
# --------------------------
+23 -16
View File
@@ -23,6 +23,8 @@ unsigned int checkArray(unsigned int *array, unsigned int len, unsigned int numb
if (array[i] == number)
{
free = 0;
break;
}
}
@@ -32,33 +34,38 @@ unsigned int checkArray(unsigned int *array, unsigned int len, unsigned int numb
unsigned int *createNumbers(unsigned int len)
{
srand(time(NULL));
unsigned int *array = (unsigned int*)malloc(len * sizeof(unsigned int));
int randomNr, counter;
unsigned int *array = (unsigned int *)malloc(len * sizeof(unsigned int));
int randomNr, randomPos, filler;
if(array == NULL)
if (array == NULL)
{
return NULL;
return NULL; // Fehler
}
for (int i = 0; i < len; i++)
{
array[i] = 0;
}
for (int i = 0; i < len; i++)
{
counter = 0;
do
{
if (counter == 9)
array[i] = (rand() % (2 * len))+ 1;
} while (!checkArray(array, i, array[i]));
}
randomPos = rand() % len;
randomNr = array[randomPos];
filler = randomPos;
while(filler == randomPos)
{
return NULL;
filler = rand() % len;
}
array[filler] = randomNr;
randomNr = rand() % (2 * len + 1);
counter++;
} while (!checkArray(array, i, randomNr));
array[i] = randomNr;
printf("%u ", array[i]);
}
printf("\n");
return array;
}
BIN
View File
Binary file not shown.
Binary file not shown.
+80
View File
@@ -0,0 +1,80 @@
#include <stdio.h>
#include <stdlib.h>
#include "bintree.h"
#include "unity.h"
void sizeTest()
{
TreeNode *root = (TreeNode *)malloc(sizeof(TreeNode));
TreeNode *node1 = (TreeNode *)malloc(sizeof(TreeNode));
TreeNode *node2 = (TreeNode *)malloc(sizeof(TreeNode));
int dataRoot = 2;
int dataNode1 = 1;
int dataNode2 = 3;
root->data = &dataRoot;
root->left = (TreeNode *)node1;
root->right = (TreeNode *)node2;
node1->data = &dataNode1;
node1->left = NULL;
node1->right = NULL;
node2->data = &dataNode2;
node2->left = NULL;
node2->right = NULL;
TEST_ASSERT_EQUAL_INT(3,treeSize(root));
}
void clearTest()
{
TreeNode *root = (TreeNode *)malloc(sizeof(TreeNode));
TreeNode *node1 = (TreeNode *)malloc(sizeof(TreeNode));
TreeNode *node2 = (TreeNode *)malloc(sizeof(TreeNode));
int dataRoot = 2;
int dataNode1 = 1;
int dataNode2 = 3;
root->data = &dataRoot;
root->left = (TreeNode *)node1;
root->right = (TreeNode *)node2;
node1->data = &dataNode1;
node1->left = NULL;
node1->right = NULL;
node2->data = &dataNode2;
node2->left = NULL;
node2->right = NULL;
TreeNode *ptr = root;
clearTree(ptr);
TEST_ASSERT_EQUAL_INT(0,treeSize(root));
}
void setUp(void)
{
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
}
void tearDown(void)
{
// Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
}
int main()
{
UNITY_BEGIN();
printf("============================\nNumbers tests\n============================\n");
RUN_TEST(sizeTest);
RUN_TEST(clearTest);
return UNITY_END();
}