Kommentare eingefügt

This commit is contained in:
Nicolas Reckert 2025-12-15 20:41:41 +01:00
parent 5ed4a367c8
commit 25c5a9ea5f
9 changed files with 33 additions and 21 deletions

View File

@ -3,18 +3,20 @@
#include "bintree.h" #include "bintree.h"
#include "stack.h" #include "stack.h"
/* Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates /* Fügt eine Kopie der Daten in den Baum ein, geordnet nach compareFct. Akzeptiert Duplikate,
if isDuplicate is NULL, otherwise ignores duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added). */ wenn isDuplicate NULL ist, andernfalls ignoriert Duplikate und setzt isDuplicate auf 1 (oder auf 0 bei neuem Eintrag). */
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)
{ {
// Überprüfe ungültige Eingabeparameter
if (compareFct == NULL || data == NULL || dataSize == 0) if (compareFct == NULL || data == NULL || dataSize == 0)
return root; // invalid input: do nothing return root; // ungültige Eingabe: nichts tun
// Wenn der Baum leer ist, erstelle einen neuen Wurzelknoten
if (root == NULL) if (root == NULL)
{ {
TreeNode *node = (TreeNode *)malloc(sizeof(TreeNode)); TreeNode *node = (TreeNode *)malloc(sizeof(TreeNode));
if (node == NULL) if (node == NULL)
return NULL; // allocation failed return NULL; // Speicherallokation fehlgeschlagen
node->data = malloc(dataSize); node->data = malloc(dataSize);
if (node->data == NULL) if (node->data == NULL)
@ -32,25 +34,29 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
return node; return node;
} }
// Vergleiche neue Daten mit aktueller Wurzel
int cmp = compareFct(data, root->data); int cmp = compareFct(data, root->data);
// Wenn neue Daten kleiner sind, füge in linken Unterbaum ein
if (cmp < 0) if (cmp < 0)
{ {
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate); root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
} }
// Wenn neue Daten größer sind, füge in rechten Unterbaum ein
else if (cmp > 0) else if (cmp > 0)
{ {
root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate); root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
} }
else // cmp == 0 -> duplicate // Wenn gleich (Duplikat)
else
{ {
// Wenn Duplikate erkannt werden sollen, setze Flag und ignoriere
if (isDuplicate != NULL) if (isDuplicate != NULL)
{ {
*isDuplicate = 1; *isDuplicate = 1;
// ignore duplicate insertion
} }
// Andernfalls erlaube Duplikate durch Einfügen in rechten Unterbaum
else else
{ {
// duplicates allowed: insert to right subtree for stability
root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate); root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
} }
} }
@ -58,23 +64,23 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
return root; return root;
} }
/* Iterates over the tree given by root in-order (ascending order). /* Iteriert über den Baum in aufsteigender Reihenfolge (in-order).
Follows the usage of strtok: If root != NULL then create/reset iterator for that tree. Verwendet die Logik von strtok: Wenn root != NULL, initialisiere/reset Iterator für diesen Baum.
If root == NULL, continue iteration from last position. Wenn root == NULL, setze Iteration von letzter Position fort.
Uses stack to manage traversal state. */ Verwendet Stack zur Verwaltung des Traversierungs-Zustands. */
void *nextTreeData(TreeNode *root) void *nextTreeData(TreeNode *root)
{ {
// static iterator state // Statischer Stack zur Aufrechterhaltung des Iterator-Zustands zwischen Aufrufen
static StackNode *iterStack = NULL; static StackNode *iterStack = NULL;
// initialize iterator for a new tree // Wenn ein neuer Baum bereitgestellt wird, initialisiere den Iterator
if (root != NULL) if (root != NULL)
{ {
// clear any previous iterator state // Lösche vorherigen Iterator-Zustand
clearStack(iterStack); clearStack(iterStack);
iterStack = NULL; iterStack = NULL;
// push root and all its left descendants // Pushe die Wurzel und alle linken Nachfahren auf den Stack
TreeNode *cur = root; TreeNode *cur = root;
while (cur != NULL) while (cur != NULL)
{ {
@ -84,20 +90,20 @@ void *nextTreeData(TreeNode *root)
} }
else else
{ {
// if user asks to continue but iterator not initialized, nothing to return // Wenn Iteration fortgesetzt wird, aber kein Stack initialisiert, gib NULL zurück
if (iterStack == NULL) if (iterStack == NULL)
return NULL; return NULL;
} }
// get next node // Wenn Stack leer ist, keine weiteren Elemente
if (iterStack == NULL) if (iterStack == NULL)
return NULL; return NULL;
// pop the top node // Poppe den nächsten Knoten vom Stack (in-order-Traversierung)
TreeNode *node = (TreeNode *)top(iterStack); TreeNode *node = (TreeNode *)top(iterStack);
iterStack = pop(iterStack); iterStack = pop(iterStack);
// after popping node, push its right child and all left descendants of that right child // Pushe den rechten Unterbaum des aktuellen Knotens und seine linken Nachfahren
TreeNode *r = node->right; TreeNode *r = node->right;
while (r != NULL) while (r != NULL)
{ {
@ -108,26 +114,31 @@ void *nextTreeData(TreeNode *root)
return node->data; return node->data;
} }
/* Releases all memory resources (including data copies). */ /* Gibt alle Speicherressourcen frei (einschließlich Datenkopien). */
void clearTree(TreeNode *root) void clearTree(TreeNode *root)
{ {
// Basisfall: wenn Baum leer, nichts tun
if (root == NULL) if (root == NULL)
return; return;
// Rekursiv linken und rechten Unterbaum löschen
if (root->left != NULL) if (root->left != NULL)
clearTree(root->left); clearTree(root->left);
if (root->right != NULL) if (root->right != NULL)
clearTree(root->right); clearTree(root->right);
// Daten und Knoten selbst freigeben
free(root->data); free(root->data);
root->data = NULL; root->data = NULL;
free(root); free(root);
} }
/* Returns the number of entries in the tree given by root. */ /* Gibt die Anzahl der Einträge im Baum zurück. */
unsigned int treeSize(const TreeNode *root) unsigned int treeSize(const TreeNode *root)
{ {
// Basisfall: leerer Baum hat Größe 0
if (root == NULL) if (root == NULL)
return 0; return 0;
// Größe ist 1 (aktueller Knoten) plus Größen der Unterbäume
return 1 + treeSize(root->left) + treeSize(root->right); return 1 + treeSize(root->left) + treeSize(root->right);
} }

BIN
Start_Windows/bintree.o Normal file

Binary file not shown.

BIN
Start_Windows/doble.exe Normal file

Binary file not shown.

BIN
Start_Windows/highscore.o Normal file

Binary file not shown.

View File

@ -1,2 +1,3 @@
Nicolas;9984
Test;4988 Test;4988
player1;3999 player1;3999

BIN
Start_Windows/main.o Normal file

Binary file not shown.

BIN
Start_Windows/numbers.o Normal file

Binary file not shown.

BIN
Start_Windows/stack.o Normal file

Binary file not shown.

BIN
Start_Windows/timer.o Normal file

Binary file not shown.