diff --git a/bintree.c b/bintree.c index 8687439..298466f 100644 --- a/bintree.c +++ b/bintree.c @@ -53,14 +53,45 @@ TreeNode *addToTree(TreeNode* root, const void* data, size_t dataSize, CompareFc } -// 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, +// 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) { + static StackNode *stack = NULL; + if (root != NULL) { + clearStack(stack); + stack = push(NULL, root); + while(root->left != NULL) { + root = root->left; + stack = push(stack,root); + } + } + + if (stack == NULL) { + return NULL; + } + + TreeNode* result = (TreeNode*)top(stack); + stack = pop(stack); + root = result; + + if (root->right != NULL) { + root = root->right; + stack = push(stack,root); + while(root->left != NULL) { + root = root->left; + stack = push(stack,root); + } + } + return result->data; } + + // Releases all memory resources (including data copies). void clearTree(TreeNode *root) { diff --git a/stack.c b/stack.c index 226a36f..0a46615 100644 --- a/stack.c +++ b/stack.c @@ -8,7 +8,7 @@ * `clearStack`: gibt den gesamten Speicher frei. */ // Pushes data as pointer onto the stack. -StackNode* push(StackNode* stack, void* data) // es gibt bereits ein stack element, dieses wird übergeben + die daten, die in den stack müssen +StackNode* push(StackNode* stack, void* data) { //if(!data) //return 0; -> nicht notwendig