added compareFct and fillArray

This commit is contained in:
Maximilian Ott 2025-12-13 12:14:33 +01:00
parent c47a735954
commit 5532cec3ad

View File

@ -13,6 +13,43 @@
// Returns len random numbers between 1 and 2x len in random order which are all different, except for two entries.
// Returns NULL on errors. Use your implementation of the binary search tree to check for possible duplicates while
// creating random numbers.
static int compareFct(const void *argument1, const void *argument2)
{
unsigned int x = *(const unsigned int *)argument1;
unsigned int y = *(const unsigned int *)argument2;
if (x < y) return -1;
if (x > y) return 1;
return 0;
}
static void fillArray(unsigned int *array, unsigned int len)
{
TreeNode *root = NULL;
int isDuplicate;
unsigned int value;
unsigned int i = 0;
srand(time(NULL));
while (i < len)
{
value = rand() % (2 * len) + 1;
root = addToTree(root, &value, sizeof(unsigned int), compareFct, &isDuplicate);
if (isDuplicate == 0)
{
array[i] = value;
i++;
}
// Bei Duplikat: nochmal versuchen, kein Inkrement
}
clearTree(root);
}
unsigned int *createNumbers(unsigned int len)
{
if (len < 2)
@ -43,10 +80,6 @@ unsigned int *createNumbers(unsigned int len)
//TODO: sicherstellen, dass im Array keine doppelten Zahlen vorhanden sind (Max)
position = rand() % len;
duplicate_value = array[position];