-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy path1.swift
48 lines (41 loc) · 1.33 KB
/
1.swift
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
import Foundation
let minDepth = UInt32(4)
let maxDepth = CommandLine.argc > 1 ? max(UInt32(CommandLine.arguments[1]) ?? 0, minDepth + 2) : 10
let stretchDepth = maxDepth + 1
let check = Tree(depth: stretchDepth).check
print("stretch tree of depth \(stretchDepth)\t check: \(check)")
let longLivingTree = Tree(depth: maxDepth)
for halfDepth in (minDepth / 2)..<(maxDepth / 2 + 1) {
let depth = halfDepth * 2
let iterations = UInt32(1 << (maxDepth - depth + minDepth))
var chk: UInt32 = 0
for _ in 1...iterations {
chk += Tree(depth: depth).check
}
print("\(iterations)\t trees of depth \(depth)\t check: \(chk)")
}
print("long lived tree of depth \(maxDepth)\t check: \(longLivingTree.check)")
indirect enum Tree {
case Empty
case Node(left: Tree, right: Tree)
init(depth: UInt32) {
if depth > 0 {
self = .Node(left: Tree(depth: depth - 1), right: Tree(depth: depth - 1))
} else {
self = .Node(left: .Empty, right: .Empty)
}
}
var check: UInt32 {
switch self {
case .Node(let left, let right):
switch (left, right) {
case (.Empty, .Empty):
return 1
default:
return 1 + left.check + right.check
}
case .Empty:
return 1
}
}
}