Struct Node

Source
pub struct Node<T> { /* private fields */ }
Expand description

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

Implementations§

Source§

impl<T> Node<T>

Source

pub fn data(&self) -> &T

Reference of its associated data.

Source

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

Mutable reeference of its associated data.

Source

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

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

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

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

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

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

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

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

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

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

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

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

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

Source

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.

Source

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

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

Source

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.

Source

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

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

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

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

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

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>

Source

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

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

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

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

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

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<T> Debug for Node<T>
where T: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<T> Default for Node<T>

Source§

fn default() -> Node<T>

Returns the “default value” for a type. Read more
Source§

impl<T> Display for Node<T>
where T: Display,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<T> Hash for Node<T>
where T: Hash,

Source§

fn hash<H>(&self, state: &mut H)
where H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a, T> IntoIterator for &'a Node<T>
where T: 'a,

Source§

type Item = &'a Node<T>

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> <&'a Node<T> as IntoIterator>::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T> Ord for Node<T>
where T: Ord,

Source§

fn cmp(&self, other: &Node<T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T> PartialEq for Node<T>
where T: PartialEq,

Source§

fn eq(&self, other: &Node<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
Source§

fn ne(&self, other: &Node<T>) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> PartialOrd for Node<T>
where T: PartialOrd,

Source§

fn partial_cmp(&self, other: &Node<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'a, T> Split for &'a Node<T>
where T: 'a,

Source§

type Item = &'a T

Source§

type Iter = Iter<'a, T>

Source§

fn split( self, ) -> (<&'a Node<T> as Split>::Item, <&'a Node<T> as Split>::Iter, usize)

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> TupleTree<T, ()> for T

Source§

const SIZE: Size

Source§

fn descendants(_indirect_level: usize) -> usize

Source§

fn height() -> usize

Source§

fn preorder(self, f: &mut impl FnMut(Visit<T>))

Source§

fn preorder_with_size_hint(self, f: &mut impl FnMut(Visit<T>, Size))

Source§

fn postorder(self, f: &mut impl FnMut(Visit<T>))

Source§

fn postorder_with_size_hint(self, f: &mut impl FnMut(Visit<T>, Size))