binary-trees Dart program
source code
/* The Computer Language Benchmarks game
https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
contributed by Jos Hirth, transliterated from Jarkko Miettinen's Java program
*reset*
*/
final int minDepth = 4;
void main(args){
int n = args.length > 0 ? int.parse(args[0]) : 0;
int maxDepth = (minDepth + 2 > n) ? minDepth + 2 : n;
int stretchDepth = maxDepth + 1;
int check = (TreeNode.bottomUpTree(stretchDepth)).itemCheck();
print("stretch tree of depth $stretchDepth\t check: $check");
TreeNode longLivedTree = TreeNode.bottomUpTree(maxDepth);
for (int depth = minDepth; depth <= maxDepth; depth += 2){
int iterations = 1 << (maxDepth - depth + minDepth);
check = 0;
for (int i = 1; i <= iterations; i++){
check += (TreeNode.bottomUpTree(depth)).itemCheck();
}
print("${iterations}\t trees of depth $depth\t check: $check");
}
print("long lived tree of depth $maxDepth\t check: ${longLivedTree.itemCheck()}");
}
class TreeNode{
TreeNode left, right;
TreeNode([this.left, this.right]);
static TreeNode bottomUpTree(int depth){
if (depth > 0){
return new TreeNode(
bottomUpTree(depth - 1),
bottomUpTree(depth - 1)
);
}
return new TreeNode();
}
int itemCheck(){
if (left == null){
return 1;
}
return 1 + left.itemCheck() + right.itemCheck();
}
}
notes, command-line, and program output
NOTES:
64-bit Ubuntu quad core
Dart VM version: 2.8.1 (stable) (Unknown timestamp) on "linux_x64"
Wed, 06 May 2020 19:17:39 GMT
MAKE:
/usr/bin/dartanalyzer binarytrees.dart
make: /usr/bin/dartanalyzer: Command not found
make: [/home/dunham/8000-benchmarksgame/nanobench/makefiles/u64q.programs.Makefile:445: binarytrees.dart_run] Error 127 (ignored)
0.04s to complete and log all make actions
COMMAND LINE:
/usr/bin/dart binarytrees.dart 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