Compare commits

..

No commits in common. "1ad200b828e57f4038181947a683f95c8679b0fb" and "ad395ccf6673e6124c7ae43d7f33692bdd38ff55" have entirely different histories.

View File

@ -51,34 +51,10 @@ 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. // 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, // 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.
// Initialisiert den Stack mit der Wurzel und allen linken Nachfolgern void *nextTreeData(TreeNode *root)
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). // Releases all memory resources (including data copies).
void clearTree(TreeNode *root) void clearTree(TreeNode *root)
@ -99,9 +75,5 @@ void clearTree(TreeNode *root)
// Returns the number of entries in the tree given by root. // Returns the number of entries in the tree given by root.
unsigned int treeSize(const TreeNode *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);
} }