fixed bintree.c to utilize stack.c

This commit is contained in:
Jonas Stamm 2025-12-14 22:02:23 +01:00
parent 75d01e630a
commit cd4f74f9a5
2 changed files with 36 additions and 102 deletions

130
bintree.c
View File

@ -2,72 +2,47 @@
#include "stack.h" #include "stack.h"
#include "bintree.h" #include "bintree.h"
//TODO: binären Suchbaum implementieren
/* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv),
* `clearTree`: gibt den gesamten Baum frei (rekursiv),
* `treeSize`: zählt die Knoten im Baum (rekursiv),
* `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. */
// Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates // Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates
// 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)
{ {
// Teil 1: Trivialfall (Einfügen des neuen Knotens)
if (root == NULL) if (root == NULL)
{ {
// Speicher für den Knoten selbst reservieren
TreeNode *newNode = (TreeNode *)malloc(sizeof(TreeNode)); TreeNode *newNode = (TreeNode *)malloc(sizeof(TreeNode));
if (newNode == NULL) if (newNode == NULL)
{ return NULL;
return NULL; // Fehler beim Allokieren
}
// Speicher für die Datenkopie reservieren
newNode->data = malloc(dataSize); newNode->data = malloc(dataSize);
if (newNode->data == NULL) if (newNode->data == NULL)
{ {
free(newNode); free(newNode);
return NULL; // Fehler beim Allokieren return NULL;
} }
// Daten kopieren
memcpy(newNode->data, data, dataSize); memcpy(newNode->data, data, dataSize);
// Initialisieren
newNode->left = NULL; newNode->left = NULL;
newNode->right = NULL; newNode->right = NULL;
// Flag setzen und Knoten zurückgeben
if (isDuplicate != NULL) *isDuplicate = 0; if (isDuplicate != NULL) *isDuplicate = 0;
return newNode; return newNode;
} }
// Teil 2: Rekursiver Fall (Vergleich)
int comparison = compareFct(data, root->data); int comparison = compareFct(data, root->data);
if (comparison == 0) if (comparison == 0)
{ {
// Duplikat gefunden
if (isDuplicate != NULL) *isDuplicate = 1; if (isDuplicate != NULL) *isDuplicate = 1;
// Duplikate werden akzeptiert, wenn isDuplicate == NULL (siehe bintree.h)
// Da wir aber in createNumbers Duplikate vermeiden wollen, geben wir hier einfach root zurück.
// Wenn Duplikate erlaubt sind, könntest du hier einen zweiten Knoten einfügen,
// aber standardmäßig überspringen wir Duplikate, wenn isDuplicate gesetzt ist.
return root; return root;
} }
else if (comparison < 0) else if (comparison < 0)
{ {
// Wert ist kleiner -> gehe nach links
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate); root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
} }
else // comparison > 0 else
{ {
// Wert ist größer -> gehe nach rechts
root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate); root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
} }
// 3. Wenn die Rekursion zurückkehrt, wird der aktuelle root-Pointer zurückgegeben.
return root; return root;
} }
@ -76,81 +51,53 @@ 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)
{ {
/* static iterator state */ /* static iterator state using stack.c */
static TreeNode **stack = NULL; static StackNode *stack = NULL;
static int top = -1;
static int capacity = 0;
static TreeNode *currentRoot = NULL; static TreeNode *currentRoot = NULL;
/* helper: ensure capacity */
if (capacity == 0) {
capacity = 16;
stack = (TreeNode**)malloc(capacity * sizeof(TreeNode*));
if (stack == NULL) { capacity = 0; return NULL; }
}
/* helper: push one node */
#define PUSH_NODE(n) do { \
if (top + 1 >= capacity) { \
int newcap = capacity * 2; \
TreeNode **tmp = (TreeNode**)realloc(stack, newcap * sizeof(TreeNode*)); \
if (tmp == NULL) { /* allocation failed, keep old stack */ break; } \
stack = tmp; capacity = newcap; \
} \
stack[++top] = (n); \
} while(0)
/* helper: push node and all left descendants */ /* helper: push node and all left descendants */
void push_lefts(TreeNode *n) { void push_lefts(TreeNode *n)
while (n != NULL) { {
PUSH_NODE(n); while (n != NULL)
{
stack = push(stack, (void *)n);
n = n->left; n = n->left;
} }
} }
/* If a new root is provided and differs from current, reset iterator */ /* If a new root is provided and differs from current, or explicit restart with same root, reset iterator */
if (root != NULL && root != currentRoot) { if (root != NULL && root != currentRoot)
free(stack); {
clearStack(stack);
stack = NULL; stack = NULL;
top = -1;
capacity = 0;
currentRoot = root; currentRoot = root;
/* reinitialize stack */
capacity = 16;
stack = (TreeNode**)malloc(capacity * sizeof(TreeNode*));
if (stack == NULL) { capacity = 0; return NULL; }
top = -1;
push_lefts(root); push_lefts(root);
} else if (root != NULL && root == currentRoot) { }
/* explicit restart with same root: reset and push again */ else if (root != NULL && root == currentRoot)
free(stack); {
/* explicit restart with same root */
clearStack(stack);
stack = NULL; stack = NULL;
top = -1;
capacity = 0;
capacity = 16;
stack = (TreeNode**)malloc(capacity * sizeof(TreeNode*));
if (stack == NULL) { capacity = 0; return NULL; }
top = -1;
push_lefts(root); push_lefts(root);
} }
/* if root == NULL: continue with existing stack */ /* if root == NULL: continue with existing stack */
/* nothing left */ /* nothing left */
if (top < 0) { if (stack == NULL)
free(stack); {
stack = NULL;
capacity = 0;
currentRoot = NULL; currentRoot = NULL;
return NULL; return NULL;
} }
/* pop top */ /* pop top */
TreeNode *node = stack[top--]; TreeNode *node = (TreeNode *)top(stack);
stack = pop(stack);
void *result = node->data; void *result = node->data;
/* if right child exists, push it and all its lefts */ /* if right child exists, push it and all its lefts */
if (node->right != NULL) { if (node->right != NULL)
{
push_lefts(node->right); push_lefts(node->right);
} }
@ -160,38 +107,23 @@ void *nextTreeData(TreeNode *root)
// Releases all memory resources (including data copies). // Releases all memory resources (including data copies).
void clearTree(TreeNode *root) void clearTree(TreeNode *root)
{ {
// 1. Basis-Fall: Wenn der Knoten NULL ist, beende
if (root == NULL) if (root == NULL)
{
return; return;
}
// 2. Rekursiver Schritt (gehe in die Tiefe)
// Gib den linken Teilbaum frei
clearTree(root->left); clearTree(root->left);
// Gib den rechten Teilbaum frei
clearTree(root->right); clearTree(root->right);
// 3. Aktion: Gebe die Ressourcen dieses Knotens frei
// Zuerst die dynamisch kopierten Daten freigeben (siehe addToTree)
if (root->data != NULL) if (root->data != NULL)
{ free(root->data);
free(root->data); // Speicher für die Zahl freigeben
}
// Dann den Knoten selbst freigeben
free(root); free(root);
} }
// Returns the number of entries in the tree given by root. // Returns the number of entries in the tree given by root.
unsigned int treeSize(const TreeNode *root) unsigned int treeSize(const TreeNode *root)
{ {
if (root == NULL) { //0 for end if (root == NULL)
return 0; return 0u;
}
return 1u + treeSize(root->left) + treeSize(root->right); //using 1u to insure unsigned return 1u + treeSize(root->left) + treeSize(root->right);
//recursively adds entries in the tree after root to left and right
} }

View File

@ -32,6 +32,8 @@ doble : main.o $(program_obj_files)
$(program_obj_filesobj_files): %.o: %.c $(program_obj_filesobj_files): %.o: %.c
$(CC) -c $(FLAGS) $^ -o $@ $(CC) -c $(FLAGS) $^ -o $@
#Ab hier redundant -------------------
main.o: main.c main.o: main.c
$(CC) -c $(FLAGS) main.c $(CC) -c $(FLAGS) main.c