# binary-trees Julia #4 program

## source code

# The Computer Language Benchmarks Game
# https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
#
# Contributed by Adam Beckmeyer. Based on code by Jarret Revels, Alex
# Arslan, Michal Stransky, Jens Adam.

using Distributed

@everywhere struct Node
l::Union{Node,Nothing}
r::Union{Node,Nothing}
end

@everywhere make(n) =
n === 0 ? Node(nothing, nothing) : Node(make(n-1), make(n-1))

@everywhere check(node) =
node.l === nothing ? 1 : 1 + check(node.l) + check(node.r)

function binary_trees(io, n)
write(io, "stretch tree of depth \$(n+1)\t check: \$(check(make(n+1)))\n")

long_tree = make(n)

d = 4
while d <= n
niter = 1 << (n - d + 4)
c = @distributed (+) for _ in 1:niter
check(make(d))
end#for
write(io, "\$niter\t trees of depth \$d\t check: \$c\n")
d += 2
end

write(io, "long lived tree of depth \$n\t check: \$(check(long_tree))\n")
end#function

isinteractive() || binary_trees(stdout, parse(Int, ARGS[1]))

## notes, command-line, and program output

NOTES:
julia version build 19.0.1+10-21

Wed, 25 Jan 2023 00:59:38 GMT

MAKE:
4

0.13s to complete and log all make actions

COMMAND LINE:
/opt/src/julia-1.8.5/bin/julia -O3 --cpu-target=ivybridge --math-mode=ieee -p4 -- binarytrees.julia-4.julia 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