binary-trees Dart #8 program
source code
/* The Computer Language Benchmarks Game
https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
contributed by Isaac Gouy
*/
final class Tree {
Tree? left, right;
Tree (this.left, this.right);
static Tree with_(int depth) {
return (depth == 0)
? new Tree(null, null)
: new Tree( Tree.with_(depth-1), Tree.with_(depth-1));
}
int nodeCount() {
return (identical(left, null))
? 1
: 1 + left!.nodeCount() + right!.nodeCount();
}
void clear(){
if (left != null) {
left?.clear();
left = null;
right?.clear();
right = null;
}
}
}
void main(List<String> args) {
const minDepth = 4;
final maxDepth = (args.length > 0) ? int.parse(args[0]) : 10;
final stretchDepth = maxDepth + 1;
stretch(stretchDepth);
final longLivedTree = Tree.with_(maxDepth);
for (var depth = minDepth; depth < stretchDepth; depth += 2) {
final iterations = 1 << maxDepth - depth + minDepth;
var sum = 0;
for (int i = 0; i < iterations; i++) {
sum += count(depth);
}
print("${iterations}\t trees of depth $depth\t check: $sum");
}
var c = longLivedTree.nodeCount();
longLivedTree.clear();
print("long lived tree of depth $maxDepth\t check: $c");
}
void stretch(int depth){
print("stretch tree of depth $depth\t check: ${count(depth)}");
}
int count(int depth){
var t = Tree.with_(depth);
final c = t.nodeCount();
t.clear();
return c;
}
notes, command-line, and program output
NOTES:
64-bit Ubuntu quad core
Dart SDK version: 3.5.4 (stable)
Wed Oct 16 16:18:51 2024
Wed, 23 Oct 2024 02:23:55 GMT
MAKE:
/opt/src/dart-sdk/bin/dart analyze
Analyzing tmp...
No issues found!
/opt/src/dart-sdk/bin/dart compile exe binarytrees.dartexe-8.dartexe -o binarytrees.dartexe-8.dartexe_run
Generated: /home/dunham/all-benchmarksgame/benchmarksgame_i53330/binarytrees/tmp/binarytrees.dartexe-8.dartexe_run
6.25s to complete and log all make actions
COMMAND LINE:
./binarytrees.dartexe-8.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