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
clear:
if this.left != null:
this.left.clear
this.left = null
this.right.clear
this.right = null
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
stretch stretch_depth
long-lived-tree := Tree.with max-depth
for depth := min-depth; depth < stretch-depth; depth += 2:
iterations := 1 << max-depth - depth + min-depth
sum := 0
iterations.repeat:
sum += count depth
print "$iterations\t trees of depth $depth\t check: $sum"
c := long_lived_tree.node_count
long_lived_tree.clear
print "long lived tree of depth $max-depth\t " + "check: $(c)"
stretch depth:
print "stretch tree of depth $depth\t check: $(count depth)"
count depth:
t := Tree.with depth
c := t.node_count
t.clear
return c
notes, command-line, and program output
NOTES:
64-bit Ubuntu quad core
v2.0.0-alpha.174
Fri, 13 Dec 2024 00:25:24 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
0.79s 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