Added nextTreeData and iterateThroughTree in bintree.c

This commit is contained in:
Tobias Grampp 2025-12-03 12:35:38 +01:00
parent 00df653018
commit b857e6b273
2 changed files with 19 additions and 1 deletions

View File

@ -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).

View File

@ -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