-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbinary-trees.links
61 lines (48 loc) · 1.94 KB
/
binary-trees.links
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
# Binary trees
#
# References:
# Benchmark game: binary-trees
# https://benchmarksgame-team.pages.debian.net/benchmarksgame/program/binarytrees-ocaml-2.html
# Corentin Risselin's implementation of the same problem in Python
# https://benchmarksgame-team.pages.debian.net/benchmarksgame/program/binarytrees-python3-5.html
# Troestler Christophe's implementation of the same problem in OCaml
# https://benchmarksgame-team.pages.debian.net/benchmarksgame/program/binarytrees-ocaml-2.html
typename Tree = [|Node:(Tree, Tree)|Nil|];
sig makeTree : (Int) ~> Tree
fun makeTree(d) {
if (d == 0) Node(Nil, Nil)
else Node(makeTree(d-1), makeTree(d-1))
}
sig checkNode : (Tree) ~> Int
fun checkNode(tree) {
switch (tree) {
case Nil -> 0
case Node(l, r) -> 1 + checkNode(l) + checkNode(r)
}
}
sig run : (Int) ~> Int
fun run(d) {
checkNode(makeTree(d))
}
sig loopDepth : (Int, Int, Int) ~> ()
fun loopDepth(d, maxDepth, minDepth) {
var treeCount = 2 ^ (maxDepth + minDepth - d);
var dTreeCount = for (i <- [1 .. treeCount]) [d];
var checkSum = sum(map(run, dTreeCount));
var message = intToString(treeCount) ^^ "\t trees of depth " ^^ intToString(d) ^^ "\t check: " ^^ intToString(checkSum);
println(message)
}
sig main : () ~> ()
fun main() {
var minDepth = 4;
var requestedMaxDepth = stringToInt(getArgs() !! 0);
var maxDepth = maximum(minDepth + 2, requestedMaxDepth);
var stretchDepth = maxDepth + 1;
var checkStretch = checkNode(makeTree(stretchDepth));
println("stretch tree of depth " ^^ intToString(stretchDepth) ^^ "\t check: " ^^ intToString(checkStretch));
var longLivedTree = makeTree(maxDepth);
var testDepth = for (i <- [0 .. (stretchDepth - minDepth)/2]) [minDepth + 2*i];
var dummy = for (d <- testDepth) [loopDepth(d, maxDepth, minDepth)];
println("long lived tree of depth " ^^ intToString(maxDepth) ^^ "\t check: " ^^ intToString(checkNode(longLivedTree)))
}
main()