forked from fsprojects/fantomas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binarytrees2.fs
53 lines (46 loc) · 1.46 KB
/
binarytrees2.fs
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
/// The Computer Language Benchmarks Game
/// http://shootout.alioth.debian.org/
///
/// Minor modification by Don Syme & Jomo Fisher to use null as representation
/// of Empty node.
/// Based on F# version by Robert Pickering
/// Based on ocaml version by Troestler Christophe & Isaac Gouy
module BinaryTrees2
[<CompilationRepresentation(CompilationRepresentationFlags.UseNullAsTrueValue)>]
type Tree<'T> =
| Empty
| Node of Tree<'T> * 'T * Tree<'T>
let rec make i d =
if d = 0 then Node(Empty, i, Empty)
else
let i2 = 2 * i
let d = d - 1
Node(make (i2 - 1) d, i, make i2 d)
let rec check x =
match x with
| Empty -> 0
| Node(l, i, r) -> i + check l - check r
let rec loopDepths maxDepth minDepth d =
if d <= maxDepth then
let niter = 1 <<< (maxDepth - d + minDepth)
let mutable c = 0
for i = 1 to niter do
c <- c + check (make i d) + check (make (-i) d)
printf "%i\t trees of depth %i\t check: %i\n" (2 * niter) d c
loopDepths maxDepth minDepth (d + 2)
[<EntryPoint>]
let main args =
let minDepth = 4
let maxDepth =
let n =
if args.Length > 0 then int args.[0]
else 10
max (minDepth + 2) n
let stretchDepth = maxDepth + 1
let c = check (make 0 stretchDepth)
printf "stretch tree of depth %i\t check: %i\n" stretchDepth c
let longLivedTree = make 0 maxDepth
loopDepths maxDepth minDepth minDepth
printf "long lived tree of depth %i\t check: %i\n" maxDepth
(check longLivedTree)
0