generated from freudenreichan/info2Praktikum-DobleSpiel
inplemented nextTreeData
This commit is contained in:
parent
70119f897a
commit
321b9630cc
27
bintree.c
27
bintree.c
@ -56,7 +56,34 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
|
|||||||
// push the top node and push all its left nodes.
|
// push the top node and push all its left nodes.
|
||||||
void *nextTreeData(TreeNode *root)
|
void *nextTreeData(TreeNode *root)
|
||||||
{
|
{
|
||||||
|
/* Iterator state kept across calls (strtok-style). */
|
||||||
|
static StackNode *iterStack = NULL;
|
||||||
|
|
||||||
|
/* Starting a new traversal: reset stack and seed with root's left spine. */
|
||||||
|
if (root != NULL) {
|
||||||
|
clearStack(iterStack);
|
||||||
|
iterStack = NULL;
|
||||||
|
|
||||||
|
for (TreeNode *n = root; n != NULL; n = n->left) {
|
||||||
|
iterStack = push(iterStack, n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* No more elements. */
|
||||||
|
if (iterStack == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Visit next node in-order. */
|
||||||
|
TreeNode *node = (TreeNode *)top(iterStack);
|
||||||
|
iterStack = pop(iterStack);
|
||||||
|
|
||||||
|
/* After visiting, push the left spine of the right subtree. */
|
||||||
|
for (TreeNode *n = node->right; n != NULL; n = n->left) {
|
||||||
|
iterStack = push(iterStack, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
return node->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Releases all memory resources (including data copies).
|
// Releases all memory resources (including data copies).
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user