|
bool | empty () const |
|
size_t | size () const |
|
const std::unordered_map< Key, T > & | nodes () const |
|
const std::unordered_map< Key, T > & | values () const |
|
const_root_view | roots () const |
|
root_view | roots () |
|
const T & | at (const Key &id) const |
|
T & | at (const Key &id) |
|
chain_view | ancestors (const Key &id) |
|
const_chain_view | ancestors (const Key &id) const |
|
descendant_view | descendants (const Key &id, SearchType search_type=SearchType::kBreadthFirstSearch, size_t max_depth=std::numeric_limits< size_t >::max()) |
|
const_descendant_view | descendants (const Key &id, SearchType search_type=SearchType::kBreadthFirstSearch, size_t max_depth=std::numeric_limits< size_t >::max()) const |
|
descendant_view | nodes (SearchType search_type, size_t max_depth=std::numeric_limits< size_t >::max()) |
|
const_descendant_view | nodes (SearchType search_type, size_t max_depth=std::numeric_limits< size_t >::max()) const |
|
bool | contains (const Key &id) const |
|
size_t | depth (const Key &id) const |
|
void | insert (const Key &id, const T &value) |
|
void | insert (const Key &id, T &&value) |
|
void | insert_child (const Key &id_parent, const Key &id, const T &value) |
|
void | insert_child (const Key &id_parent, const Key &id, T &&value) |
|
const std::optional< Key > & | parent (const Key &id) const |
|
child_view | children (const Key &id) |
|
const_child_view | children (const Key &id) const |
|
void | clear_parent (const Key &id) |
|
void | clear_children (const Key &id) |
|
void | set_parent (const Key &id, const Key &id_parent) |
|
void | add_child (const Key &id, const Key &id_child) |
|
bool | is_ancestor (const Key &id_ancestor, const Key &id) const |
|
bool | is_descendant (const Key &id_descendant, const Key &id) const |
|
void | printf (std::ostream &os, const std::function< std::string(const Key &key, const T &val)> &StringifyValue) const |
|
template<typename Key, typename T>
class ctrl_utils::Tree< Key, T >
Tree represented as a flat map of keys to nodes and a map from keys to parent keys.
Inserting and accessing nodes is O(1).
Removing nodes is O(M), where M is the number of the removed node's siblings.
Iterating over ancestors is O(D), where D is the number of ancestors of the given node.
Iterating over descendants is O(N), where N is the number of descendants of the given node.
template<typename Key , typename T >
descendant_view ctrl_utils::Tree< Key, T >::descendants |
( |
const Key & |
id, |
|
|
SearchType |
search_type = SearchType::kBreadthFirstSearch , |
|
|
size_t |
max_depth = std::numeric_limits<size_t>::max() |
|
) |
| |
|
inline |
Returns an iterator over all the descendants of the given node, from the given node (inclusive) to all the leaves found with depth first search.
Dereferencing the iterator returns a (key, value) pair.
Tree<int, char> tree;
tree.insert(0, 'a');
tree.insert_child(0, 1, 'b');
tree.insert_child(0, 2, 'c');
for (const std::pair<int, char>& key_val : tree.descendants(0)) {
std::cout << key_val.first << ": " << key_val.second << std::endl;
}
- Parameters
-
id | Key to start descendants search. |
search_type | Whether to use breadth first or depth first search. |
max_depth | Maximum depth to search. |
template<typename Key , typename T >
bool ctrl_utils::Tree< Key, T >::is_ancestor |
( |
const Key & |
id_ancestor, |
|
|
const Key & |
id |
|
) |
| const |
|
inline |
Checks whether id_ancestor is an ancestor of id.
Nodes are ancestors of themselves.
This function is O(D), where D is the depth of the given node.
template<typename Key , typename T >
bool ctrl_utils::Tree< Key, T >::is_descendant |
( |
const Key & |
id_descendant, |
|
|
const Key & |
id |
|
) |
| const |
|
inline |
Checks whether id_descendant is a descendant of id.
Nodes are descendants of themselves.
This function is O(D), where D is the depth of the descendant node.