-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy path6.rs
53 lines (43 loc) · 1.45 KB
/
6.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
use std::cmp::max;
enum TreeNode {
Leaf,
Node(Box<TreeNode>, Box<TreeNode>),
}
impl TreeNode {
fn check(&self) -> usize {
match &self {
TreeNode::Leaf => 1,
TreeNode::Node(left, right) => 1 + left.check() + right.check(),
}
}
fn create(depth: usize) -> Box<TreeNode> {
if depth > 0 {
let next_depth = depth - 1;
Box::new(TreeNode::Node(Self::create(next_depth), Self::create(next_depth)))
} else {
Box::new(TreeNode::Leaf)
}
}
}
const MIN_DEPTH: usize = 4;
fn main() {
let n = std::env::args_os()
.nth(1)
.and_then(|s| s.into_string().ok())
.and_then(|n| n.parse().ok())
.unwrap_or(10);
let max_depth = max(MIN_DEPTH + 2, n);
let stretch_depth = max_depth + 1;
println!("stretch tree of depth {}\t check: {}", stretch_depth, TreeNode::create(stretch_depth).check());
let long_lived_tree = TreeNode::create(max_depth);
for iteration_depth in (MIN_DEPTH..stretch_depth).step_by(2) {
let iterations = 1 << (max_depth - iteration_depth + MIN_DEPTH);
let mut nodes = 0;
for _ in 0..iterations {
nodes += TreeNode::create(iteration_depth).check();
}
println!("{iterations}\t trees of depth {iteration_depth}\t check: {nodes}")
}
let nodes = long_lived_tree.check();
println!("long lived tree of depth {max_depth}\t check: {nodes}");
}