The Computer Language
Benchmarks Game

binary-trees Go #9 program

source code

/* The Computer Language Benchmarks Game
 * http://benchmarksgame.alioth.debian.org/
 *
 * contributed by Ugorji Nwoke
 */

package main

import (
   "fmt"
   "os"
   "strconv"
)

const minDepth = 4

type treeNode struct {
   left, right *treeNode
   item        int
}

func (n *treeNode) itemCheck() int {
   if n.left == nil {
      return n.item
   }
   return n.item + n.left.itemCheck() - n.right.itemCheck()
}

func bottomUp(item, depth int) *treeNode {
   if depth > 0 {
      return &treeNode{
         bottomUp(2*item-1, depth-1),
         bottomUp(2*item, depth-1),
         item,
      }
   }
   return &treeNode{nil, nil, item}
}

func main() {
   n := 0
   if len(os.Args) > 1 {
      if n2, err2 := strconv.ParseInt(os.Args[1], 10, 0); err2 == nil {
         n = int(n2)
      }
   }
   maxDepth := n
   if minDepth+2 > n {
      maxDepth = minDepth + 2
   }
   stretchDepth := maxDepth + 1
   check := bottomUp(0, stretchDepth).itemCheck()
   fmt.Printf("stretch tree of depth %v\t check: %v\n", stretchDepth, check)
   longLivedTree := bottomUp(0, maxDepth)
   for depth := minDepth; depth <= maxDepth; depth += 2 {
      interactions := 1 << uint(maxDepth-depth+minDepth)
      check = 0
      for i := 1; i <= interactions; i++ {
         check += bottomUp(i, depth).itemCheck()
         check += bottomUp(-i, depth).itemCheck()
      }
      fmt.Printf("%v\t trees of depth %v\t check: %v\n", (interactions * 2), depth, check)
   }
   fmt.Printf("long lived tree of depth %v\t check: %v\n", maxDepth, longLivedTree.itemCheck())
}
    

notes, command-line, and program output

NOTES:
32-bit Ubuntu one core
go version go1.7 linux/386


Tue, 16 Aug 2016 03:36:14 GMT

MAKE:
/usr/local/src/go/bin/go build -o binarytrees.go-9.go_run
0.35s to complete and log all make actions

COMMAND LINE:
./binarytrees.go-9.go_run 20

PROGRAM OUTPUT:
stretch tree of depth 21	 check: -1
2097152	 trees of depth 4	 check: -2097152
524288	 trees of depth 6	 check: -524288
131072	 trees of depth 8	 check: -131072
32768	 trees of depth 10	 check: -32768
8192	 trees of depth 12	 check: -8192
2048	 trees of depth 14	 check: -2048
512	 trees of depth 16	 check: -512
128	 trees of depth 18	 check: -128
32	 trees of depth 20	 check: -32
long lived tree of depth 20	 check: -1