[tree] Add some new functions
This commit is contained in:
parent
3d916ea911
commit
9f165800b7
@ -142,3 +142,25 @@ void tree_remove(tree_t * tree, tree_node_t * node) {
|
||||
list_merge(parent->children, node->children);
|
||||
free(node);
|
||||
}
|
||||
|
||||
void tree_break_off(tree_t * tree, tree_node_t * node) {
|
||||
tree_node_t * parent = node->parent;
|
||||
if (!parent) return;
|
||||
list_delete(parent->children, list_find(parent->children, node));
|
||||
}
|
||||
|
||||
tree_node_t * tree_node_find(tree_node_t * node, void * search, tree_comparator_t comparator) {
|
||||
if (comparator(node->value,search)) {
|
||||
return node;
|
||||
}
|
||||
tree_node_t * found;
|
||||
foreach(child, node->children) {
|
||||
found = tree_node_find((tree_node_t *)child->value, search, comparator);
|
||||
if (found) return found;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
tree_node_t * tree_find(tree_t * tree, void * value, tree_comparator_t comparator) {
|
||||
return tree_node_find(tree->root, value, comparator);
|
||||
}
|
||||
|
@ -19,6 +19,8 @@ typedef struct {
|
||||
tree_node_t * root;
|
||||
} tree_t;
|
||||
|
||||
typedef uint8_t (*tree_comparator_t) (void *, void *);
|
||||
|
||||
tree_t * tree_create();
|
||||
void tree_set_root(tree_t * tree, void * value);
|
||||
void tree_node_destroy(tree_node_t * node);
|
||||
@ -31,5 +33,8 @@ tree_node_t * tree_node_find_parent(tree_node_t * haystack, tree_node_t * needle
|
||||
void tree_node_parent_remove(tree_t * tree, tree_node_t * parent, tree_node_t * node);
|
||||
void tree_node_remove(tree_t * tree, tree_node_t * node);
|
||||
void tree_remove(tree_t * tree, tree_node_t * node);
|
||||
tree_node_t * tree_find(tree_t * tree, void * value, tree_comparator_t comparator);
|
||||
void tree_break_off(tree_t * tree, tree_node_t * node);
|
||||
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user