simons wirre gedanken

This commit is contained in:
Simon May 2025-12-02 17:06:00 +01:00
parent a3b951d0a9
commit fc3508140a
4 changed files with 80 additions and 44 deletions

View File

@ -54,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;
}

View File

@ -1 +0,0 @@
player1;3999

View File

@ -23,6 +23,7 @@ unsigned int checkArray(unsigned int *array, unsigned int len, unsigned int numb
if (array[i] == number)
{
free = 0;
break;
}
}
@ -32,33 +33,57 @@ 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, counter;
if(array == NULL)
if (array == NULL)
{
return NULL;
return NULL; // Fehler
}
for (int i = 0; i < len; i++)
{
array[i] = 0;
}
randomNr = rand() % (2 * len + 1);
randomPos = rand() % (len);
array[randomPos] = randomNr;
do
{
randomPos = rand() % (len);
} while (array[randomPos] != 0);
array[randomPos] = randomNr;
while (!checkArray(array, len, 0))
{
counter = 0;
do
{
if (counter == 9)
if (counter == len)
{
return NULL;
}
randomPos = rand() % (len);
counter++;
} while (array[randomPos] != 0);
counter = 0;
do
{
if (counter == len * 2)
{
return NULL;
}
randomNr = rand() % (2 * len + 1);
counter++;
} while (!checkArray(array, i, randomNr));
} while (!checkArray(array, len, randomNr));
array[i] = randomNr;
printf("%u ", array[i]);
array[randomPos] = randomNr;
}
printf("\n");
return array;
}
@ -145,7 +170,7 @@ unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
{
temp[i] = numbers[i];
}
// Sorting arr using mergesort
mergeSort(temp, 0, len - 1);
@ -154,7 +179,7 @@ unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
duplicate = temp[i];
if (duplicate == temp[i + 1])
{
break;
}
}

View File

@ -4,38 +4,57 @@
#include "unity.h"
void treeTest()
void sizeTest()
{
TreeNode root;
TreeNode node1;
TreeNode node2;
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;
root.data = &dataRoot;
root.left = &node1;
root.right = &node2;
node1->data = &dataNode1;
node1->left = NULL;
node1->right = NULL;
node1.data = &dataNode1;
node1.left = NULL;
node1.right = NULL;
node2->data = &dataNode2;
node2->left = NULL;
node2->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;
TEST_ASSERT_EQUAL_INT(3,treeSize(&root));
root->data = &dataRoot;
root->left = (TreeNode *)node1;
root->right = (TreeNode *)node2;
node1->data = &dataNode1;
node1->left = NULL;
node1->right = NULL;
clearTree(&root);
TEST_ASSERT_EQUAL_INT(0,treeSize(&root));
node2->data = &dataNode2;
node2->left = NULL;
node2->right = NULL;
TreeNode *ptr = root;
clearTree(ptr);
TEST_ASSERT_EQUAL_INT(0,treeSize(root));
}
void setUp(void)
@ -54,7 +73,8 @@ int main()
printf("============================\nNumbers tests\n============================\n");
RUN_TEST(treeTest);
RUN_TEST(sizeTest);
RUN_TEST(clearTest);
return UNITY_END();
}