1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 08:37:46 +00:00

LibC: Implement twalk

This commit is contained in:
Tim Schumacher 2021-10-14 20:46:26 +02:00 committed by Brian Gianforcaro
parent 7448626bae
commit 4b423a5ec7
3 changed files with 145 additions and 0 deletions

View file

@ -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);
}
}