Skip to content

Commit

Permalink
common/rbtree: Add tree traversal and get root routines
Browse files Browse the repository at this point in the history
The commit adds two routines:
- tree traversal that applies user-provided handler function for each node
- get root of a tree

The routines will be used in the further commits

Signed-off-by: Dmitry Gladkov <dmitry.gladkov@intel.com>
  • Loading branch information
dmitrygx committed Sep 12, 2018
1 parent a6e33da commit 9d60747
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions include/rbtree.h
Expand Up @@ -95,5 +95,12 @@ RbtIterator rbtFindLeftmost(RbtHandle h, void *key,
RbtIterator rbtFind(RbtHandle h, void *key);
// returns iterator associated with key

void rbtTraversal(RbtHandle h, RbtIterator it, void *handler_arg,
void(*handler)(void *arg, RbtIterator it));
// tree traversal that visits (applies handler()) each node in the tree data
// strucutre exactly once.

void *rbtRoot(RbtHandle h);
// returns the root of the tree

#endif /* RBTREE_H_ */
21 changes: 21 additions & 0 deletions src/rbtree.c
Expand Up @@ -434,3 +434,24 @@ void *rbtFind(RbtHandle h, void *key) {
}
return NULL;
}

void rbtTraversal(RbtHandle h, RbtIterator it, void *handler_arg,
void(*handler)(void *arg, RbtIterator it)) {
RbtType *rbt = h;
NodeType *root = it;

// apply handler for:
// -o the root of the tree/subtree
handler(handler_arg, it);
// - the left subtree
if (root->left != SENTINEL)
rbtTraversal(h, root->left, handler_arg, handler);
// - the right subtree
if (root->right != SENTINEL)
rbtTraversal(h, root->right, handler_arg, handler);
}

void *rbtRoot(RbtHandle h) {
RbtType *rbt = h;
return rbt->root;
}

0 comments on commit 9d60747

Please sign in to comment.