pub struct Tree<T> { /* private fields */ }Expand description
A tree structure containing multiple nodes of generic type T.
Each node in the tree is indexed by its position in the internal vector. The tree supports operations for adding, accessing, and traversing nodes.
§Example
use easy_tree::Tree;
let mut tree = Tree::new();
let root = tree.add_node("root");
let child = tree.add_child(root, "child");Implementations§
Source§impl<T> Tree<T>
impl<T> Tree<T>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new, empty tree.
§Returns
A Tree instance with no nodes.
§Example
use easy_tree::Tree;
let tree: Tree<i32> = Tree::new();Examples found in repository?
More examples
3fn main() {
4 let mut tree = Tree::new();
5 let root = tree.add_node("root");
6 let child1 = tree.add_child(root, "child1");
7 let _grandchild1 = tree.add_child(child1, "grandchild1");
8 let _child2 = tree.add_child(root, "child2");
9
10 let mut log = vec![];
11 tree.traverse(
12 |idx, data, log| log.push(format!("Visiting node {}: {}", idx, data)),
13 |idx, data, log| log.push(format!("Finished node {}: {}", idx, data)),
14 &mut log,
15 );
16
17 println!("{:?}", log);
18}Sourcepub fn add_node(&mut self, data: T) -> usize
pub fn add_node(&mut self, data: T) -> usize
Adds a new node to the tree.
This method is typically used to add a root node or a disconnected node.
§Parameters
data: The data to associate with the new node.
§Returns
The index of the newly added node.
§Example
use easy_tree::Tree;
let mut tree = Tree::new();
let root = tree.add_node("root");Examples found in repository?
More examples
3fn main() {
4 let mut tree = Tree::new();
5 let root = tree.add_node("root");
6 let child1 = tree.add_child(root, "child1");
7 let _grandchild1 = tree.add_child(child1, "grandchild1");
8 let _child2 = tree.add_child(root, "child2");
9
10 let mut log = vec![];
11 tree.traverse(
12 |idx, data, log| log.push(format!("Visiting node {}: {}", idx, data)),
13 |idx, data, log| log.push(format!("Finished node {}: {}", idx, data)),
14 &mut log,
15 );
16
17 println!("{:?}", log);
18}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 to an existing node in the tree.
§Parameters
parent: The index of the parent node.data: The data to associate with the new child node.
§Returns
The index of the newly added child node.
§Example
use easy_tree::Tree;
let mut tree = Tree::new();
let root = tree.add_node("root");
let child = tree.add_child(root, "child");Examples found in repository?
More examples
3fn main() {
4 let mut tree = Tree::new();
5 let root = tree.add_node("root");
6 let child1 = tree.add_child(root, "child1");
7 let _grandchild1 = tree.add_child(child1, "grandchild1");
8 let _child2 = tree.add_child(root, "child2");
9
10 let mut log = vec![];
11 tree.traverse(
12 |idx, data, log| log.push(format!("Visiting node {}: {}", idx, data)),
13 |idx, data, log| log.push(format!("Finished node {}: {}", idx, data)),
14 &mut log,
15 );
16
17 println!("{:?}", log);
18}Sourcepub fn add_child_to_root(&mut self, data: T) -> usize
pub fn add_child_to_root(&mut self, data: T) -> usize
Sourcepub fn get(&self, index: usize) -> Option<&T>
pub fn get(&self, index: usize) -> Option<&T>
Retrieves a reference to the data stored in a node.
§Parameters
index: The index of the node to access.
§Returns
Some(&T) if the node exists, or None if the index is out of bounds.
§Example
use easy_tree::Tree;
let mut tree = Tree::new();
let root = tree.add_node(42);
assert_eq!(tree.get(root), Some(&42));Sourcepub fn get_unchecked(&self, index: usize) -> &T
pub fn get_unchecked(&self, index: usize) -> &T
Retrieves a reference to the data stored in a node without bounds checking.
This method is faster than Tree::get because it does not perform any bounds checking.
However, it is unsafe to use if the provided index is out of bounds or invalid.
§Parameters
index: The index of the node to access.
§Returns
A reference to the data stored in the node.
§Safety
Ensure that:
- The
indexis within the valid range of node indices in the tree (0 toTree::len() - 1). - The node at the given index exists and has not been removed (if applicable).
§Example
use easy_tree::Tree;
let mut tree = Tree::new();
let root = tree.add_node(42);
// Safe use: The index is valid.
assert_eq!(tree.get_unchecked(root), &42);
// Unsafe use: Accessing an invalid index would cause undefined behavior.
// let invalid = tree.get_unchecked(999); // Avoid this!Sourcepub fn get_mut(&mut self, index: usize) -> Option<&mut T>
pub fn get_mut(&mut self, index: usize) -> Option<&mut T>
Retrieves a mutable reference to the data stored in a node.
§Parameters
index: The index of the node to access.
§Returns
Some(&mut T) if the node exists, or None if the index is out of bounds.
§Example
use easy_tree::Tree;
let mut tree = Tree::new();
let root = tree.add_node(42);
*tree.get_mut(root).unwrap() = 43;
assert_eq!(tree.get(root), Some(&43));Sourcepub fn get_unchecked_mut(&mut self, index: usize) -> &mut T
pub fn get_unchecked_mut(&mut self, index: usize) -> &mut T
Retrieves a mutable reference to the data stored in a node without bounds checking.
This method is faster than Tree::get_mut because it does not perform any bounds checking.
However, it is unsafe to use if the provided index is out of bounds or invalid.
§Parameters
index: The index of the node to access.
§Returns
A mutable reference to the data stored in the node.
§Safety
Ensure that:
- The
indexis within the valid range of node indices in the tree (0 toTree::len() - 1). - The node at the given index exists and has not been removed (if applicable).
- No other references to the same node are active during this call, to avoid data races or aliasing violations.
§Example
use easy_tree::Tree;
let mut tree = Tree::new();
let root = tree.add_node(42);
// Safe use: The index is valid.
*tree.get_unchecked_mut(root) = 99;
assert_eq!(tree.get_unchecked(root), &99);
// Unsafe use: Accessing an invalid index would cause undefined behavior.
// let invalid = tree.get_unchecked_mut(999); // Avoid this!Sourcepub fn parent_index_unchecked(&self, index: usize) -> Option<usize>
pub fn parent_index_unchecked(&self, index: usize) -> Option<usize>
Returns the parent index of a node, if it has a parent.
§Parameters
index: The index of the node.
§Returns
Some(parent_index) if the node has a parent, or None otherwise.
§Panics
This method panics if the index is out of bounds.
§Example
use easy_tree::Tree;
let mut tree = Tree::new();
let root = tree.add_node(42);
let child = tree.add_child(root, 99);
assert_eq!(tree.parent_index_unchecked(child), Some(root));Sourcepub fn children(&self, index: usize) -> &[usize]
pub fn children(&self, index: usize) -> &[usize]
Returns a slice of the indices of the children of a node.
§Parameters
index: The index of the node.
§Returns
A slice containing the indices of the node’s children.
§Panics
This method panics if the index is out of bounds.
§Example
use easy_tree::Tree;
let mut tree = Tree::new();
let root = tree.add_node("root");
let child = tree.add_child(root, "child");
assert_eq!(tree.children(root), &[child]);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, )
Traverses the tree in a depth-first manner.
The traversal applies two callbacks:
before_processing_children: Called before processing the children of a node.after_processing_the_subtree: Called after processing all children of a node.
§Parameters
before_processing_children: A function to apply before visiting children.after_processing_the_subtree: A function to apply after visiting children.state: Mutable state to share across callbacks.
§Example
use easy_tree::Tree;
let mut tree = Tree::new();
let root = tree.add_node("root");
let child = tree.add_child(root, "child");
let mut log = vec![];
tree.traverse(
|idx, data, log| log.push(format!("Entering node {}: {}", idx, data)),
|idx, data, log| log.push(format!("Leaving node {}: {}", idx, data)),
&mut log,
);Examples found in repository?
3fn main() {
4 let mut tree = Tree::new();
5 let root = tree.add_node("root");
6 let child1 = tree.add_child(root, "child1");
7 let _grandchild1 = tree.add_child(child1, "grandchild1");
8 let _child2 = tree.add_child(root, "child2");
9
10 let mut log = vec![];
11 tree.traverse(
12 |idx, data, log| log.push(format!("Visiting node {}: {}", idx, data)),
13 |idx, data, log| log.push(format!("Finished node {}: {}", idx, data)),
14 &mut log,
15 );
16
17 println!("{:?}", log);
18}Sourcepub fn traverse_mut<S>(
&mut self,
before_processing_children: impl FnMut(usize, &mut T, &mut S),
after_processing_the_subtree: impl FnMut(usize, &mut T, &mut S),
s: &mut S,
)
pub fn traverse_mut<S>( &mut self, before_processing_children: impl FnMut(usize, &mut T, &mut S), after_processing_the_subtree: impl FnMut(usize, &mut T, &mut S), s: &mut S, )
Walks the tree recursively, applying the given functions before and after processing the children of each node. This version allows for mutable access to the nodes.
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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