Skip to main content

Tree

Struct Tree 

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

The type representing a Tree

Implementations§

Source§

impl<T> Tree<T>

Source

pub fn new(data: T) -> Tree<T>

Create a tree only have one node with value

§Example
use xtree::Tree;
let tree = Tree::new(3);

!! Use tr! macro to instead

Source

pub fn add_child(&mut self, child: Tree<T>)

Add a child to root

§Example
let mut t1 = tr!(1);
let t2 = tr!(2);
t1.add_child(t2);

!! Use ‘/’ operator to instead

Source

pub fn cursor(&self) -> Cursor<'_, T>

Get a immutable Cursor which points the root

Examples found in repository?
examples/basic.rs (line 29)
5fn main(){
6    let mut tree =
7        tr!(1)
8            / (tr!(2)
9                / tr!(5)
10                / tr!(6))
11            / (tr!(3)
12                / tr!(7)
13                / tr!(8))
14            / tr!(4);
15
16    let mut cursor = tree.cursor_mut();
17    cursor.move_child(0);
18    *cursor.current() = 10;
19    cursor.move_parent();
20    cursor.move_child(1);
21    let sub_tree = cursor.remove().unwrap();
22    for (depth,node) in sub_tree.df_iter().depth(){
23        for _ in 0..depth {
24            print!("-");
25        }
26        println!("{}",node);
27    }
28
29    let mut cursor = tree.cursor();
30    println!("children of root:");
31    for child in cursor.children() {
32        print!("{} ",child)
33    }
34    println!();
35    println!("root:{}",cursor.current());
36    cursor.move_child(0);
37    println!("first child:{}",cursor.current());
38    cursor.move_parent();
39    cursor.move_child(1);
40    println!("second child:{}",cursor.current());
41
42    for itr in tree.df_iter_mut() {
43        *itr += 1;
44        print!("{} ",itr);
45    }
46    println!();
47
48    for (depth,node) in tree.df_iter().depth(){
49        for _ in 0..depth {
50            print!("-");
51        }
52        println!("{}",node);
53    }
54
55    for itr in tree.bf_iter(){
56        print!("{} ",itr);
57    }
58}
Source

pub fn cursor_mut(&mut self) -> CursorMut<'_, T>

Get a mutable Cursor which points the root

Examples found in repository?
examples/basic.rs (line 16)
5fn main(){
6    let mut tree =
7        tr!(1)
8            / (tr!(2)
9                / tr!(5)
10                / tr!(6))
11            / (tr!(3)
12                / tr!(7)
13                / tr!(8))
14            / tr!(4);
15
16    let mut cursor = tree.cursor_mut();
17    cursor.move_child(0);
18    *cursor.current() = 10;
19    cursor.move_parent();
20    cursor.move_child(1);
21    let sub_tree = cursor.remove().unwrap();
22    for (depth,node) in sub_tree.df_iter().depth(){
23        for _ in 0..depth {
24            print!("-");
25        }
26        println!("{}",node);
27    }
28
29    let mut cursor = tree.cursor();
30    println!("children of root:");
31    for child in cursor.children() {
32        print!("{} ",child)
33    }
34    println!();
35    println!("root:{}",cursor.current());
36    cursor.move_child(0);
37    println!("first child:{}",cursor.current());
38    cursor.move_parent();
39    cursor.move_child(1);
40    println!("second child:{}",cursor.current());
41
42    for itr in tree.df_iter_mut() {
43        *itr += 1;
44        print!("{} ",itr);
45    }
46    println!();
47
48    for (depth,node) in tree.df_iter().depth(){
49        for _ in 0..depth {
50            print!("-");
51        }
52        println!("{}",node);
53    }
54
55    for itr in tree.bf_iter(){
56        print!("{} ",itr);
57    }
58}
Source

pub fn df_iter(&self) -> DfIter<'_, T>

Get a immutable Depth-First iterator

Examples found in repository?
examples/basic.rs (line 22)
5fn main(){
6    let mut tree =
7        tr!(1)
8            / (tr!(2)
9                / tr!(5)
10                / tr!(6))
11            / (tr!(3)
12                / tr!(7)
13                / tr!(8))
14            / tr!(4);
15
16    let mut cursor = tree.cursor_mut();
17    cursor.move_child(0);
18    *cursor.current() = 10;
19    cursor.move_parent();
20    cursor.move_child(1);
21    let sub_tree = cursor.remove().unwrap();
22    for (depth,node) in sub_tree.df_iter().depth(){
23        for _ in 0..depth {
24            print!("-");
25        }
26        println!("{}",node);
27    }
28
29    let mut cursor = tree.cursor();
30    println!("children of root:");
31    for child in cursor.children() {
32        print!("{} ",child)
33    }
34    println!();
35    println!("root:{}",cursor.current());
36    cursor.move_child(0);
37    println!("first child:{}",cursor.current());
38    cursor.move_parent();
39    cursor.move_child(1);
40    println!("second child:{}",cursor.current());
41
42    for itr in tree.df_iter_mut() {
43        *itr += 1;
44        print!("{} ",itr);
45    }
46    println!();
47
48    for (depth,node) in tree.df_iter().depth(){
49        for _ in 0..depth {
50            print!("-");
51        }
52        println!("{}",node);
53    }
54
55    for itr in tree.bf_iter(){
56        print!("{} ",itr);
57    }
58}
Source

pub fn df_iter_mut(&mut self) -> DfIterMut<'_, T>

Get a mutable Depth-First iterator

Examples found in repository?
examples/basic.rs (line 42)
5fn main(){
6    let mut tree =
7        tr!(1)
8            / (tr!(2)
9                / tr!(5)
10                / tr!(6))
11            / (tr!(3)
12                / tr!(7)
13                / tr!(8))
14            / tr!(4);
15
16    let mut cursor = tree.cursor_mut();
17    cursor.move_child(0);
18    *cursor.current() = 10;
19    cursor.move_parent();
20    cursor.move_child(1);
21    let sub_tree = cursor.remove().unwrap();
22    for (depth,node) in sub_tree.df_iter().depth(){
23        for _ in 0..depth {
24            print!("-");
25        }
26        println!("{}",node);
27    }
28
29    let mut cursor = tree.cursor();
30    println!("children of root:");
31    for child in cursor.children() {
32        print!("{} ",child)
33    }
34    println!();
35    println!("root:{}",cursor.current());
36    cursor.move_child(0);
37    println!("first child:{}",cursor.current());
38    cursor.move_parent();
39    cursor.move_child(1);
40    println!("second child:{}",cursor.current());
41
42    for itr in tree.df_iter_mut() {
43        *itr += 1;
44        print!("{} ",itr);
45    }
46    println!();
47
48    for (depth,node) in tree.df_iter().depth(){
49        for _ in 0..depth {
50            print!("-");
51        }
52        println!("{}",node);
53    }
54
55    for itr in tree.bf_iter(){
56        print!("{} ",itr);
57    }
58}
Source

pub fn bf_iter(&self) -> BfIter<'_, T>

Get a immutable Breadth-first iterator

Examples found in repository?
examples/basic.rs (line 55)
5fn main(){
6    let mut tree =
7        tr!(1)
8            / (tr!(2)
9                / tr!(5)
10                / tr!(6))
11            / (tr!(3)
12                / tr!(7)
13                / tr!(8))
14            / tr!(4);
15
16    let mut cursor = tree.cursor_mut();
17    cursor.move_child(0);
18    *cursor.current() = 10;
19    cursor.move_parent();
20    cursor.move_child(1);
21    let sub_tree = cursor.remove().unwrap();
22    for (depth,node) in sub_tree.df_iter().depth(){
23        for _ in 0..depth {
24            print!("-");
25        }
26        println!("{}",node);
27    }
28
29    let mut cursor = tree.cursor();
30    println!("children of root:");
31    for child in cursor.children() {
32        print!("{} ",child)
33    }
34    println!();
35    println!("root:{}",cursor.current());
36    cursor.move_child(0);
37    println!("first child:{}",cursor.current());
38    cursor.move_parent();
39    cursor.move_child(1);
40    println!("second child:{}",cursor.current());
41
42    for itr in tree.df_iter_mut() {
43        *itr += 1;
44        print!("{} ",itr);
45    }
46    println!();
47
48    for (depth,node) in tree.df_iter().depth(){
49        for _ in 0..depth {
50            print!("-");
51        }
52        println!("{}",node);
53    }
54
55    for itr in tree.bf_iter(){
56        print!("{} ",itr);
57    }
58}
Source

pub fn bf_iter_mut(&mut self) -> BfIterMut<'_, T>

Get a mutable Breadth-First iterator

Trait Implementations§

Source§

impl<T> Div for Tree<T>

Source§

type Output = Tree<T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Self) -> Self::Output

Performs the / operation. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Tree<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Tree<T>
where T: RefUnwindSafe,

§

impl<T> Send for Tree<T>
where T: Send,

§

impl<T> Sync for Tree<T>
where T: Sync,

§

impl<T> Unpin for Tree<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Tree<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for Tree<T>
where T: UnwindSafe,

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<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, 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.