Struct trees::potted::tree::Tree

source ·
pub struct Tree<T> { /* private fields */ }

Implementations

Break the tree into root’s data and the children forest.

Examples
use trees::potted::Tree;

let tree: Tree<_> = ( 1, (2,3,4), (5,6,7) ).into();
let ( root_data, forest ) = tree.abandon();
assert_eq!( root_data, 1 );
assert_eq!( forest.to_string(), "( 2( 3 4 ) 5( 6 7 ) )" );

For debug purpose.

Provides a forward iterator with owned data in a breadth-first manner

Examples
use trees::{bfs,Size};
use trees::potted::Tree;

let tree: Tree<_> = (0, (1,2,3), (4,5,6) ).into();
let visits = tree.into_bfs().iter.collect::<Vec<_>>();
assert_eq!( visits, vec![
    bfs::Visit{ data: 0, size: Size{ degree: 2, node_cnt: 7 }},
    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 }},
]);

Methods from Deref<Target = Node<T>>

Returns true if this node has no child nodes, otherwise false.

Examples
use trees::potted::{Tree,Node,TreeData,TupleTree};

let mut tree = Tree::<_>::from(( 1, ));
let root: &mut Node<_> = tree.root_mut();
assert!( root.is_leaf() );

root.append_tr(( 2, ));
assert!( !root.is_leaf() );
👎Deprecated since 0.2.1: please use data field instead
👎Deprecated since 0.2.1: please use data field instead
👎Deprecated since 0.2.1: please use data field instead

Returns reference of parent node if exists, otherwise None.

Examples
use trees::potted::{Tree,Node,TreeData,TupleTree};

let mut tree = Tree::<_>::from(( 1, 2 ));
let root  : &Node<_> = tree.root();
let child : &Node<_> = root.iter().next().unwrap();
assert_eq!( root.parent(),  None );
assert_eq!( child.parent(), Some( root ));

Returns the immutable reference of n-th child node if exists, otherwise None. Note that it is zero-based index.

Examples
use trees::potted::{Tree,TreeData,TupleTree};

let tree: Tree<_> = ( "a", "b", "c", "d", "e" ).into();
assert_eq!( tree.root().nth_child(2).unwrap().data, "d" );

Returns the mutable reference of n-th child node if exists, otherwise None. Note that it is zero-based index.

Examples
use trees::potted::{Tree,TreeData,TupleTree};

let mut tree: Tree<_> = ( "a", "b", "c", "d", "e" ).into();
tree.root_mut().nth_child_mut(2).unwrap().data = "D";
assert_eq!( tree.to_string(), "a( b c D e )" );

Provides a forward iterator over child nodes

Examples
use trees::potted::{Tree,TreeData,TupleTree};

let tree: Tree<_> = ( "a","b","c","d" ).into();
let mut iter = tree.root().iter();
assert_eq!( iter.next().unwrap().data, "b" );
assert_eq!( iter.next().unwrap().data, "c" );
assert_eq!( iter.next().unwrap().data, "d" );
assert_eq!( iter.next(), None );
assert_eq!( iter.next(), None );

Provides a forward iterator over child nodes with mutable references.

Examples
use trees::potted::{Tree,TreeData,TupleTree};

let mut tree: Tree<_> = ( 0, 1, 2, 3 ).into();
for child in tree.root_mut().iter_mut() {
    child.data *= 10;
}
assert_eq!( tree.root().to_string(), "0( 10 20 30 )" );

Provides a forward iterator in a breadth-first manner

Examples
use trees::{bfs,Size};
use trees::potted::Tree;

let tree: Tree<_> = (0, (1,2,3), (4,5,6) ).into();
let visits = tree.root().bfs().iter.collect::<Vec<_>>();
assert_eq!( visits, vec![
    bfs::Visit{ data: &0, size: Size{ degree: 2, node_cnt: 7 }},
    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 }},
]);

Add the tuple tree as the first child.

Examples
use trees::potted::{Tree,TreeData,TupleTree};

let mut tree: Tree<_> = ( "a", "b", "c" ).into();
tree.root_mut().prepend_tr(( "d", "e", "f" ));
assert_eq!( tree.root().to_string(), "a( d( e f ) b c )" );

Add the tuple tree as the last child.

Examples
use trees::potted::{Tree,TreeData,TupleTree};

let mut tree: Tree<_> = ( "a", "b", "c" ).into();
tree.root_mut().append_tr(( "d", "e", "f" ));
assert_eq!( tree.root().to_string(), "a( b c d( e f ) )" );

Add the tuple forest as the first-n children.

Examples
use trees::potted::{Tree,TreeData,TupleTree,fr};

let mut tree: Tree<_> = ( "a", "b", "c" ).into();
tree.root_mut().prepend_fr(( fr(), "d", "e", "f" ));
assert_eq!( tree.root().to_string(), "a( d e f b c )" );

Add the tuple forest as the last-n children.

Examples
use trees::potted::{Tree,TreeData,TupleTree,fr};

let mut tree: Tree<_> = ( "a", "b", "c" ).into();
tree.root_mut().append_fr(( fr(), "d", "e", "f" ));
assert_eq!( tree.root().to_string(), "a( b c d e f )" );

Insert the tuple tree as the n-th child. Note that it is zero-based index.

Examples
use trees::potted::{Tree,TreeData,TupleTree};

let mut tree: Tree<_> = ( "a", "b", "c", "d", "e" ).into();
tree.root_mut().insert_tr( 2, ("f",) );
assert_eq!( tree.root().to_string(), "a( b c f d e )" );

Drop the first child.

Examples
use trees::potted::{Tree,TreeData,TupleTree};

let mut tree: Tree<_> = ( 1, 2, 3, 4 ).into();
tree.root_mut().drop_front();
assert_eq!( tree.root().to_string(), "1( 3 4 )" );

Drop the last child.

Examples
use trees::potted::{Tree,TreeData,TupleTree};

let mut tree: Tree<_> = ( 1, 2, 3, 4 ).into();
tree.root_mut().drop_back();
assert_eq!( tree.root().to_string(), "1( 2 3 )" );

Drop the n-th child.

Examples
use trees::potted::{Tree,TreeData,TupleTree};

let mut tree: Tree<_> = ( "a", "b", "c", "d", "e" ).into();
tree.root_mut().drop_nth( 2 );
assert_eq!( tree.root().to_string(), "a( b c e )" );

Add tree(s) from a bfs iterator as the first child(ren).

Examples
use trees::linked::fully::tr;
use trees::linked::singly::tr as t;
use trees::potted::{Tree,TreeData,TupleTree};

let mut potted: Tree<_> = ( 0, (1,2), (3,4) ).into();

let linked = tr(5) /tr(6) /tr(7);
potted.root_mut().nth_child_mut(0).unwrap().prepend_bfs( linked.into_bfs().wrap() );
assert_eq!( potted.root().to_string(), "0( 1( 5( 6 7 ) 2 ) 3( 4 ) )" );

let linked = t(8) /t(9) /t(10);
potted.root_mut().nth_child_mut(1).unwrap().prepend_bfs( linked.into_bfs().wrap() );
assert_eq!( potted.root().to_string(), "0( 1( 5( 6 7 ) 2 ) 3( 8( 9 10 ) 4 ) )" );
Examples
use trees::linked::fully::tr;
use trees::linked::singly::tr as t;
use trees::potted::{Tree,TreeData,TupleTree};

let mut potted: Tree<_> = ( 0, (1,2), (3,4) ).into();

let linked = tr(5) -tr(6) -tr(7);
potted.root_mut().nth_child_mut(0).unwrap().prepend_bfs( linked.into_bfs().wrap() );
assert_eq!( potted.root().to_string(), "0( 1( 5 6 7 2 ) 3( 4 ) )" );

//let linked = t(8) /t(9) /t(10);
//potted.root_mut().nth_child_mut(1).unwrap().prepend_bfs( linked.into_bfs() );
//assert_eq!( potted.root().to_string(), "0( 1( 5 6 7 2 ) 3( 8 9 10 4 ) )" );

Add tree(s) from a bfs iterator as the last child(ren).

Examples
use trees::linked::fully::tr;
use trees::linked::singly::tr as t;
use trees::potted::{Tree,TreeData,TupleTree};

let mut potted: Tree<_> = ( 0, (1,2), (3,4) ).into();

let linked = tr(5) /tr(6) /tr(7);
potted.root_mut().nth_child_mut(0).unwrap().append_bfs( linked.into_bfs().wrap() );
assert_eq!( potted.root().to_string(), "0( 1( 2 5( 6 7 ) ) 3( 4 ) )" );

let linked = t(8) /t(9) /t(10);
potted.root_mut().nth_child_mut(1).unwrap().append_bfs( linked.into_bfs().wrap() );
assert_eq!( potted.root().to_string(), "0( 1( 2 5( 6 7 ) ) 3( 4 8( 9 10 ) ) )" );
Examples
use trees::linked::fully::tr;
use trees::linked::singly::tr as t;
use trees::potted::{Tree,TreeData,TupleTree};

let mut potted: Tree<_> = ( 0, (1,2), (3,4) ).into();

let linked = tr(5) -tr(6) -tr(7);
potted.root_mut().nth_child_mut(0).unwrap().append_bfs( linked.into_bfs().wrap() );
assert_eq!( potted.root().to_string(), "0( 1( 2 5 6 7 ) 3( 4 ) )" );

let linked = t(8) -t(9) -t(10);
potted.root_mut().nth_child_mut(1).unwrap().append_bfs( linked.into_bfs().wrap() );
assert_eq!( potted.root().to_string(), "0( 1( 2 5 6 7 ) 3( 4 8 9 10 ) )" );

Provides a forward iterator with mutable references in a breadth-first manner

Examples
use trees::{bfs,Size};
use trees::potted::Tree;

let mut tree: Tree<_> = (0, (1,2,3), (4,5,6) ).into();
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: 7 }},
    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 }},
]);

Trait Implementations

Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Formats the value using the given formatter. Read more
The resulting type after dereferencing.
Dereferences the value.
Mutably dereferences the value.
Formats the value using the given formatter. Read more
Executes the destructor for this type. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.