This commit is contained in:
Simon May 2025-12-09 17:02:55 +01:00
parent fc3508140a
commit a018971c04
5 changed files with 34 additions and 52 deletions

View File

@ -2,6 +2,7 @@
#include "stack.h" #include "stack.h"
#include "bintree.h" #include "bintree.h"
static StackNode *stack;
// 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),
@ -12,25 +13,23 @@
// 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).
TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate) TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate)
{ {
TreeNode newNode = {data, NULL, NULL}; if (root == NULL)
if(root == NULL)
{ {
return &newNode; TreeNode *newNode = malloc(sizeof(TreeNode));
newNode->data = malloc(dataSize);
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
} }
if (compareFct(root->data, data) < 0)
if (data < root->data)
{ {
root->left = addToTree(root->left, data, dataSize,compareFct, isDuplicate); root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
} }
else if(data > root->data) else if (compareFct(root->data, data) > 0)
{ {
root->right = addToTree(root->right, data, dataSize,compareFct, isDuplicate); root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
} }
return root; return root;
} }
@ -39,9 +38,11 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
// push the top node and push all its left nodes. // push the top node and push all its left nodes.
void *nextTreeData(TreeNode *root) void *nextTreeData(TreeNode *root)
{ {
return NULL;
} }
// Releases all memory resources (including data copies). // Releases all memory resources (including data copies).
void clearTree(TreeNode *root) void clearTree(TreeNode *root)
{ {
@ -49,7 +50,7 @@ void clearTree(TreeNode *root)
{ {
return; return;
} }
if (root->left != NULL) if (root->left != NULL)
{ {
clearTree(root->left); clearTree(root->left);

View File

@ -34,7 +34,8 @@ unsigned int *createNumbers(unsigned int len)
{ {
srand(time(NULL)); srand(time(NULL));
unsigned int *array = (unsigned int *)malloc(len * sizeof(unsigned int)); unsigned int *array = (unsigned int *)malloc(len * sizeof(unsigned int));
int randomNr, randomPos, counter; int randomNr, randomPos, counter, filler;
if (array == NULL) if (array == NULL)
{ {
@ -45,45 +46,25 @@ unsigned int *createNumbers(unsigned int len)
{ {
array[i] = 0; array[i] = 0;
} }
randomNr = rand() % (2 * len + 1); for (int i = 0; i < len; i++)
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 do
{ {
if (counter == len) array[i] = rand() % (2 * len) + 1;
{ } while (!checkArray(array, i, array[i]));
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, len, randomNr));
array[randomPos] = randomNr;
} }
randomPos = rand() % len;
randomNr = array[randomPos];
filler = randomPos;
while(filler == randomPos)
{
filler = rand() % len;
}
array[filler] = randomNr;
return array; return array;
} }

View File

@ -13,7 +13,7 @@ StackNode *push(StackNode *stack, void *data)
StackNode *tempNode, *newNode; StackNode *tempNode, *newNode;
newNode = malloc(sizeof(StackNode)); newNode = malloc(sizeof(StackNode));
newNode->value = *(int *)data; newNode->value = data;
newNode->next = NULL; newNode->next = NULL;
if (stack == NULL) if (stack == NULL)

View File

@ -9,7 +9,7 @@ The latest element is taken from the stack. */
//TODO: passenden Datentyp als struct anlegen //TODO: passenden Datentyp als struct anlegen
typedef struct Node { typedef struct Node {
int value; void *value;
struct Node* next; struct Node* next;
} StackNode; } StackNode;

View File

@ -25,7 +25,7 @@ void test_push(void)
TEST_ASSERT_EQUAL_INT(2, testNode->next->value); TEST_ASSERT_EQUAL_INT(2, testNode->next->value);
} }
StackNode* setup(int value, StackNode* next) { StackNode* setup(void *value, StackNode* next) {
StackNode* node = malloc(sizeof(StackNode)); // allocate memory on heap StackNode* node = malloc(sizeof(StackNode)); // allocate memory on heap
if (node == NULL) { if (node == NULL) {
perror("malloc failed"); perror("malloc failed");