pub struct Node<T> { /* private fields */ }
Expand description
Composed of data
and a list of its child Node
s.
Size infomation tracked.
Implementations§
Source§impl<T> Node<T>
impl<T> Node<T>
Sourcepub fn has_no_child(&self) -> bool
pub fn has_no_child(&self) -> bool
Returns true
if Node
has no child nodes.
§Examples
use trees::Tree;
let mut tree = Tree::new(0);
let mut root = tree.root_mut();
assert!( root.has_no_child() );
root.push_back( Tree::new(1) );
assert!( !root.has_no_child() );
Sourcepub fn degree(&self) -> usize
pub fn degree(&self) -> usize
Returns the number of child nodes in Node
.
§Examples
use trees::Tree;
let mut tree = Tree::new(0);
let mut root = tree.root_mut();
assert_eq!( root.degree(), 0 );
root.push_back( Tree::new(1) );
assert_eq!( root.degree(), 1 );
root.push_back( Tree::new(2) );
assert_eq!( root.degree(), 2 );
Sourcepub fn node_count(&self) -> usize
pub fn node_count(&self) -> usize
Returns the number of all child nodes in Node
, including itself.
§Examples
use trees::Tree;
let tree = Tree::<i32>::from_tuple(( 0, (1,2), (3,4) ));
assert_eq!( tree.root().node_count(), 5 );
Sourcepub fn parent(&self) -> Option<&Node<T>>
pub fn parent(&self) -> Option<&Node<T>>
Returns the parent node of this node, or None if it is the root node.
§Examples
use trees::Tree;
let tree = Tree::<i32>::from_tuple(( 0, 1, 2, 3 ));
tree.root().iter().for_each( |child| {
assert_eq!( child.parent(), Some( tree.root()))
});
Sourcepub fn insert_prev_sib(&mut self, sib: Tree<T>)
pub fn insert_prev_sib(&mut self, sib: Tree<T>)
Inserts sib tree before self
.
The newly inserted node will not be iterated over by the currently running iterator.
§Examples
use trees::tr;
let mut tree = tr(0) /tr(1)/tr(2);
tree.iter_mut().for_each( |mut sub| sub.insert_prev_sib( tr(3) ));
assert_eq!( tree.to_string(), "0( 3 1 3 2 )" );
Sourcepub fn insert_next_sib(&mut self, sib: Tree<T>)
pub fn insert_next_sib(&mut self, sib: Tree<T>)
Inserts sib tree after self
.
The newly inserted node will not be iterated over by the currently running iterator.
§Examples
use trees::tr;
let mut tree = tr(0) /tr(1)/tr(2);
tree.iter_mut().for_each( |mut sub| sub.insert_next_sib( tr(3) ));
assert_eq!( tree.to_string(), "0( 1 3 2 3 )" );
Sourcepub fn detach(&mut self) -> Tree<T>
pub fn detach(&mut self) -> Tree<T>
The subtree departs from its parent and becomes an indepent Tree
.
§Examples
use trees::{tr, fr};
let mut forest = fr()-tr(1)-tr(2)-tr(3);
forest.iter_mut().for_each( |mut sub| { sub.detach(); });
assert_eq!( forest, fr() );
Sourcepub fn iter<'a, 's>(&'s self) -> Iter<'a, T>where
's: 'a,
pub fn iter<'a, 's>(&'s self) -> Iter<'a, T>where
's: 'a,
Provides a forward iterator over child Node
s
§Examples
use trees::Tree;
let mut tree = Tree::new(0);
assert_eq!( tree.iter().next(), None );
tree.push_back( Tree::new(1) );
tree.push_back( Tree::new(2) );
let mut iter = tree.root().iter();
assert_eq!( iter.next(), Some( Tree::new(1).root() ));
assert_eq!( iter.next(), Some( Tree::new(2).root() ));
assert_eq!( iter.next(), None );
Sourcepub fn iter_mut<'a, 's>(&'s mut self) -> IterMut<'a, T>where
's: 'a,
pub fn iter_mut<'a, 's>(&'s mut self) -> IterMut<'a, T>where
's: 'a,
Provides a forward iterator over child Node
s with mutable references.
§Examples
use trees::Tree;
let mut tree = Tree::<i32>::from_tuple(( 0, (1, 2, 3), ));
tree.front_mut().unwrap()
.iter_mut()
.for_each( |mut child| *child.data_mut() *= 10 );
assert_eq!( tree.to_string(), "0( 1( 20 30 ) )" );
Sourcepub fn front(&self) -> Option<&Node<T>>
pub fn front(&self) -> Option<&Node<T>>
Returns the first child of this node, or None if it has no child.
Sourcepub fn front_mut(&mut self) -> Option<Pin<&mut Node<T>>>
pub fn front_mut(&mut self) -> Option<Pin<&mut Node<T>>>
Returns a mutable pointer to the first child of this node, or None if it has no child.
Sourcepub fn back(&self) -> Option<&Node<T>>
pub fn back(&self) -> Option<&Node<T>>
Returns the last child of this node, or None if it has no child.
Sourcepub fn back_mut(&mut self) -> Option<Pin<&mut Node<T>>>
pub fn back_mut(&mut self) -> Option<Pin<&mut Node<T>>>
Returns a mutable pointer to the last child of this node, or None if it has no child.
Sourcepub fn push_front(&mut self, tree: Tree<T>)
pub fn push_front(&mut self, tree: Tree<T>)
Adds the tree as the first child.
§Examples
use trees::Tree;
let mut tree = Tree::new(0);
tree.root_mut().push_front( Tree::new(1) );
assert_eq!( tree.to_string(), "0( 1 )" );
tree.root_mut().push_front( Tree::new(2) );
assert_eq!( tree.to_string(), "0( 2 1 )" );
Sourcepub fn push_back(&mut self, tree: Tree<T>)
pub fn push_back(&mut self, tree: Tree<T>)
Adds the tree as the last child.
§Examples
use trees::Tree;
let mut tree = Tree::new(0);
tree.root_mut().push_back( Tree::new(1) );
assert_eq!( tree.to_string(), "0( 1 )" );
tree.root_mut().push_back( Tree::new(2) );
assert_eq!( tree.to_string(), "0( 1 2 )" );
Sourcepub fn pop_front(&mut self) -> Option<Tree<T>>
pub fn pop_front(&mut self) -> Option<Tree<T>>
Removes and return the first child.
§Examples
use trees::Tree;
let mut tree = Tree::<i32>::from_tuple(( 0, (1, 2, 3), ));
assert_eq!( tree.to_string(), "0( 1( 2 3 ) )" );
assert_eq!( tree.front_mut().unwrap().pop_front(), Some( Tree::new(2) ));
assert_eq!( tree.to_string(), "0( 1( 3 ) )" );
assert_eq!( tree.front_mut().unwrap().pop_front(), Some( Tree::new(3) ));
assert_eq!( tree.to_string(), "0( 1 )" );
Sourcepub fn pop_back(&mut self) -> Option<Tree<T>>
pub fn pop_back(&mut self) -> Option<Tree<T>>
Removes and return the last child.
§Examples
use trees::Tree;
let mut tree = Tree::<i32>::from_tuple(( 0, (1, 2, 3), ));
assert_eq!( tree.to_string(), "0( 1( 2 3 ) )" );
assert_eq!( tree.front_mut().unwrap().pop_back(), Some( Tree::new(3) ));
assert_eq!( tree.to_string(), "0( 1( 2 ) )" );
assert_eq!( tree.front_mut().unwrap().pop_back(), Some( Tree::new(2) ));
assert_eq!( tree.to_string(), "0( 1 )" );
Sourcepub fn prepend(&mut self, forest: Forest<T>)
pub fn prepend(&mut self, forest: Forest<T>)
Adds all the forest’s trees at front of children list.
§Examples
use trees::{Forest, Tree};
let mut tree = Tree::new(0);
tree.push_back( Tree::new(1) );
tree.push_back( Tree::new(2) );
let mut forest = Forest::new();
forest.push_back( Tree::new(3) );
forest.push_back( Tree::new(4) );
tree.root_mut().prepend( forest );
assert_eq!( tree.to_string(), "0( 3 4 1 2 )" );
Sourcepub fn append(&mut self, forest: Forest<T>)
pub fn append(&mut self, forest: Forest<T>)
Adds all the forest’s trees at back of children list.
§Examples
use trees::{Forest, Tree};
let mut tree = Tree::new(0);
tree.root_mut().push_back( Tree::new(1) );
tree.root_mut().push_back( Tree::new(2) );
let mut forest = Forest::new();
forest.push_back( Tree::new(3) );
forest.push_back( Tree::new(4) );
tree.root_mut().append( forest );
assert_eq!( tree.to_string(), "0( 1 2 3 4 )" );
Source§impl<T> Node<T>
impl<T> Node<T>
Sourcepub fn deep_clone(&self) -> Tree<T>where
T: Clone,
pub fn deep_clone(&self) -> Tree<T>where
T: Clone,
Clones the node deeply and creates a new tree.
§Examples
use trees::Tree;
let tree = Tree::<i32>::from_tuple(( 0, (1,2,3), (4,5,6), (7,8,9), ));
assert_eq!( tree.iter().nth(1).unwrap().deep_clone(),
Tree::from_tuple(( 4,5,6 )));
Sourcepub fn deep_clone_forest(&self) -> Forest<T>where
T: Clone,
pub fn deep_clone_forest(&self) -> Forest<T>where
T: Clone,
Clones the node’s descendant nodes as a forest.
§Examples
use trees::{Tree,Forest};
let tree = Tree::<i32>::from_tuple(( 0, (1,2,3), (4,5,6), (7,8,9), ));
assert_eq!( tree.iter().nth(1).unwrap().deep_clone_forest(),
Forest::from_tuple(( 5,6 )));
Sourcepub fn bfs_children(&self) -> BfsForest<Splitted<Iter<'_, T>>>
pub fn bfs_children(&self) -> BfsForest<Splitted<Iter<'_, T>>>
Provides a forward iterator in a breadth-first manner, which iterates over all its descendants.
§Examples
use trees::Tree;
let tree = Tree::from_tuple(( 0, (1,2,3), (4,5,6), ));
let visits = tree.root().bfs_children().iter
.map( |visit| (*visit.data, visit.size.degree, visit.size.descendants ))
.collect::<Vec<_>>();
assert_eq!( visits, vec![ (1, 2, 2), (4, 2, 2), (2, 0, 0), (3, 0, 0), (5, 0, 0), (6, 0, 0), ]);
Sourcepub fn bfs_children_mut(&mut self) -> BfsForest<Splitted<IterMut<'_, T>>>
pub fn bfs_children_mut(&mut self) -> BfsForest<Splitted<IterMut<'_, T>>>
Provides a forward iterator with mutable references in a breadth-first manner, which iterates over all its descendants.
§Examples
use trees::{tr, Tree};
let mut tree = Tree::from_tuple(( 0, (1,2,3), (4,5,6), ));
let mut root = tree.root_mut();
root.bfs_children_mut().iter
.zip( 1.. )
.for_each( |(visit,nth)| *visit.data += 10 * nth );
assert_eq!( tree, Tree::<i32>::from_tuple(( 0, (11,32,43), (24,55,66), )));
Sourcepub fn bfs(&self) -> BfsTree<Splitted<Iter<'_, T>>>
pub fn bfs(&self) -> BfsTree<Splitted<Iter<'_, T>>>
Provides a forward iterator in a breadth-first manner.
§Examples
use trees::Tree;
let tree = Tree::from_tuple(( 0, (1,2,3), (4,5,6), ));
let visits = tree.root().bfs().iter
.map( |visit| (*visit.data, visit.size.degree, visit.size.descendants ))
.collect::<Vec<_>>();
assert_eq!( visits, vec![ (0, 2, 6), (1, 2, 2), (4, 2, 2), (2, 0, 0), (3, 0, 0), (5, 0, 0), (6, 0, 0), ]);
Sourcepub fn bfs_mut(&mut self) -> BfsTree<Splitted<IterMut<'_, T>>>
pub fn bfs_mut(&mut self) -> BfsTree<Splitted<IterMut<'_, T>>>
Provides a forward iterator with mutable references in a breadth-first manner.
§Examples
use trees::{tr, Tree};
let mut tree = Tree::from_tuple(( 0, (1,2,3), (4,5,6), ));
let mut root = tree.root_mut();
root.bfs_mut().iter
.zip( 1.. )
.for_each( |(visit,nth)| *visit.data += 10 * nth );
assert_eq!( tree, Tree::<i32>::from_tuple(( 10, (21,42,53), (34,65,76), )));
Trait Implementations§
Source§impl<'a, T> IntoIterator for &'a Node<T>where
T: 'a,
impl<'a, T> IntoIterator for &'a Node<T>where
T: 'a,
Source§impl<T> Ord for Node<T>where
T: Ord,
impl<T> Ord for Node<T>where
T: Ord,
Source§impl<T> PartialOrd for Node<T>where
T: PartialOrd,
impl<T> PartialOrd for Node<T>where
T: PartialOrd,
impl<T> Eq for Node<T>where
T: Eq,
Auto Trait Implementations§
impl<T> Freeze for Node<T>where
T: Freeze,
impl<T> !RefUnwindSafe for Node<T>
impl<T> !Send for Node<T>
impl<T> !Sync for Node<T>
impl<T> Unpin for Node<T>where
T: Unpin,
impl<T> !UnwindSafe for Node<T>
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<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.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