From 31feb026d39c99d17f192cf55cf1d8504584734b Mon Sep 17 00:00:00 2001 From: Moritz Hertel Date: Thu, 18 Dec 2025 10:58:48 +0100 Subject: [PATCH] =?UTF-8?q?stack=20funktion=20vervollst=C3=A4ndigt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bintree.c | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/bintree.c b/bintree.c index b1cf8f5..ddc57bb 100644 --- a/bintree.c +++ b/bintree.c @@ -51,9 +51,34 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, // Iterates over the tree given by root. Follows the usage of strtok. If tree is NULL, the next entry of the last tree given is returned in ordering direction. // Use your implementation of a stack to organize the iterator. Push the root node and all left nodes first. On returning the next element, // push the top node and push all its left nodes. -void *nextTreeData(TreeNode *root) -{ +// Initialisiert den Stack mit der Wurzel und allen linken Nachfolgern +StackNode* initTraversal(TreeNode* root) { + StackNode* stack = NULL; + TreeNode* current = root; + while (current != NULL) { + stack = push(stack, current); + current = current->left; + } + return stack; +} +void* nextTreeData(StackNode** stack) { + if (*stack == NULL) { + return NULL; // Traversierung beendet + } + + // Obersten Knoten holen + TreeNode* node = (TreeNode*)top(*stack); + *stack = pop(*stack); + + // Falls rechter Teilbaum existiert, diesen und alle linken Nachfolger auf den Stack legen + TreeNode* current = node->right; + while (current != NULL) { + *stack = push(*stack, current); + current = current->left; + } + + return node->data; } // Releases all memory resources (including data copies). @@ -75,5 +100,9 @@ void clearTree(TreeNode *root) // Returns the number of entries in the tree given by root. unsigned int treeSize(const TreeNode *root) { - + if (root == NULL) { + return 0; // Basisfall: leerer Teilbaum + } + // Rekursiv: Größe = 1 (aktueller Knoten) + Größe des linken Teilbaums + Größe des rechten Teilbaums + return 1 + treeSize(root->left) + treeSize(root->right); } \ No newline at end of file