From 401118417b307d14cb1c9603be32e5abd396913e Mon Sep 17 00:00:00 2001 From: Dan Staples Date: Thu, 13 Nov 2014 16:11:28 -0500 Subject: [PATCH] minor fixes to tree functions --- src/tree.c | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/tree.c b/src/tree.c index 6d320ce..5293c2c 100644 --- a/src/tree.c +++ b/src/tree.c @@ -251,11 +251,7 @@ _co_tree_insert_r(_treenode_t *parent, _treenode_t *current, const char *orig_ke if (current == NULL) { current = (_treenode_t *) h_calloc(1, sizeof(_treenode_t)); - if(parent == NULL) - { - parent = current; - } - else + if(parent) { current->parent = parent; hattach(current, parent); @@ -599,7 +595,7 @@ _co_tree_raw_r(char **output, const size_t *olen, size_t *written, _treenode_t * } else if(IS_LIST(current->value)) { - vlen = co_list_raw(*output, *olen, current->value); + vlen = co_list_raw(*output, *olen - *written, current->value); CHECK(vlen > 0, "Failed to dump tree value."); } else @@ -882,17 +878,17 @@ _co_tree_next_r(_treenode_t *root, _treenode_t *current, _treenode_t *previous) *high = current->high; co_obj_t *key = current->key; - if (previous && key && (previous == parent || (previous == low && parent == root))) + if (key && ((previous && previous == parent) || (parent == root && ((!previous && !low) || previous == low)))) return key; - else if (low && (!previous || (previous && previous == parent))) + else if (low && (!previous || previous == parent)) return _co_tree_next_r(root, low, current); // go low - else if (equal && (!previous || (previous && (previous == parent || previous == low)))) + else if (equal && (!previous || previous == parent || previous == low)) return _co_tree_next_r(root, equal, current); // go equal - else if (high && previous && previous == equal) + else if (high && (!previous || previous == parent || previous == low || previous == equal)) return _co_tree_next_r(root, high, current); // go high - else if (parent) + else if (parent) return _co_tree_next_r(root, parent, current); // go up - else + else return key; // if NULL, tree walk has completed } @@ -922,4 +918,4 @@ co_tree_next(const co_obj_t *tree, co_obj_t *key) co_obj_t *ret = _co_tree_next_r(root, key_node, NULL); if (ret != key) return ret; return NULL; -} \ No newline at end of file +}