pub struct Tree<T> { /* private fields */ }
Expand description
A tree structure containing Node
s.
Implementations§
Source§impl<T> Tree<T>
impl<T> Tree<T>
Sourcepub fn new() -> Tree<T>
pub fn new() -> Tree<T>
Creates a new Tree
with a capacity of 0.
use slab_tree::tree::Tree;
let tree: Tree<i32> = Tree::new();
Sourcepub fn set_root(&mut self, root: T) -> NodeId
pub fn set_root(&mut self, root: T) -> NodeId
Sets the “root” of the Tree
to be root
.
If there is already a “root” node in the Tree
, that node is shifted down and the new
one takes its place.
use slab_tree::tree::Tree;
let mut tree = Tree::new();
let root_id = tree.set_root(1);
assert_eq!(tree.root_id().unwrap(), root_id);
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the Tree
’s current capacity. Capacity is defined as the number of times new
Node
s can be added to the Tree
before it must allocate more memory.
use slab_tree::tree::Tree;
let tree: Tree<i32> = Tree::new();
assert_eq!(tree.capacity(), 0);
Sourcepub fn root_id(&self) -> Option<NodeId>
pub fn root_id(&self) -> Option<NodeId>
Returns the NodeId
of the root node of the Tree
.
use slab_tree::tree::Tree;
let mut tree = Tree::new();
tree.set_root(1);
let root_id = tree.root_id().expect("root doesn't exist?");
assert_eq!(tree.get(root_id).unwrap().data(), &1);
Examples found in repository?
5fn main() {
6 // "hello"
7 // / \
8 // "world" "trees"
9 // |
10 // "are"
11 // |
12 // "cool"
13
14 let mut tree = TreeBuilder::new().with_root("hello").build();
15 let root_id = tree.root_id().expect("root doesn't exist?");
16 let mut hello = tree.get_mut(root_id).unwrap();
17
18 hello.append("world");
19 hello.append("trees").append("are").append("cool");
20}
Sourcepub fn root(&self) -> Option<NodeRef<'_, T>>
pub fn root(&self) -> Option<NodeRef<'_, T>>
Returns a NodeRef
pointing to the root Node
of the Tree
.
use slab_tree::tree::Tree;
let mut tree = Tree::new();
tree.set_root(1);
let root = tree.root().expect("root doesn't exist?");
assert_eq!(root.data(), &1);
Sourcepub fn root_mut(&mut self) -> Option<NodeMut<'_, T>>
pub fn root_mut(&mut self) -> Option<NodeMut<'_, T>>
Returns a NodeMut
pointing to the root Node
of the Tree
.
use slab_tree::tree::Tree;
let mut tree = Tree::new();
tree.set_root(1);
let mut root = tree.root_mut().expect("root doesn't exist?");
assert_eq!(root.data(), &mut 1);
*root.data() = 2;
assert_eq!(root.data(), &mut 2);
Examples found in repository?
5fn main() {
6 let mut tree = TreeBuilder::new().with_root(0).build();
7 let mut root = tree.root_mut().unwrap();
8 {
9 let mut one = root.append(1);
10 let mut two = one.append(2);
11 two.append(3);
12 two.append(4);
13 }
14 {
15 let mut five = root.append(5);
16 five.append(6).append(7);
17 five.append(8);
18 }
19 root.append(9);
20
21 let mut s = String::new();
22 // 0
23 // ├── 1
24 // │ └── 2
25 // │ ├── 3
26 // │ └── 4
27 // ├── 5
28 // │ ├── 6
29 // │ │ └── 7
30 // │ └── 8
31 // └── 9
32 tree.write_formatted(&mut s).unwrap();
33 print!("{}", s);
34}
Sourcepub fn get(&self, node_id: NodeId) -> Option<NodeRef<'_, T>>
pub fn get(&self, node_id: NodeId) -> Option<NodeRef<'_, T>>
Returns the NodeRef
pointing to the Node
that the given NodeId
identifies. If the
NodeId
in question points to nothing (or belongs to a different Tree
) a None
-value
will be returned; otherwise, a Some
-value will be returned.
use slab_tree::tree::Tree;
let mut tree = Tree::new();
tree.set_root(1);
let root_id = tree.root_id().expect("root doesn't exist?");
let root = tree.get(root_id);
assert!(root.is_some());
let root = root.unwrap();
assert_eq!(root.data(), &1);
Sourcepub fn get_mut(&mut self, node_id: NodeId) -> Option<NodeMut<'_, T>>
pub fn get_mut(&mut self, node_id: NodeId) -> Option<NodeMut<'_, T>>
Returns the NodeMut
pointing to the Node
that the given NodeId
identifies. If the
NodeId
in question points to nothing (or belongs to a different Tree
) a None
-value
will be returned; otherwise, a Some
-value will be returned.
use slab_tree::tree::Tree;
let mut tree = Tree::new();
tree.set_root(1);
let root_id = tree.root_id().expect("root doesn't exist?");
let root = tree.get_mut(root_id);
assert!(root.is_some());
let mut root = root.unwrap();
*root.data() = 2;
assert_eq!(root.data(), &mut 2);
Examples found in repository?
5fn main() {
6 // "hello"
7 // / \
8 // "world" "trees"
9 // |
10 // "are"
11 // |
12 // "cool"
13
14 let mut tree = TreeBuilder::new().with_root("hello").build();
15 let root_id = tree.root_id().expect("root doesn't exist?");
16 let mut hello = tree.get_mut(root_id).unwrap();
17
18 hello.append("world");
19 hello.append("trees").append("are").append("cool");
20}
Sourcepub fn remove(&mut self, node_id: NodeId, behavior: RemoveBehavior) -> Option<T>
pub fn remove(&mut self, node_id: NodeId, behavior: RemoveBehavior) -> Option<T>
Remove a Node
by its NodeId
and return the data that it contained.
Returns a Some
-value if the Node
exists; returns a None
-value otherwise.
Children of the removed Node
can either be dropped with DropChildren
or orphaned with
OrphanChildren
.
use slab_tree::tree::TreeBuilder;
use slab_tree::behaviors::RemoveBehavior::*;
let mut tree = TreeBuilder::new().with_root(1).build();
let two_id = {
let mut root = tree.root_mut().expect("root doesn't exist?");
let two_id = root.append(2).node_id();
root.append(3);
two_id
};
let two = tree.remove(two_id, DropChildren);
assert!(two.is_some());
assert_eq!(two.unwrap(), 2);
let root = tree.root().expect("root doesn't exist?");
assert!(root.first_child().is_some());
assert_eq!(root.first_child().unwrap().data(), &mut 3);
assert!(root.last_child().is_some());
assert_eq!(root.last_child().unwrap().data(), &mut 3);
Source§impl<T: Debug> Tree<T>
impl<T: Debug> Tree<T>
Sourcepub fn write_formatted<W: Write>(&self, w: &mut W) -> Result
pub fn write_formatted<W: Write>(&self, w: &mut W) -> Result
Write formatted tree representation and nodes with debug formatting.
Example:
use slab_tree::tree::TreeBuilder;
let mut tree = TreeBuilder::new().with_root(0).build();
let mut root = tree.root_mut().unwrap();
root.append(1)
.append(2);
root.append(3);
let mut s = String::new();
tree.write_formatted(&mut s).unwrap();
assert_eq!(&s, "\
0
├── 1
│ └── 2
└── 3
");
Writes nothing if the tree is empty.
use slab_tree::tree::TreeBuilder;
let tree = TreeBuilder::<i32>::new().build();
let mut s = String::new();
tree.write_formatted(&mut s).unwrap();
assert_eq!(&s, "");
Examples found in repository?
5fn main() {
6 let mut tree = TreeBuilder::new().with_root(0).build();
7 let mut root = tree.root_mut().unwrap();
8 {
9 let mut one = root.append(1);
10 let mut two = one.append(2);
11 two.append(3);
12 two.append(4);
13 }
14 {
15 let mut five = root.append(5);
16 five.append(6).append(7);
17 five.append(8);
18 }
19 root.append(9);
20
21 let mut s = String::new();
22 // 0
23 // ├── 1
24 // │ └── 2
25 // │ ├── 3
26 // │ └── 4
27 // ├── 5
28 // │ ├── 6
29 // │ │ └── 7
30 // │ └── 8
31 // └── 9
32 tree.write_formatted(&mut s).unwrap();
33 print!("{}", s);
34}