binary-trees Dart exe 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) (Thu Apr 30 09:25:21 2020 +0200) on "linux_x64"
Thu, 07 May 2020 04:27:50 GMT
MAKE:
/opt/src/dartsdk-linux-x64-release/dart-sdk/bin/dartanalyzer binarytrees.dartexe
Analyzing binarytrees.dartexe...
No issues found!
/opt/src/dartsdk-linux-x64-release/dart-sdk/bin/dart2native -k exe binarytrees.dartexe -o binarytrees.dartexe_run
Generated: /home/dunham/benchmarksgame_quadcore/binarytrees/tmp/binarytrees.dartexe_run
12.21s to complete and log all make actions
COMMAND LINE:
./binarytrees.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