stack funktion vervollständigt
This commit is contained in:
parent
ad395ccf66
commit
31feb026d3
35
bintree.c
35
bintree.c
@ -51,9 +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.
|
// 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.
|
||||||
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).
|
// Releases all memory resources (including data copies).
|
||||||
@ -75,5 +100,9 @@ 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);
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user