// Allocation: build and walk many binary trees (after the Computer // Language Benchmarks Game's binary-trees). Each tree holds its round's // number, so that no compiler builds it only once. package main import "fmt" type tree struct { leaf bool value int64 left *tree right *tree } func makeTree(d, v int64) *tree { if d == 0 { return &tree{leaf: true, value: v} } return &tree{left: makeTree(d-1, v), right: makeTree(d-1, v)} } func check(t *tree) int64 { if t.leaf { return t.value % 2 } return 1 + check(t.left) + check(t.right) } func pow2(k int64) int64 { if k == 0 { return 1 } return 2 * pow2(k-1) } func repeat(i, d, acc int64) int64 { if i == 0 { return acc } return repeat(i-1, d, acc+check(makeTree(d, i))) } func depths(d, max, acc int64) int64 { if d > max { return acc } return depths(d+2, max, acc+repeat(pow2(max-d+4), d, 0)) } func main() { fmt.Println(depths(4, 16, 0)) }