pub struct Tree<T> { /* private fields */ }Expand description
The type representing a Tree
Implementations§
Source§impl<T> Tree<T>
impl<T> Tree<T>
Sourcepub fn new(data: T) -> Tree<T>
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
Sourcepub fn add_child(&mut self, child: Tree<T>)
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
Sourcepub fn cursor(&self) -> Cursor<'_, T>
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}Sourcepub fn cursor_mut(&mut self) -> CursorMut<'_, T>
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}Sourcepub fn df_iter(&self) -> DfIter<'_, T> ⓘ
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}Sourcepub fn df_iter_mut(&mut self) -> DfIterMut<'_, T> ⓘ
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}Sourcepub fn bf_iter(&self) -> BfIter<'_, T> ⓘ
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}Sourcepub fn bf_iter_mut(&mut self) -> BfIterMut<'_, T> ⓘ
pub fn bf_iter_mut(&mut self) -> BfIterMut<'_, T> ⓘ
Get a mutable Breadth-First iterator
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more