public snapshot · system/tools/nib/bench/xlang/ · system@main · commit a92f56ab098e · shared 2026-11-22T22:43:57Z· frozen: this page does not change when the repository does
all files
  1package main
  2
  3import "fmt"
  4
  5type Node struct {
  6	left, right *Node
  7}
  8
  9func make_(d int) *Node {
 10	if d > 0 {
 11		return &Node{make_(d - 1), make_(d - 1)}
 12	}
 13	return &Node{}
 14}
 15
 16func check(t *Node) int {
 17	n := 1
 18	if t.left != nil {
 19		n += check(t.left)
 20	}
 21	if t.right != nil {
 22		n += check(t.right)
 23	}
 24	return n
 25}
 26
 27func main() {
 28	const maxDepth = 21
 29	const minDepth = 4
 30	stretch := maxDepth + 1
 31	fmt.Printf("stretch tree of depth %d\t check: %d\n", stretch, check(make_(stretch)))
 32	longLived := make_(maxDepth)
 33	for d := minDepth; d <= maxDepth-1; d += 2 {
 34		iters := 1 << uint(maxDepth-d+minDepth)
 35		sum := 0
 36		for i := 0; i < iters; i++ {
 37			sum += check(make_(d))
 38		}
 39		fmt.Printf("%d\t trees of depth %d\t check: %d\n", iters, d, sum)
 40	}
 41	fmt.Printf("long lived tree of depth %d\t check: %d\n", maxDepth, check(longLived))
 42}