diff --git a/bintree.c b/bintree.c index 182c509..debfc9e 100644 --- a/bintree.c +++ b/bintree.c @@ -49,7 +49,24 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc // push the top node and push all its left nodes. void *nextTreeData(TreeNode *root) { + StackNode *stackRoot = NULL;//Generates a new, empty stack + stackRoot = iterateThroughTree(root, stackRoot); //Fills the stack via the helping function iterateThroughTree + stackNode * tempBuffer = pop(stackRoot); + clearStack(stackRoot); + return tempBuffer; +} +//Function to aid nextTreeData +StackNode *iterateThroughTree(TreeNode *root, StackNode *stackRoot) +{ + if(!root) + { + return stackRoot; + } + stackRoot = iterateThroughTree(root->left, stackRoot); + stackNode = push(stackNode, root->data); + stackRoot = iterateThroughTree(root->right, stackRoot); + return stackRoot; } // Releases all memory resources (including data copies). diff --git a/bintree.h b/bintree.h index 25e16b2..db8bc8d 100644 --- a/bintree.h +++ b/bintree.h @@ -23,5 +23,6 @@ void *nextTreeData(TreeNode *root); void clearTree(TreeNode *root); // Returns the number of entries in the tree given by root. unsigned int treeSize(const TreeNode *root); - +//Function to aid nextTreeData +StackNode iterateThroughTree(TreeNode *root, StackNode stackRoot); #endif \ No newline at end of file