pub struct Forest<T> { /* private fields */ }
Expand description
A nullable forest
Implementations
sourceimpl<T> Forest<T>
impl<T> Forest<T>
sourcepub fn degree(&self) -> usize
pub fn degree(&self) -> usize
Returns the number of child nodes in Forest
.
Examples
use trees::linked::fully::tr;
let forest = tr(0) - tr(1)/tr(2)/tr(3) - tr(4)/tr(5)/tr(6);
assert_eq!( forest.degree(), 3 );
sourcepub fn node_count(&self) -> usize
pub fn node_count(&self) -> usize
Returns the number of all subnodes in Forest
.
Examples
use trees::linked::fully::tr;
let forest = tr(0) - tr(1)/tr(2)/tr(3) - tr(4)/tr(5)/tr(6);
assert_eq!( forest.node_count(), 7 );
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if the Forest
is empty.
This operation should compute in O(1) time.
Examples
use trees::linked::fully::{tr,fr};
let mut forest = fr();
assert!( forest.is_empty() );
forest.push_back( tr(1) );
assert!( !forest.is_empty() );
sourcepub fn first(&self) -> Option<&Node<T>>
pub fn first(&self) -> Option<&Node<T>>
Returns the first child of the forest, or None if it is empty.
sourcepub fn first_mut(&mut self) -> Option<&mut Node<T>>
pub fn first_mut(&mut self) -> Option<&mut Node<T>>
Returns a mutable pointer to the first child of the forest, or None if it is empty.
sourcepub fn last(&self) -> Option<&Node<T>>
pub fn last(&self) -> Option<&Node<T>>
Returns the last child of the forest, or None if it is empty.
sourcepub fn last_mut(&mut self) -> Option<&mut Node<T>>
pub fn last_mut(&mut self) -> Option<&mut Node<T>>
Returns a mutable pointer to the last child of the forest, or None if it is empty.
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::linked::fully::{tr,fr};
let mut forest = fr();
forest.push_front( tr(1) );
assert_eq!( forest.to_string(), "( 1 )" );
forest.push_front( tr(2) );
assert_eq!( forest.to_string(), "( 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 first child.
Examples
use trees::linked::fully::{tr,fr};
let mut forest = fr();
forest.push_back( tr(1) );
assert_eq!( forest.to_string(), "( 1 )" );
forest.push_back( tr(2) );
assert_eq!( forest.to_string(), "( 1 2 )" );
sourcepub fn pop_front(&mut self) -> Option<Tree<T>>
pub fn pop_front(&mut self) -> Option<Tree<T>>
remove and return the first child
Examples
use trees::linked::fully::tr;
let mut forest = -tr(1)-tr(2);
assert_eq!( forest.pop_front(), Some( tr(1) ));
assert_eq!( forest.to_string(), "( 2 )" );
assert_eq!( forest.pop_front(), Some( tr(2) ));
assert_eq!( forest.to_string(), "()" );
sourcepub fn pop_back(&mut self) -> Option<Tree<T>>
pub fn pop_back(&mut self) -> Option<Tree<T>>
remove and return the first child
Examples
use trees::linked::fully::tr;
let mut forest = -tr(1)-tr(2);
assert_eq!( forest.pop_back(), Some( tr(2) ));
assert_eq!( forest.to_string(), "( 1 )" );
assert_eq!( forest.pop_back(), Some( tr(1) ));
assert_eq!( forest.to_string(), "()" );
sourcepub fn prepend(&mut self, forest: Forest<T>)
pub fn prepend(&mut self, forest: Forest<T>)
merge the forest at front
Examples
use trees::linked::fully::{tr,fr};
let mut forest = fr();
forest.prepend( -tr(0)-tr(1) );
assert_eq!( forest.to_string(), "( 0 1 )" );
forest.prepend( -tr(2)-tr(3) );
assert_eq!( forest.to_string(), "( 2 3 0 1 )" );
sourcepub fn append(&mut self, forest: Forest<T>)
pub fn append(&mut self, forest: Forest<T>)
merge the forest at back
Examples
use trees::linked::fully::{tr,fr};
let mut forest = fr();
forest.append( -tr(0)-tr(1) );
assert_eq!( forest.to_string(), "( 0 1 )" );
forest.append( -tr(2)-tr(3) );
assert_eq!( forest.to_string(), "( 0 1 2 3 )" );
sourcepub fn iter<'a>(&self) -> Iter<'a, T> ⓘ
pub fn iter<'a>(&self) -> Iter<'a, T> ⓘ
Provides a forward iterator over child Node
s
Examples
use trees::linked::fully::{tr,fr};
let forest = fr::<i32>();
assert_eq!( forest.iter().next(), None );
let forest = -tr(1)-tr(2);
let mut iter = forest.iter();
assert_eq!( iter.next(), Some( tr(1).root() ));
assert_eq!( iter.next(), Some( tr(2).root() ));
assert_eq!( iter.next(), None );
assert_eq!( iter.next(), None );
pub fn children<'a>(&self) -> Iter<'a, T> ⓘ
iter
insteadsourcepub fn iter_mut<'a>(&mut self) -> IterMut<'a, T> ⓘ
pub fn iter_mut<'a>(&mut self) -> IterMut<'a, T> ⓘ
Provides a forward iterator over child Node
s with mutable references.
Examples
use trees::linked::fully::{tr,fr};
let mut forest = fr::<i32>();
assert_eq!( forest.iter_mut().next(), None );
let mut forest = -tr(1)-tr(2);
for child in forest.iter_mut() { child.data *= 10; }
assert_eq!( forest.to_string(), "( 10 20 )" );
pub fn children_mut<'a>(&mut self) -> IterMut<'a, T> ⓘ
iter_mut
insteadsourcepub fn onto_iter<'a>(&mut self) -> OntoIter<'a, T> ⓘ
pub fn onto_iter<'a>(&mut self) -> OntoIter<'a, T> ⓘ
Provide an iterator over Forest
’s Subnode
s for insert/remove at any position.
See Subnode
’s document for more.
sourcepub fn bfs<'a, 's: 'a>(&'s self) -> BfsForest<Splitted<Iter<'a, T>>>
pub fn bfs<'a, 's: 'a>(&'s self) -> BfsForest<Splitted<Iter<'a, T>>>
Provides a forward iterator in a breadth-first manner
Examples
use trees::{bfs,Size};
use trees::linked::fully::{tr,fr};
let forest = fr::<i32>();
let visits = forest.bfs().iter.collect::<Vec<_>>();
assert!( visits.is_empty() );
let forest = -( tr(1)/tr(2)/tr(3) ) -( tr(4)/tr(5)/tr(6) );
let visits = forest.bfs().iter.collect::<Vec<_>>();
assert_eq!( visits, vec![
bfs::Visit{ data: &1, size: Size{ degree: 2, node_cnt: 3 }},
bfs::Visit{ data: &4, size: Size{ degree: 2, node_cnt: 3 }},
bfs::Visit{ data: &2, size: Size{ degree: 0, node_cnt: 1 }},
bfs::Visit{ data: &3, size: Size{ degree: 0, node_cnt: 1 }},
bfs::Visit{ data: &5, size: Size{ degree: 0, node_cnt: 1 }},
bfs::Visit{ data: &6, size: Size{ degree: 0, node_cnt: 1 }},
]);
sourcepub fn bfs_mut<'a, 's: 'a>(&'s mut self) -> BfsForest<Splitted<IterMut<'a, T>>>
pub fn bfs_mut<'a, 's: 'a>(&'s mut self) -> BfsForest<Splitted<IterMut<'a, T>>>
Provides a forward iterator with mutable references in a breadth-first manner
Examples
use trees::{bfs,Size};
use trees::linked::fully::{tr,fr};
let mut forest = fr::<i32>();
let visits = forest.bfs_mut().iter.collect::<Vec<_>>();
assert!( visits.is_empty() );
let mut forest = -( tr(1)/tr(2)/tr(3) ) -( tr(4)/tr(5)/tr(6) );
let visits = forest.bfs_mut().iter.collect::<Vec<_>>();
assert_eq!( visits, vec![
bfs::Visit{ data: &mut 1, size: Size{ degree: 2, node_cnt: 3 }},
bfs::Visit{ data: &mut 4, size: Size{ degree: 2, node_cnt: 3 }},
bfs::Visit{ data: &mut 2, size: Size{ degree: 0, node_cnt: 1 }},
bfs::Visit{ data: &mut 3, size: Size{ degree: 0, node_cnt: 1 }},
bfs::Visit{ data: &mut 5, size: Size{ degree: 0, node_cnt: 1 }},
bfs::Visit{ data: &mut 6, size: Size{ degree: 0, node_cnt: 1 }},
]);
sourcepub fn into_bfs(self) -> BfsForest<Splitted<IntoIter<T>>>
pub fn into_bfs(self) -> BfsForest<Splitted<IntoIter<T>>>
Provides a forward iterator with owned data in a breadth-first manner
Examples
use trees::{bfs,Size};
use trees::linked::fully::{tr,fr};
let forest = fr::<i32>();
let visits = forest.into_bfs().iter.collect::<Vec<_>>();
assert!( visits.is_empty() );
let forest = -( tr(1)/tr(2)/tr(3) ) -( tr(4)/tr(5)/tr(6) );
let visits = forest.into_bfs().iter.collect::<Vec<_>>();
assert_eq!( visits, vec![
bfs::Visit{ data: 1, size: Size{ degree: 2, node_cnt: 3 }},
bfs::Visit{ data: 4, size: Size{ degree: 2, node_cnt: 3 }},
bfs::Visit{ data: 2, size: Size{ degree: 0, node_cnt: 1 }},
bfs::Visit{ data: 3, size: Size{ degree: 0, node_cnt: 1 }},
bfs::Visit{ data: 5, size: Size{ degree: 0, node_cnt: 1 }},
bfs::Visit{ data: 6, size: Size{ degree: 0, node_cnt: 1 }},
]);
Trait Implementations
sourceimpl<T> BorrowMut<Forest<T>> for Tree<T>
impl<T> BorrowMut<Forest<T>> for Tree<T>
sourcefn borrow_mut(&mut self) -> &mut Forest<T>
fn borrow_mut(&mut self) -> &mut Forest<T>
sourceimpl<T> Extend<Tree<T>> for Forest<T>
impl<T> Extend<Tree<T>> for Forest<T>
sourcefn extend<I: IntoIterator<Item = Tree<T>>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = Tree<T>>>(&mut self, iter: I)
sourcefn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)sourcefn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)sourceimpl<T> From<Forest<T>> for ForestWalk<T>
impl<T> From<Forest<T>> for ForestWalk<T>
sourceimpl<T> FromIterator<Tree<T>> for Forest<T>
impl<T> FromIterator<Tree<T>> for Forest<T>
sourcefn from_iter<I: IntoIterator<Item = Tree<T>>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = Tree<T>>>(iter: I) -> Self
sourceimpl<T> Into<Forest<T>> for ForestWalk<T>
impl<T> Into<Forest<T>> for ForestWalk<T>
sourceimpl<T> IntoIterator for Forest<T>
impl<T> IntoIterator for Forest<T>
sourceimpl<T: Ord> Ord for Forest<T>
impl<T: Ord> Ord for Forest<T>
1.21.0 · sourcefn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
1.21.0 · sourcefn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
1.50.0 · sourcefn clamp(self, min: Self, max: Self) -> Selfwhere
Self: Sized + PartialOrd<Self>,
fn clamp(self, min: Self, max: Self) -> Selfwhere
Self: Sized + PartialOrd<Self>,
sourceimpl<T: PartialOrd> PartialOrd<Forest<T>> for Forest<T>
impl<T: PartialOrd> PartialOrd<Forest<T>> for Forest<T>
sourcefn partial_cmp(&self, other: &Self) -> Option<Ordering>
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more