The Computer Language
24.04 Benchmarks Game

binary-trees Toit #2 program

source code

/* The Computer Language Benchmarks game
   https://salsa.debian.org/benchmarksgame-team/benchmarksgame/

   transliterated from Antoine Pitrou's Python program by Isaac Gouy
   */  
  
make-tree depth/int -> List:
  if depth == 0: return [null, null]
  depth -= 1
  return [make-tree depth, make-tree depth]
  
count-nodes node/List -> int:
  left := node[0]
  if not left: return 1
  right := node[1]  
  return 1 + (count-nodes left) + (count-nodes right)
    
main args:
  n := args.size == 1 ? int.parse args[0] : 0
  min-depth := 4
  max-depth := max (min-depth + 2) n
  stretch-depth := max-depth + 1  
  
  print "stretch tree of depth $stretch-depth\t "
      + "check: $(count-nodes (make-tree stretch-depth))"     
       
  long-lived-tree := make-tree max-depth
  
  for depth := min-depth; depth <= max-depth; depth += 2: 
    iterations := 1 << max-depth - depth + min-depth  
    check := 0    
    iterations.repeat: check += count-nodes (make-tree depth)      
    print "$iterations\t trees of depth $depth\t check: $check"        
  
  print "long lived tree of depth $max-depth\t "
      + "check: $(count-nodes long-lived-tree)"   
   
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
v2.0.0-alpha.146



 Wed, 24 Apr 2024 21:41:30 GMT

MAKE:
cp -r Include/toit/.packages .
cp -r Include/toit/package.lock .
cp -r Include/toit/package.yaml .	
/opt/src/toit/bin/toit.compile -O2 --strip -o binarytrees.toit-2.toit_run binarytrees.toit-2.toit 

1.37s to complete and log all make actions

COMMAND LINE:
 ./binarytrees.toit-2.toit_run 21

PROGRAM OUTPUT:
stretch tree of depth 22	 check: 8388607
2097152	 trees of depth 4	 check: 65011712
524288	 trees of depth 6	 check: 66584576
131072	 trees of depth 8	 check: 66977792
32768	 trees of depth 10	 check: 67076096
8192	 trees of depth 12	 check: 67100672
2048	 trees of depth 14	 check: 67106816
512	 trees of depth 16	 check: 67108352
128	 trees of depth 18	 check: 67108736
32	 trees of depth 20	 check: 67108832
long lived tree of depth 21	 check: 4194303