pub struct Tree<T> { /* private fields */ }Expand description
Rooted multi-way tree with O(1) id lookup.
Every node has a unique auto-incrementing u64 id (root = 0).
An internal HashMap<u64, Vec<usize>> maps each id to its path
(sequence of child indices from the root), so find and
find_path_to are O(1) amortised.
Implementations§
Source§impl<T> Tree<T>
impl<T> Tree<T>
Sourcepub fn push_child(
&mut self,
parent_id: u64,
value: T,
) -> Result<u64, ParentNotFound>
pub fn push_child( &mut self, parent_id: u64, value: T, ) -> Result<u64, ParentNotFound>
Append a child to the node identified by parent_id.
Returns the newly assigned id, or ParentNotFound when
parent_id does not exist in the tree.
Sourcepub fn find_mut(&mut self, id: u64) -> Option<&mut Node<T>>
pub fn find_mut(&mut self, id: u64) -> Option<&mut Node<T>>
Mutable look up by id. O(1) amortised.
Sourcepub fn find_path_to(&self, id: u64) -> Option<Vec<usize>>
pub fn find_path_to(&self, id: u64) -> Option<Vec<usize>>
Return the index path from the root to the given id. O(1) amortised.
Sourcepub fn remove_subtree(&mut self, id: u64) -> Option<Node<T>>
pub fn remove_subtree(&mut self, id: u64) -> Option<Node<T>>
Remove a subtree rooted at id and return its root node.
Returns None when id is the tree root (cannot remove) or does not
exist. Remaining sibling indices are compacted and the internal index
is rebuilt.
Sourcepub fn prune(&mut self, policy: PrunePolicy)
pub fn prune(&mut self, policy: PrunePolicy)
Apply a pruning policy to the entire tree.
The internal index is rebuilt after pruning. No nodes are protected:
if you need to preserve a specific path (e.g. the current cursor),
use CursoredTree::prune which automatically repairs the cursor.