-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathbinarytrees.jl
More file actions
63 lines (48 loc) · 1.4 KB
/
binarytrees.jl
File metadata and controls
63 lines (48 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# The Computer Language Benchmarks Game
# https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
# contributed by Jarret Revels and Alex Arslan
# based on an OCaml program
# *reset*
using Distributed
using Printf
@everywhere abstract type BTree end
@everywhere struct Node <: BTree
left::Union{Nothing,Node}
right::Union{Nothing,Node}
end
@everywhere function make(d)
if d == 0
Node(nothing, nothing)
else
Node(make(d-1), make(d-1))
end
end
@everywhere check(t::Nothing) = 0
@everywhere check(t::Node) = 1 + check(t.left) + check(t.right)
function loop_depths(min_depth, max_depth)
out = @distributed vcat for d in min_depth:2:max_depth
niter = 1 << (max_depth - d + min_depth)
c = 0
for j = 1:niter
c += check(make(d))
end
@sprintf("%i\t trees of depth %i\t check: %i\n", niter, d, c)
end
for s in out
print(s)
end
end
function perf_binary_trees(N::Int=10)
min_depth = 4
max_depth = N
stretch_depth = max_depth + 1
# create and check stretch tree
let c = check(make(stretch_depth))
@printf("stretch tree of depth %i\t check: %i\n", stretch_depth, c)
end
long_lived_tree = make(max_depth)
loop_depths(min_depth, max_depth)
@printf("long lived tree of depth %i\t check: %i\n", max_depth, check(long_lived_tree))
end
n = parse(Int,ARGS[1])
perf_binary_trees(n)