wrote nextTreeData() and nextTreeDataRec()
This commit is contained in:
parent
9ee0f9e281
commit
9d08970513
33
bintree.c
33
bintree.c
@ -13,6 +13,7 @@
|
|||||||
|
|
||||||
|
|
||||||
static TreeNode *root = NULL;
|
static TreeNode *root = NULL;
|
||||||
|
static StackNode *stackRoot = NULL;
|
||||||
|
|
||||||
|
|
||||||
TreeNode *addToTree (TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate);
|
TreeNode *addToTree (TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate);
|
||||||
@ -25,6 +26,7 @@
|
|||||||
void clearTreeRec (TreeNode *currentNode);
|
void clearTreeRec (TreeNode *currentNode);
|
||||||
void clearNode (TreeNode *node);
|
void clearNode (TreeNode *node);
|
||||||
int treeSizeRec (const TreeNode *currentNode);
|
int treeSizeRec (const TreeNode *currentNode);
|
||||||
|
void *nextTreeDataRec (TreeNode *node, StackNode *stack);
|
||||||
|
|
||||||
|
|
||||||
// Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates
|
// Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates
|
||||||
@ -91,9 +93,40 @@ TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType
|
|||||||
// push the top node and push all its left nodes.
|
// push the top node and push all its left nodes.
|
||||||
|
|
||||||
// Needs stack!!
|
// Needs stack!!
|
||||||
|
// Stack functions: push(), pop(), top(), clearStack()
|
||||||
void *nextTreeData(TreeNode *root)
|
void *nextTreeData(TreeNode *root)
|
||||||
{
|
{
|
||||||
|
void *pointerToReturn = NULL;
|
||||||
|
|
||||||
|
if (root != NULL)
|
||||||
|
{
|
||||||
|
// Add tree to stack so that bigest entry is ontop
|
||||||
|
stackRoot = nextTreeDataRec(root, stackRoot);
|
||||||
|
pointerToReturn = stackRoot;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// return current top entry and then pop that top entry
|
||||||
|
pointerToReturn = top(stackRoot);
|
||||||
|
stackRoot = pop(stackRoot);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return pointerToReturn;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void *nextTreeDataRec(TreeNode *tree, StackNode *stack)
|
||||||
|
{
|
||||||
|
if (tree != NULL)
|
||||||
|
{
|
||||||
|
stack = nextTreeDataRec (tree->left, stack);
|
||||||
|
stack = push (stack, tree->data);
|
||||||
|
stack = nextTreeDataRec (tree->right, stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return stack;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user