Compare commits

...

2 Commits

Author SHA1 Message Date
1ad200b828 stack funktion vervollständigt 2025-12-18 11:03:20 +01:00
31feb026d3 stack funktion vervollständigt 2025-12-18 10:58:48 +01:00

View File

@ -51,10 +51,34 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize,
// 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.
// 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.
void *nextTreeData(TreeNode *root)
{
// Initialisiert den Stack mit der Wurzel und allen linken Nachfolgern
StackNode* initTraversal(TreeNode* root) {
StackNode* stack = NULL;
TreeNode* current = root;
while (current != NULL) {
stack = push(stack, current);
current = current->left;
}
return stack;
}
void* nextTreeData(StackNode** stack) {
if (*stack == NULL) {
return NULL; // Traversierung beendet
// Obersten Knoten holen
TreeNode* node = (TreeNode*)top(*stack);
*stack = pop(*stack);
// Falls rechter Teilbaum existiert, diesen und alle linken Nachfolger auf den Stack legen
TreeNode* current = node->right;
while (current != NULL) {
*stack = push(*stack, current);
current = current->left;
}
return node->data;
}}
// Releases all memory resources (including data copies).
void clearTree(TreeNode *root)
@ -75,5 +99,9 @@ void clearTree(TreeNode *root)
// Returns the number of entries in the tree given by root.
unsigned int treeSize(const TreeNode *root)
{
if (root == NULL) {
return 0; // Basisfall: leerer Teilbaum
}
// Rekursiv: Größe = 1 (aktueller Knoten) + Größe des linken Teilbaums + Größe des rechten Teilbaums
return 1 + treeSize(root->left) + treeSize(root->right);
}