Skip to content

Commit

Permalink
Maintain: Added detailed notes for preorder traversal.
Browse files Browse the repository at this point in the history
  • Loading branch information
RegGateX committed May 28, 2022
1 parent 5718484 commit d4ffcdb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
11 changes: 9 additions & 2 deletions src/rbtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -847,12 +847,20 @@ struct rb_node *rb_pre_next(const struct rb_node *node)
if (!node)
return NULL;

/**
* If there are left and right child nodes,
* then we iterate directly.
*/
if (node->left)
return node->left;

if (node->right)
return node->right;

/**
* if we have no children, Go up till we find an ancestor
* which have a another right-hand child.
*/
while ((parent = node->parent) && (!parent->right || node == parent->right))
node = parent;

Expand All @@ -870,8 +878,7 @@ struct rb_node *rb_post_first(const struct rb_root *root)
if (!root || !node)
return NULL;

node = rb_left_deep(node);
return node;
return rb_left_deep(node);
}

struct rb_node *rb_post_next(const struct rb_node *node)
Expand Down
2 changes: 1 addition & 1 deletion src/rbtree.h
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ static inline void rb_delete_augmented(struct rb_root *root, struct rb_node *nod
pos; pos = rb_next_entry(pos, member))

/**
* rb_for_each_entry_reverse - iterate backwards over cached rbtree of given type.
* rb_cached_for_each_entry_reverse - iterate backwards over cached rbtree of given type.
* @pos: the type * to use as a loop cursor.
* @cached: the cached root for your rbtree.
* @member: the name of the rb_node within the struct.
Expand Down

0 comments on commit d4ffcdb

Please sign in to comment.