binTreeV2 nextTreeData
This commit is contained in:
parent
f4aa2c7f57
commit
94d625fc77
35
bintree.c
35
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.
|
// Iterates over the tree given by root. Follows the usage of strtok. If tree is NULL,
|
||||||
// Use your implementation of a stack to organize the iterator. Push the root node and all left nodes first. On returning the next element,
|
// 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.
|
// push the top node and push all its left nodes.
|
||||||
void *nextTreeData(TreeNode *root)
|
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).
|
// Releases all memory resources (including data copies).
|
||||||
void clearTree(TreeNode *root)
|
void clearTree(TreeNode *root)
|
||||||
{
|
{
|
||||||
|
|||||||
2
stack.c
2
stack.c
@ -8,7 +8,7 @@
|
|||||||
* `clearStack`: gibt den gesamten Speicher frei. */
|
* `clearStack`: gibt den gesamten Speicher frei. */
|
||||||
|
|
||||||
// Pushes data as pointer onto the stack.
|
// 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)
|
//if(!data)
|
||||||
//return 0; -> nicht notwendig
|
//return 0; -> nicht notwendig
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user