pub struct Tree<T> { /* private fields */ }
Expand description
A tree structure containing nodes.
Implementations§
source§impl<T> Tree<T>
impl<T> Tree<T>
sourcepub fn add_node(&mut self, data: T) -> usize
pub fn add_node(&mut self, data: T) -> usize
Adds a new node with the given data to the tree and returns its index. The nodes
added with this method will be disconnected from the tree, so use it only for the root node.
For adding children, use the add_child
method.
sourcepub fn add_child(&mut self, parent: usize, data: T) -> usize
pub fn add_child(&mut self, parent: usize, data: T) -> usize
Adds a child node with the given data to the specified parent node and returns the child’s index.
sourcepub fn add_child_to_root(&mut self, data: T) -> usize
pub fn add_child_to_root(&mut self, data: T) -> usize
Adds a child node with the given data to the root node and returns the child’s index.
sourcepub fn get(&self, index: usize) -> Option<&T>
pub fn get(&self, index: usize) -> Option<&T>
Returns a reference to the data of the node at the given index, or None
if the index is
out of bounds.
sourcepub fn get_unchecked(&self, index: usize) -> &T
pub fn get_unchecked(&self, index: usize) -> &T
Returns a reference to the data of the node at the given index without bounds checking.
sourcepub fn get_mut(&mut self, index: usize) -> Option<&mut T>
pub fn get_mut(&mut self, index: usize) -> Option<&mut T>
Returns a mutable reference to the data of the node at the given index, or None
if the
index is out of bounds.
sourcepub fn get_unchecked_mut(&mut self, index: usize) -> &mut T
pub fn get_unchecked_mut(&mut self, index: usize) -> &mut T
Returns a mutable reference to the data of the node at the given index without bounds checking.
sourcepub fn parent_index_unchecked(&self, index: usize) -> Option<usize>
pub fn parent_index_unchecked(&self, index: usize) -> Option<usize>
Returns the index of the parent node of the node at the given index, or None
if the node
has no parent.
sourcepub fn children(&self, index: usize) -> &[usize]
pub fn children(&self, index: usize) -> &[usize]
Returns a slice of the indices of the children of the node at the given index.
sourcepub fn traverse<'a, S>(
&'a self,
before_processing_children: impl FnMut(usize, &'a T, &mut S),
after_processing_the_subtree: impl FnMut(usize, &'a T, &mut S),
s: &mut S,
)
pub fn traverse<'a, S>( &'a self, before_processing_children: impl FnMut(usize, &'a T, &mut S), after_processing_the_subtree: impl FnMut(usize, &'a T, &mut S), s: &mut S, )
Walks the tree recursively, applying the given functions before and after processing the children of each node.
sourcepub fn iter(&self) -> impl Iterator<Item = (usize, &T)>
pub fn iter(&self) -> impl Iterator<Item = (usize, &T)>
Returns an iterator over the indices and data of the nodes in the tree.
source§impl<T: Send + Sync> Tree<T>
impl<T: Send + Sync> Tree<T>
sourcepub fn par_iter(&self) -> impl ParallelIterator<Item = (usize, &T)>
pub fn par_iter(&self) -> impl ParallelIterator<Item = (usize, &T)>
Returns a parallel iterator over the indices and data of the nodes in the tree.
sourcepub fn par_iter_mut(&mut self) -> impl ParallelIterator<Item = (usize, &mut T)>
pub fn par_iter_mut(&mut self) -> impl ParallelIterator<Item = (usize, &mut T)>
Returns a mutable parallel iterator over the indices and data of the nodes in the tree.
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for Tree<T>
impl<T> RefUnwindSafe for Tree<T>where
T: RefUnwindSafe,
impl<T> Send for Tree<T>where
T: Send,
impl<T> Sync for Tree<T>where
T: Sync,
impl<T> Unpin for Tree<T>where
T: Unpin,
impl<T> UnwindSafe for Tree<T>where
T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more