pub struct Subnode<'a, T: 'a> { /* private fields */ }
Expand description
Wrapper of Node
for allowing modification of parent or sib links.
Any Node
that is the root of some Tree
is impossible to be Subnode
.
Implementations
sourceimpl<'a, T: 'a> Subnode<'a, T>
impl<'a, T: 'a> Subnode<'a, T>
sourcepub fn insert_before(&mut self, sib: Tree<T>)
pub fn insert_before(&mut self, sib: Tree<T>)
Insert sib tree before self
.
The newly inserted node will not be iterated over by the currently running iterator.
Examples
use trees::linked::singly::tr;
let mut tree = tr(0) /tr(1)/tr(2);
for mut sub in tree.onto_iter() { sub.insert_before( tr(3) ); }
assert_eq!( tree.to_string(), "0( 3 1 3 2 )" );
sourcepub fn insert_after(&mut self, sib: Tree<T>)
pub fn insert_after(&mut self, sib: Tree<T>)
Insert sib tree after self
.
The newly inserted node will not be iterated over by the currently running iterator.
Examples
use trees::linked::singly::tr;
let mut tree = tr(0) /tr(1)/tr(2);
for mut sub in tree.onto_iter() { sub.insert_after( tr(3) ); }
assert_eq!( tree.to_string(), "0( 1 3 2 3 )" );
sourcepub fn depart(self) -> Tree<T>
pub fn depart(self) -> Tree<T>
The subtree departs from its parent and becomes an indepent Tree
.
Examples
use trees::linked::singly::{tr,fr};
let mut forest = -tr(1)-tr(2)-tr(3);
//for sub in forest.onto_iter() { sub.depart(); }
//forest.onto_iter().next().unwrap().depart();
//assert_eq!( forest, fr() );
Methods from Deref<Target = Node<T>>
pub fn is_leaf(&self) -> bool
sourcepub fn forest(&self) -> &Forest<T>
pub fn forest(&self) -> &Forest<T>
Returns the given Tree
’s children as a borrowed Forest
.
Examples
use trees::linked::singly::tr;
let mut tree = tr(0) /tr(1)/tr(2);
assert_eq!( tree.forest().to_string(), "( 1 2 )" );
sourcepub fn forest_mut(&mut self) -> &mut Forest<T>
pub fn forest_mut(&mut self) -> &mut Forest<T>
Returns the given Tree
’s children as a mutable borrowed Forest
.
Examples
use trees::linked::singly::tr;
let mut tree = tr(0) /tr(1)/tr(2);
for child in tree.forest_mut().iter_mut() { child.data *= 10; }
assert_eq!( tree.to_string(), "0( 10 20 )" );
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::singly::tr;
let mut tree = tr(0);
tree.push_front( tr(1) );
assert_eq!( tree.to_string(), "0( 1 )" );
tree.push_front( tr(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>)
Add the tree as the last child
Examples
use trees::linked::singly::tr;
let mut tree = tr(0);
tree.push_back( tr(1) );
assert_eq!( tree.to_string(), "0( 1 )" );
tree.push_back( tr(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>>
Remove and return the first child
Examples
use trees::linked::singly::tr;
let mut tree = tr(0) /tr(1)/tr(2);
assert_eq!( tree.pop_front(), Some( tr(1) ));
assert_eq!( tree.to_string(), "0( 2 )" );
assert_eq!( tree.pop_front(), Some( tr(2) ));
assert_eq!( tree.to_string(), "0" );
sourcepub fn prepend(&mut self, forest: Forest<T>)
pub fn prepend(&mut self, forest: Forest<T>)
Add all the forest’s trees at front of children list
Examples
use trees::linked::singly::tr;
let mut tree = tr(0);
tree.prepend( -tr(1)-tr(2) );
assert_eq!( tree.to_string(), "0( 1 2 )" );
tree.prepend( -tr(3)-tr(4) );
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>)
Add all the forest’s trees at back of children list
Examples
use trees::linked::singly::tr;
let mut tree = tr(0);
tree.append( -tr(1)-tr(2) );
assert_eq!( tree.to_string(), "0( 1 2 )" );
tree.append( -tr(3)-tr(4) );
assert_eq!( tree.to_string(), "0( 1 2 3 4 )" );
sourcepub fn iter<'a, 's: 'a>(&'s self) -> Iter<'a, T> ⓘ
pub fn iter<'a, 's: 'a>(&'s self) -> Iter<'a, T> ⓘ
Provides a forward iterator over child Node
s
Examples
use trees::linked::singly::tr;
let tree = tr(0);
assert_eq!( tree.iter().next(), None );
let tree = tr(0) /tr(1)/tr(2);
let mut iter = tree.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 );
sourcepub fn iter_mut<'a, 's: 'a>(&'s mut self) -> IterMut<'a, T> ⓘ
pub fn iter_mut<'a, 's: 'a>(&'s mut self) -> IterMut<'a, T> ⓘ
Provides a forward iterator over child Node
s with mutable references.
Examples
use trees::linked::singly::tr;
let mut tree = tr(0);
assert_eq!( tree.iter_mut().next(), None );
let mut tree = tr(0) /tr(1)/tr(2);
for child in tree.iter_mut() { child.data *= 10; }
assert_eq!( tree.to_string(), "0( 10 20 )" );
sourcepub fn onto_iter<'a, 's: 'a>(&'s mut self) -> OntoIter<'a, T> ⓘ
pub fn onto_iter<'a, 's: 'a>(&'s mut self) -> OntoIter<'a, T> ⓘ
Provide an iterator over Node
’s Subnode
s for insert/remove at any position.
See Subnode
’s document for more.
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::{bfs,Size};
use trees::linked::singly::tr;
let tree = tr(0) /( tr(1)/tr(2)/tr(3) ) /( tr(4)/tr(5)/tr(6) );
let visits = tree.root().bfs().iter.collect::<Vec<_>>();
assert_eq!( visits, vec![
bfs::Visit{ data: &0, size: Size{ degree: 2, node_cnt: 0 }},
bfs::Visit{ data: &1, size: Size{ degree: 2, node_cnt: 0 }},
bfs::Visit{ data: &4, size: Size{ degree: 2, node_cnt: 0 }},
bfs::Visit{ data: &2, size: Size{ degree: 0, node_cnt: 0 }},
bfs::Visit{ data: &3, size: Size{ degree: 0, node_cnt: 0 }},
bfs::Visit{ data: &5, size: Size{ degree: 0, node_cnt: 0 }},
bfs::Visit{ data: &6, size: Size{ degree: 0, node_cnt: 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::{bfs,Size};
use trees::linked::singly::tr;
let mut tree = tr(0) /( tr(1)/tr(2)/tr(3) ) /( tr(4)/tr(5)/tr(6) );
let visits = tree.root_mut().bfs_mut().iter.collect::<Vec<_>>();
assert_eq!( visits, vec![
bfs::Visit{ data: &mut 0, size: Size{ degree: 2, node_cnt: 0 }},
bfs::Visit{ data: &mut 1, size: Size{ degree: 2, node_cnt: 0 }},
bfs::Visit{ data: &mut 4, size: Size{ degree: 2, node_cnt: 0 }},
bfs::Visit{ data: &mut 2, size: Size{ degree: 0, node_cnt: 0 }},
bfs::Visit{ data: &mut 3, size: Size{ degree: 0, node_cnt: 0 }},
bfs::Visit{ data: &mut 5, size: Size{ degree: 0, node_cnt: 0 }},
bfs::Visit{ data: &mut 6, size: Size{ degree: 0, node_cnt: 0 }},
]);