Compare commits

...

2 Commits

Author SHA1 Message Date
Simon May
6b3294d40b Merge branch 'simons_weg' of https://git.efi.th-nuernberg.de/gitea/hallerni98888/info2Praktikum-DobleSpiel into simons_weg 2025-12-09 17:04:14 +01:00
Simon May
a018971c04 sync 2025-12-09 17:02:55 +01:00
4 changed files with 18 additions and 17 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

@ -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");