1 | // SPDX-License-Identifier: GPL-3.0-or-later |
---|---|
2 | |
3 | #include <mos/lib/structures/list.h> |
4 | #include <mos/lib/structures/tree.h> |
5 | #include <mos/moslib_global.h> |
6 | #include <mos_string.h> |
7 | |
8 | void tree_node_init(tree_node_t *node) |
9 | { |
10 | linked_list_init(head_node: &node->list_node); |
11 | linked_list_init(head_node: &node->children); |
12 | } |
13 | |
14 | void tree_add_child(tree_node_t *parent, tree_node_t *child) |
15 | { |
16 | MOS_LIB_ASSERT(parent != NULL); |
17 | MOS_LIB_ASSERT(child != NULL); |
18 | MOS_LIB_ASSERT(child->parent == NULL); |
19 | |
20 | child->parent = parent; |
21 | MOS_LIB_ASSERT_X(list_is_empty(&child->children), "Child node already has children"); |
22 | |
23 | linked_list_init(head_node: &child->children); |
24 | list_node_append(head: &parent->children, item: &child->list_node); |
25 | } |
26 |