The Computer Language
24.09 Benchmarks Game

binary-trees Toit #8 program

source code

/* The Computer Language Benchmarks Game
   https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
 
   contributed by Isaac Gouy
*/

class Tree:  
  left /Tree? := ?
  right /Tree? := ?
  
  constructor .left .right:  
  
  static with depth/int -> Tree:
     return depth == 0 
        ? Tree null null
        : Tree (Tree.with depth - 1) (Tree.with depth - 1)
  
  node-count -> int:
     return not this.left 
        ? 1
        : 1 + this.left.node-count + this.right.node-count 
    
main args:
  n := args.size == 1 ? int.parse args[0] : 10
  min-depth := 4
  max-depth := max (min-depth + 2) n
  stretch-depth := max-depth + 1  
  count := (Tree.with stretch-depth).node-count
  print "stretch tree of depth $stretch-depth\t check: $count"     
       
  long-lived-tree := Tree.with max-depth
  for depth := min-depth; depth < stretch-depth; depth += 2: 
    iterations := 1 << max-depth - depth + min-depth  
    count = 0    
    
    iterations.repeat: 
      count += (Tree.with depth).node-count     
    print "$iterations\t trees of depth $depth\t check: $count"        
  
  print "long lived tree of depth $max-depth\t "
      + "check: $(long-lived-tree.node-count)"   
   
    

notes, command-line, and program output

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



 Mon, 07 Oct 2024 19:36:44 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-8.toit_run binarytrees.toit-8.toit 

1.10s to complete and log all make actions

COMMAND LINE:
 ./binarytrees.toit-8.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