mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 16:07:45 +00:00
LibC: Implement twalk
This commit is contained in:
parent
7448626bae
commit
4b423a5ec7
3 changed files with 145 additions and 0 deletions
|
@ -89,4 +89,28 @@ void* tfind(const void* key, void* const* rootp, int (*comparator)(const void*,
|
|||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static void twalk_internal(const struct search_tree_node* node, void (*action)(const void*, VISIT, int), int depth)
|
||||
{
|
||||
if (!node)
|
||||
return;
|
||||
|
||||
if (!node->right && !node->left) {
|
||||
action(node, leaf, depth);
|
||||
return;
|
||||
}
|
||||
|
||||
action(node, preorder, depth);
|
||||
twalk_internal(node->left, action, depth + 1);
|
||||
action(node, postorder, depth);
|
||||
twalk_internal(node->right, action, depth + 1);
|
||||
action(node, endorder, depth);
|
||||
}
|
||||
|
||||
void twalk(const void* rootp, void (*action)(const void*, VISIT, int))
|
||||
{
|
||||
auto node = static_cast<const struct search_tree_node*>(rootp);
|
||||
|
||||
twalk_internal(node, action, 0);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,15 @@
|
|||
|
||||
__BEGIN_DECLS
|
||||
|
||||
typedef enum {
|
||||
preorder,
|
||||
postorder,
|
||||
endorder,
|
||||
leaf,
|
||||
} VISIT;
|
||||
|
||||
void* tsearch(const void*, void**, int (*)(const void*, const void*));
|
||||
void* tfind(const void*, void* const*, int (*)(const void*, const void*));
|
||||
void twalk(const void*, void (*)(const void*, VISIT, int));
|
||||
|
||||
__END_DECLS
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue