X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=src%2FTree.c;h=63193a224e004b9105960a72bb849f82cd42b469;hb=HEAD;hp=83262c407cf2c328bad05ef0ceeb2ff923ef615d;hpb=e45132acdb58c076d5e06849fa51c26de9a7486d;p=cgds.git diff --git a/src/Tree.c b/src/Tree.c index 83262c4..63193a2 100644 --- a/src/Tree.c +++ b/src/Tree.c @@ -149,7 +149,7 @@ void _tree_set(Tree* tree, TreeNode* treeNode, void* data) memcpy(treeNode->data, data, tree->dataSize); } -void _tree_add_child(Tree* tree, TreeNode* treeNode, void* data) +TreeNode* _tree_add_child(Tree* tree, TreeNode* treeNode, void* data) { TreeNode* newChildNode = (TreeNode*) safe_malloc(sizeof (TreeNode)); newChildNode->data = safe_malloc(tree->dataSize); @@ -165,9 +165,10 @@ void _tree_add_child(Tree* tree, TreeNode* treeNode, void* data) newChildNode->firstChild = NULL; newChildNode->lastChild = NULL; tree->size++; + return newChildNode; } -void _tree_add_sibling(Tree* tree, TreeNode* treeNode, void* data) +TreeNode* _tree_add_sibling(Tree* tree, TreeNode* treeNode, void* data) { TreeNode* newSiblingNode = (TreeNode*) safe_malloc(sizeof (TreeNode)); newSiblingNode->data = safe_malloc(tree->dataSize); @@ -181,6 +182,7 @@ void _tree_add_sibling(Tree* tree, TreeNode* treeNode, void* data) newSiblingNode->firstChild = NULL; newSiblingNode->lastChild = NULL; tree->size++; + return newSiblingNode; } void _tree_remove_rekursiv(Tree* tree, TreeNode* treeNode) @@ -273,47 +275,49 @@ void treeI_move_next(TreeIterator* treeI) TreeIteratorMode mode = treeI->mode; switch (mode) { - case IN_DEPTH: - if (!tree_is_leaf(treeI->current)) - { - // easy case: just descend deeper in the tree - treeI->current = treeI->current->firstChild; - return; - } - // leaf: while no next sibling is available, move up - while (treeI->current != NULL && treeI->current->next == NULL) - treeI->current = treeI->current->parent; - if (treeI->current != NULL) - // run goes on from next sibling - treeI->current = treeI->current->next; - break; - case IN_BREADTH: - if (treeI->current->next != NULL) - { - // easy case : just move to the next sibling - treeI->current = treeI->current->next; - return; - } - // try to go to next "cousin" on same level - if (treeI->current->parent != NULL && treeI->current->parent->next != NULL) - { - TreeNode* treeNodeParent = treeI->current->parent->next; - while (treeNodeParent != NULL && tree_is_leaf(treeNodeParent)) - treeNodeParent = treeNodeParent->next; - if (treeNodeParent != NULL) + case IN_DEPTH: + if (!tree_is_leaf(treeI->current)) { - treeI->current = treeNodeParent->firstChild; + // easy case: just descend deeper in the tree + treeI->current = treeI->current->firstChild; return; } - } - // try to go to next level - while (treeI->current->prev != NULL) - treeI->current = treeI->current->prev; - while (treeI->current != NULL && tree_is_leaf(treeI->current)) - treeI->current = treeI->current->next; - if (treeI->current != NULL) - treeI->current = treeI->current->firstChild; - break; + // leaf: while no next sibling is available, move up + while (treeI->current != NULL && treeI->current->next == NULL) + treeI->current = treeI->current->parent; + if (treeI->current != NULL) + // run goes on from next sibling + treeI->current = treeI->current->next; + break; + case IN_BREADTH: + if (treeI->current->next != NULL) + { + // easy case : just move to the next sibling + treeI->current = treeI->current->next; + return; + } + // try to go to next "cousin" on same level + if ( + treeI->current->parent != NULL && + treeI->current->parent->next != NULL + ) { + TreeNode* treeNodeParent = treeI->current->parent->next; + while (treeNodeParent != NULL && tree_is_leaf(treeNodeParent)) + treeNodeParent = treeNodeParent->next; + if (treeNodeParent != NULL) + { + treeI->current = treeNodeParent->firstChild; + return; + } + } + // try to go to next level + while (treeI->current->prev != NULL) + treeI->current = treeI->current->prev; + while (treeI->current != NULL && tree_is_leaf(treeI->current)) + treeI->current = treeI->current->next; + if (treeI->current != NULL) + treeI->current = treeI->current->firstChild; + break; } }