generated from freudenreichan/info2Praktikum-DobleSpiel
Funktionen des Binaerbaums implementiert
This commit is contained in:
parent
be5c1754f9
commit
5859268475
52
bintree.c
52
bintree.c
@ -66,38 +66,42 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
|
|||||||
// 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;
|
static StackNode *stack = NULL;
|
||||||
|
|
||||||
// Beim ersten Aufruf: ganzen linken Pfad pushen
|
// 1) Falls neuer Baum übergeben wurde → Initialisieren
|
||||||
if(root != NULL)
|
if (root != NULL)
|
||||||
{
|
{
|
||||||
while(root != NULL)
|
// alten Stack leeren
|
||||||
{
|
while (stack != NULL)
|
||||||
stack = push(stack, root); // TreeNode auf den Stack
|
stack = pop(stack);
|
||||||
root = root->left;
|
|
||||||
}
|
// alle linken Knoten pushen
|
||||||
|
while (root != NULL) {
|
||||||
|
stack = push(stack, root);
|
||||||
|
root = root->left;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Wenn Stack leer -> fertig
|
// 2) Wenn Stack leer → fertig
|
||||||
if(stack == NULL)
|
if (stack == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
// Obersten Knoten holen
|
// 3) Top-Knoten holen
|
||||||
TreeNode *node = (TreeNode*) top(stack);
|
TreeNode *node = (TreeNode *)top(stack);
|
||||||
stack = pop(stack);
|
stack = pop(stack);
|
||||||
|
|
||||||
// Rechten Teilbaum absteigen
|
// 4) Wenn rechter Teilbaum existiert → alle linken Knoten pushen
|
||||||
TreeNode *right = node->right;
|
TreeNode *right = node->right;
|
||||||
while(right != NULL)
|
while (right != NULL) {
|
||||||
{
|
stack = push(stack, right);
|
||||||
stack = push(stack, right);
|
right = right->left;
|
||||||
right = right->left;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return node->data;
|
|
||||||
|
|
||||||
|
// 5) Daten zurückgeben
|
||||||
|
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)
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user