pub struct Tree<T>{ /* private fields */ }
Expand description
A Tree structure which contains elements that are also trees?
Note: the paraemter T
requires some trait boundaries:
- std::fmt::Display
- std::clone::Clone
- std::cmp::PartialEq
Implementations§
Source§impl<T> Tree<T>
impl<T> Tree<T>
Sourcepub fn new(name: T) -> Tree<T>
pub fn new(name: T) -> Tree<T>
Creates a new structure where name
parameter will be the name element
of the tree. The sub-tree will initial with empty.
Examples found in repository?
examples/atree.rs (line 8)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
fn main() {
let mut root = MyTree::new("root".to_string());
let mut s1 = MyTree::new("s1".to_string());
let mut s1_s1 = MyTree::new("s1_s1".to_string());
let s1_s1_s1 = MyTree::new("s1_s1_s1".to_string());
let s1_s1_s2 = MyTree::new("s1_s1_s2".to_string());
let s1_s2 = MyTree::new("s1_s2".to_string());
let s1_s3 = MyTree::new("s1_s3".to_string());
s1_s1.add(s1_s1_s1);
s1_s1.add(s1_s1_s2);
s1.add(s1_s1);
s1.add(s1_s2);
s1.add(s1_s3);
let s2 = MyTree::new("s2".to_string());
root.add(s1);
root.add(s2);
let printer = tree::TreePrinter::new();
printer.print(&root);
for e in &root {
println!("{}", e);
}
let e = ["root", "s1", "s1_s1"].iter().map(|x| x.to_string()).collect();
let p = tree::Path::from(e);
let r = root.remove(&p);
if r {
println!("It has been removed!");
} else {
println!("Nothing has been removed!");
}
printer.print(&root);
}
Sourcepub fn add(&mut self, sub: Tree<T>)
pub fn add(&mut self, sub: Tree<T>)
Adds a node to the tree where the note is of type Tree<T>
.
Examples found in repository?
examples/atree.rs (line 15)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
fn main() {
let mut root = MyTree::new("root".to_string());
let mut s1 = MyTree::new("s1".to_string());
let mut s1_s1 = MyTree::new("s1_s1".to_string());
let s1_s1_s1 = MyTree::new("s1_s1_s1".to_string());
let s1_s1_s2 = MyTree::new("s1_s1_s2".to_string());
let s1_s2 = MyTree::new("s1_s2".to_string());
let s1_s3 = MyTree::new("s1_s3".to_string());
s1_s1.add(s1_s1_s1);
s1_s1.add(s1_s1_s2);
s1.add(s1_s1);
s1.add(s1_s2);
s1.add(s1_s3);
let s2 = MyTree::new("s2".to_string());
root.add(s1);
root.add(s2);
let printer = tree::TreePrinter::new();
printer.print(&root);
for e in &root {
println!("{}", e);
}
let e = ["root", "s1", "s1_s1"].iter().map(|x| x.to_string()).collect();
let p = tree::Path::from(e);
let r = root.remove(&p);
if r {
println!("It has been removed!");
} else {
println!("Nothing has been removed!");
}
printer.print(&root);
}
pub fn is_leaf(&self) -> bool
Sourcepub fn remove(&mut self, path: &Path<T>) -> bool
pub fn remove(&mut self, path: &Path<T>) -> bool
Remove an element from the Tree as specified by the path
. Returns
true
if the element has been found and removed.
Examples found in repository?
examples/atree.rs (line 36)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
fn main() {
let mut root = MyTree::new("root".to_string());
let mut s1 = MyTree::new("s1".to_string());
let mut s1_s1 = MyTree::new("s1_s1".to_string());
let s1_s1_s1 = MyTree::new("s1_s1_s1".to_string());
let s1_s1_s2 = MyTree::new("s1_s1_s2".to_string());
let s1_s2 = MyTree::new("s1_s2".to_string());
let s1_s3 = MyTree::new("s1_s3".to_string());
s1_s1.add(s1_s1_s1);
s1_s1.add(s1_s1_s2);
s1.add(s1_s1);
s1.add(s1_s2);
s1.add(s1_s3);
let s2 = MyTree::new("s2".to_string());
root.add(s1);
root.add(s2);
let printer = tree::TreePrinter::new();
printer.print(&root);
for e in &root {
println!("{}", e);
}
let e = ["root", "s1", "s1_s1"].iter().map(|x| x.to_string()).collect();
let p = tree::Path::from(e);
let r = root.remove(&p);
if r {
println!("It has been removed!");
} else {
println!("Nothing has been removed!");
}
printer.print(&root);
}
Sourcepub fn get_entry_from_path<'a>(&'a self, path: &Path<T>) -> Option<&'a Tree<T>>
pub fn get_entry_from_path<'a>(&'a self, path: &Path<T>) -> Option<&'a Tree<T>>
Gets an Entry (Tree<T>
) from a given path
.
Trait Implementations§
Source§impl<'a, T> IntoIterator for &'a Tree<T>
non-consuming version IntoIterator trait implementation for the Tree<T>
.
impl<'a, T> IntoIterator for &'a Tree<T>
non-consuming version IntoIterator trait implementation for the Tree<T>
.
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> UnwindSafe for Tree<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<Src, Scheme> ApproxFrom<Src, Scheme> for Srcwhere
Scheme: ApproxScheme,
impl<Src, Scheme> ApproxFrom<Src, Scheme> for Srcwhere
Scheme: ApproxScheme,
Source§fn approx_from(src: Src) -> Result<Src, <Src as ApproxFrom<Src, Scheme>>::Err>
fn approx_from(src: Src) -> Result<Src, <Src as ApproxFrom<Src, Scheme>>::Err>
Convert the given value into an approximately equivalent representation.
Source§impl<Dst, Src, Scheme> ApproxInto<Dst, Scheme> for Srcwhere
Dst: ApproxFrom<Src, Scheme>,
Scheme: ApproxScheme,
impl<Dst, Src, Scheme> ApproxInto<Dst, Scheme> for Srcwhere
Dst: ApproxFrom<Src, Scheme>,
Scheme: ApproxScheme,
Source§type Err = <Dst as ApproxFrom<Src, Scheme>>::Err
type Err = <Dst as ApproxFrom<Src, Scheme>>::Err
The error type produced by a failed conversion.
Source§fn approx_into(self) -> Result<Dst, <Src as ApproxInto<Dst, Scheme>>::Err>
fn approx_into(self) -> Result<Dst, <Src as ApproxInto<Dst, Scheme>>::Err>
Convert the subject into an approximately equivalent representation.
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
Source§impl<T, Dst> ConvAsUtil<Dst> for T
impl<T, Dst> ConvAsUtil<Dst> for T
Source§impl<T> ConvUtil for T
impl<T> ConvUtil for T
Source§fn approx_as<Dst>(self) -> Result<Dst, Self::Err>where
Self: Sized + ApproxInto<Dst>,
fn approx_as<Dst>(self) -> Result<Dst, Self::Err>where
Self: Sized + ApproxInto<Dst>,
Approximate the subject to a given type with the default scheme.
Source§fn approx_as_by<Dst, Scheme>(self) -> Result<Dst, Self::Err>
fn approx_as_by<Dst, Scheme>(self) -> Result<Dst, Self::Err>
Approximate the subject to a given type with a specific scheme.