binTreeV2 nextTreeData

This commit is contained in:
Tobias Busch 2025-12-12 23:41:25 +01:00
parent f4aa2c7f57
commit 94d625fc77
2 changed files with 34 additions and 3 deletions

View File

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

View File

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