This repository has been archived by the owner on Nov 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathtree.rs
executable file
·116 lines (98 loc) · 2.88 KB
/
tree.rs
1
2
3
4
5
6
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/// Represents a tree.
///
/// Each internal node can have any number of children. Each node (internal
/// and leaf) carries an `i32`.
#[derive(Debug, Clone)]
enum Tree {
Leaf(i32),
Internal {
children: Vec<Tree>,
data: i32,
},
}
impl Tree {
/// Determines if this tree is just a leaf node.
fn is_leaf(&self) -> bool {
match *self {
Tree::Leaf(_) => true,
_ => false,
}
}
/// Returns the value attached to this node.
fn data(&self) -> i32 {
match *self {
Tree::Leaf(data) => data,
Tree::Internal { data, .. } => data,
}
}
fn data_mut(&mut self) -> &mut i32 {
match *self {
Tree::Leaf(ref mut data) => data,
Tree::Internal { ref mut data, .. } => data,
}
}
/// Adds another tree as a child of this node.
fn add_child(&mut self, child: Tree) {
if let Tree::Leaf(data) = *self {
*self = Tree::Internal {
children: vec![],
data: data,
};
}
match *self {
Tree::Internal { ref mut children, .. } => {
children.push(child);
}
_ => unreachable!(),
}
}
/// Adds another leaf node with the given value as child of this node.
fn add_leaf_child(&mut self, child_data: i32) {
self.add_child(Tree::Leaf(child_data));
}
/// If this node is internal, this function returns a slice of all
/// children.
fn children(&self) -> Option<&[Tree]> {
match *self {
Tree::Internal { ref children, .. } => Some(children),
_ => None,
}
}
/// If this node is internal, this function returns a mutable slice of all
/// children.
fn children_mut(&mut self) -> Option<&mut [Tree]> {
match *self {
Tree::Internal { ref mut children, .. } => Some(children),
_ => None,
}
}
/// Returns a string that contains the values of all nodes in preorder. The
/// values are separated by `separator`.
fn format_preorder(&self, separator: char) -> String {
match *self {
Tree::Leaf(data) => data.to_string(),
Tree::Internal { data, ref children } => {
let mut out = data.to_string();
for child in children {
out.push(separator);
out += &child.format_preorder(separator);
}
out
}
}
}
}
fn main() {
let a = Tree::Leaf(3);
let b = Tree::Leaf(7);
let mut root = Tree::Internal {
children: vec![a, b],
data: 99,
};
for child in root.children_mut().unwrap() {
child.add_leaf_child(123);
child.add_leaf_child(234);
}
println!("{:#?}", root);
println!("{}", root.format_preorder(','));
}