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