The Computer Language
24.04 Benchmarks Game

binary-trees Dart #6 program

source code

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

   transliterated from Andrey Filatkin's Node #6 program by Isaac Gouy
*/

void main(List<String> args) {
  final maxDepth = (args.length > 0) ? int.parse(args[0]) : 6;

  final stretchDepth = maxDepth + 1;
  final check = itemCheck(bottomUpTree(stretchDepth));
  print("stretch tree of depth $stretchDepth\t check: $check");

  final longLivedTree = bottomUpTree(maxDepth);

  for (var depth = 4; depth <= maxDepth; depth += 2) {
    final iterations = 1 << maxDepth - depth + 4;
    work(iterations, depth);
  }

  print(
      "long lived tree of depth $maxDepth\t check: ${itemCheck(longLivedTree)}");
}

void work(int iterations, int depth) {
  var check = 0;
  for (int i = 0; i < iterations; i++) {
    check += itemCheck(bottomUpTree(depth));
  }
  print("${iterations}\t trees of depth $depth\t check: $check");
}

class TreeNode {
  TreeNode? left, right;
  TreeNode(this.left, this.right);
}

int itemCheck(TreeNode? node) {
  if (node == null || node.left == null) {
    return 1;
  } else {
    return 1 + itemCheck(node.left) + itemCheck(node.right);
  }
}

TreeNode bottomUpTree(int depth) {
  return depth > 0
      ? TreeNode(bottomUpTree(depth - 1), bottomUpTree(depth - 1))
      : TreeNode(null, null);
}
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
Dart SDK version: 3.3.0 (stable)
Tue Feb 13 10:25:19 2024



 Sat, 02 Mar 2024 03:08:20 GMT

MAKE:
/opt/src/dart-sdk/bin/dart analyze 
Analyzing tmp...
No issues found!

/opt/src/dart-sdk/bin/dart compile exe binarytrees.dartexe-6.dartexe -o binarytrees.dartexe-6.dartexe_run
Generated: /home/dunham/all-benchmarksgame/benchmarksgame_i53330/binarytrees/tmp/binarytrees.dartexe-6.dartexe_run

3.74s to complete and log all make actions

COMMAND LINE:
 ./binarytrees.dartexe-6.dartexe_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