1
+ use std;
2
+
3
+ import std. _int ;
4
+
1
5
tag tree {
2
6
nil;
3
7
node ( @tree, @tree, int) ;
@@ -14,5 +18,57 @@ fn item_check(@tree t) -> int {
14
18
}
15
19
}
16
20
21
+ fn bottom_up_tree ( int item , int depth ) -> @tree {
22
+ if ( depth > 0 ) {
23
+ ret @node ( bottom_up_tree ( 2 * item - 1 , depth - 1 ) ,
24
+ bottom_up_tree ( 2 * item, depth - 1 ) ,
25
+ item) ;
26
+ } else {
27
+ ret @nil;
28
+ }
29
+ }
30
+
17
31
fn main ( ) {
32
+
33
+ auto n = 8 ;
34
+ auto min_depth = 4 ;
35
+ auto max_depth;
36
+ if ( min_depth + 2 > n) {
37
+ max_depth = min_depth + 2 ;
38
+ } else {
39
+ max_depth = n;
40
+ }
41
+
42
+ auto stretch_depth = max_depth + 1 ;
43
+
44
+ auto stretch_tree = bottom_up_tree ( 0 , stretch_depth) ;
45
+ log #fmt( "stretch tree of depth %d\t check: %d" ,
46
+ stretch_depth, item_check ( stretch_tree) ) ;
47
+
48
+ auto long_lived_tree = bottom_up_tree ( 0 , max_depth) ;
49
+
50
+ auto depth = min_depth;
51
+ while ( depth <= max_depth) {
52
+ auto iterations = _int. pow ( 2 , ( max_depth - depth + min_depth) as uint ) ;
53
+ auto chk = 0 ;
54
+
55
+ auto i = 1 ;
56
+ while ( i <= iterations) {
57
+ auto temp_tree = bottom_up_tree ( i, depth) ;
58
+ chk += item_check ( temp_tree) ;
59
+
60
+ temp_tree = bottom_up_tree ( -i, depth) ;
61
+ chk += item_check ( temp_tree) ;
62
+
63
+ i += 1 ;
64
+ }
65
+
66
+ log #fmt( "%d\t trees of depth %d\t check: %d" ,
67
+ iterations * 2 , depth, chk) ;
68
+
69
+ depth += 2 ;
70
+ }
71
+
72
+ log #fmt( "long lived trees of depth %d\t check: %d" ,
73
+ max_depth, item_check ( long_lived_tree) ) ;
18
74
}
0 commit comments