[][src]Struct trees::node::Node

pub struct Node<T> { /* fields omitted */ }

Composed of data and a list of its child Nodes. Size infomation tracked.

Implementations

impl<T> Node<T>[src]

pub fn data(&self) -> &T

Notable traits for &'_ mut I

impl<'_, I> Iterator for &'_ mut I where
    I: Iterator + ?Sized
type Item = <I as Iterator>::Item;
[src]

Reference of its associated data.

pub fn data_mut(&mut self) -> &mut T

Notable traits for &'_ mut I

impl<'_, I> Iterator for &'_ mut I where
    I: Iterator + ?Sized
type Item = <I as Iterator>::Item;
[src]

Mutable reeference of its associated data.

pub fn has_no_child(&self) -> bool[src]

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() );

pub fn degree(&self) -> usize[src]

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 );

pub fn node_count(&self) -> usize[src]

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 );

pub fn parent(&self) -> Option<&Node<T>>[src]

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()))
});

pub fn insert_prev_sib(&mut self, mut sib: Tree<T>)[src]

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 )" );

pub fn insert_next_sib(&mut self, mut sib: Tree<T>)[src]

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 )" );

pub fn detach(&mut self) -> Tree<T>[src]

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() );

pub fn iter<'a, 's: 'a>(&'s self) -> Iter<'a, T>

Notable traits for Iter<'a, T>

impl<'a, T: 'a> Iterator for Iter<'a, T> type Item = &'a Node<T>;
[src]

Provides a forward iterator over child Nodes

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 );

pub fn iter_mut<'a, 's: 'a>(&'s mut self) -> IterMut<'a, T>

Notable traits for IterMut<'a, T>

impl<'a, T: 'a> Iterator for IterMut<'a, T> type Item = Pin<&'a mut Node<T>>;
[src]

Provides a forward iterator over child Nodes 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 ) )" );

pub fn front(&self) -> Option<&Node<T>>[src]

Returns the first child of this node, or None if it has no child.

pub fn front_mut(&mut self) -> Option<Pin<&mut Node<T>>>[src]

Returns a mutable pointer to the first child of this node, or None if it has no child.

pub fn back(&self) -> Option<&Node<T>>[src]

Returns the last child of this node, or None if it has no child.

pub fn back_mut(&mut self) -> Option<Pin<&mut Node<T>>>[src]

Returns a mutable pointer to the last child of this node, or None if it has no child.

pub fn push_front(&mut self, mut tree: Tree<T>)[src]

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 )" );

pub fn push_back(&mut self, mut tree: Tree<T>)[src]

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 )" );

pub fn pop_front(&mut self) -> Option<Tree<T>>[src]

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 )" );

pub fn pop_back(&mut self) -> Option<Tree<T>>[src]

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 )" );

pub fn prepend(&mut self, mut forest: Forest<T>)[src]

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 )" );

pub fn append(&mut self, mut forest: Forest<T>)[src]

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 )" );

impl<T> Node<T>[src]

pub fn deep_clone(&self) -> Tree<T> where
    T: Clone
[src]

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 )));

pub fn deep_clone_forest(&self) -> Forest<T> where
    T: Clone
[src]

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 )));

pub fn bfs_children(&self) -> BfsForest<Splitted<Iter<'_, T>>>[src]

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), ]);

pub fn bfs_children_mut(&mut self) -> BfsForest<Splitted<IterMut<'_, T>>>[src]

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), )));

pub fn bfs(&self) -> BfsTree<Splitted<Iter<'_, T>>>[src]

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), ]);

pub fn bfs_mut(&mut self) -> BfsTree<Splitted<IterMut<'_, T>>>[src]

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

impl<T: Debug> Debug for Node<T>[src]

impl<T> Default for Node<T>[src]

impl<T: Display> Display for Node<T>[src]

impl<T: Eq> Eq for Node<T>[src]

impl<T: Hash> Hash for Node<T>[src]

impl<'a, T: 'a> IntoIterator for &'a Node<T>[src]

type Item = Self

The type of the elements being iterated over.

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?

impl<T: Ord> Ord for Node<T>[src]

impl<T: PartialEq> PartialEq<Node<T>> for Node<T>[src]

impl<T: PartialOrd> PartialOrd<Node<T>> for Node<T>[src]

impl<'a, T: 'a> Split for &'a Node<T>[src]

type Item = &'a T

type Iter = Iter<'a, T>

Auto Trait Implementations

impl<T> !RefUnwindSafe for Node<T>[src]

impl<T> !Send for Node<T>[src]

impl<T> !Sync for Node<T>[src]

impl<T> Unpin for Node<T> where
    T: Unpin
[src]

impl<T> !UnwindSafe for Node<T>[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> TupleTree<T, ()> for T[src]