tupperware: avl: add mapping functions

This commit is contained in:
Bruno BELANYI 2020-11-24 19:44:49 +01:00
parent 564aae3d55
commit 5624e3c898
2 changed files with 59 additions and 0 deletions

View file

@ -57,4 +57,8 @@ void avl_merge(struct avl *tree, struct avl *more);
void avl_update(struct avl *tree, struct avl *more);
void avl_merge_all(struct avl *tree, struct avl *more);
void avl_prefix_map(struct avl *tree, avl_map_f map, void *cookie);
void avl_infix_map(struct avl *tree, avl_map_f map, void *cookie);
void avl_postfix_map(struct avl *tree, avl_map_f map, void *cookie);
#endif /* !TUPPERWARE_AVL_H */

View file

@ -467,3 +467,58 @@ void avl_merge_all(struct avl *tree, struct avl *more) {
avl_insert_multi(tree, v); // Keep all values
}
}
static void avl_prefix_map_helper(struct avl_node *n,
avl_map_f map, void *cookie) {
if (!n)
return;
map(n, cookie);
avl_prefix_map_helper(n->left, map, cookie);
avl_prefix_map_helper(n->right, map, cookie);
}
void avl_prefix_map(struct avl *tree, avl_map_f map, void *cookie) {
if (!tree)
return;
avl_prefix_map_helper(tree->root, map, cookie);
}
static void avl_infix_map_helper(struct avl_node *n,
avl_map_f map, void *cookie) {
if (!n)
return;
avl_infix_map_helper(n->left, map, cookie);
map(n, cookie);
avl_infix_map_helper(n->right, map, cookie);
}
void avl_infix_map(struct avl *tree, avl_map_f map, void *cookie) {
if (!tree)
return;
avl_infix_map_helper(tree->root, map, cookie);
}
static void avl_postfix_map_helper(struct avl_node *n,
avl_map_f map, void *cookie) {
if (!n)
return;
avl_postfix_map_helper(n->left, map, cookie);
avl_postfix_map_helper(n->right, map, cookie);
map(n, cookie);
}
void avl_postfix_map(struct avl *tree, avl_map_f map, void *cookie) {
if (!tree)
return;
avl_postfix_map_helper(tree->root, map, cookie);
}
struct between_parameters {
struct avl_node *end;
avl_map_f map;
void *cookie;
};