forked from mocobeta/rust99
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
43 lines (40 loc) · 1.04 KB
/
Copy pathlib.rs
File metadata and controls
43 lines (40 loc) · 1.04 KB
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
use bintree::Tree;
use std::fmt;
pub fn leaf_count<T: fmt::Display>(tree: &Tree<T>) -> usize {
match tree {
Tree::Node {
value: _,
left,
right,
} => match (left.as_ref(), right.as_ref()) {
(Tree::End, Tree::End) => 1,
_ => leaf_count(left) + leaf_count(right),
},
Tree::End => 0,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_leaf_count() {
assert_eq!(leaf_count(&Tree::<char>::end()), 0);
assert_eq!(leaf_count(&Tree::leaf('a')), 1);
assert_eq!(
leaf_count(&Tree::node('a', Tree::leaf('b'), Tree::end())),
1
);
assert_eq!(
leaf_count(&Tree::node(
'a',
Tree::node('b', Tree::leaf('d'), Tree::leaf('e')),
Tree::node(
'c',
Tree::end(),
Tree::node('f', Tree::leaf('g'), Tree::end()),
)
)),
3
);
}
}