1
1
//: Playground - noun: a place where people can play
2
2
3
3
public indirect enum BinaryTree < T> {
4
- case Node ( BinaryTree < T > , T , BinaryTree < T > )
5
- case Empty
4
+ case node ( BinaryTree < T > , T , BinaryTree < T > )
5
+ case empty
6
6
7
7
public var count : Int {
8
8
switch self {
9
- case let . Node ( left, _, right) :
9
+ case let . node ( left, _, right) :
10
10
return left. count + 1 + right. count
11
- case . Empty :
11
+ case . empty :
12
12
return 0
13
13
}
14
14
}
@@ -17,9 +17,9 @@ public indirect enum BinaryTree<T> {
17
17
extension BinaryTree : CustomStringConvertible {
18
18
public var description : String {
19
19
switch self {
20
- case let . Node ( left, value, right) :
20
+ case let . node ( left, value, right) :
21
21
return " value: \( value) , left = [ " + left. description + " ], right = [ " + right. description + " ] "
22
- case . Empty :
22
+ case . empty :
23
23
return " "
24
24
}
25
25
}
@@ -28,24 +28,24 @@ extension BinaryTree: CustomStringConvertible {
28
28
29
29
30
30
// leaf nodes
31
- let node5 = BinaryTree . Node ( . Empty , " 5 " , . Empty )
32
- let nodeA = BinaryTree . Node ( . Empty , " a " , . Empty )
33
- let node10 = BinaryTree . Node ( . Empty , " 10 " , . Empty )
34
- let node4 = BinaryTree . Node ( . Empty , " 4 " , . Empty )
35
- let node3 = BinaryTree . Node ( . Empty , " 3 " , . Empty )
36
- let nodeB = BinaryTree . Node ( . Empty , " b " , . Empty )
31
+ let node5 = BinaryTree . node ( . empty , " 5 " , . empty )
32
+ let nodeA = BinaryTree . node ( . empty , " a " , . empty )
33
+ let node10 = BinaryTree . node ( . empty , " 10 " , . empty )
34
+ let node4 = BinaryTree . node ( . empty , " 4 " , . empty )
35
+ let node3 = BinaryTree . node ( . empty , " 3 " , . empty )
36
+ let nodeB = BinaryTree . node ( . empty , " b " , . empty )
37
37
38
38
// intermediate nodes on the left
39
- let aMinus10 = BinaryTree . Node ( nodeA, " - " , node10)
40
- let timesLeft = BinaryTree . Node ( node5, " * " , aMinus10)
39
+ let aMinus10 = BinaryTree . node ( nodeA, " - " , node10)
40
+ let timesLeft = BinaryTree . node ( node5, " * " , aMinus10)
41
41
42
42
// intermediate nodes on the right
43
- let minus4 = BinaryTree . Node ( . Empty , " - " , node4)
44
- let divide3andB = BinaryTree . Node ( node3, " / " , nodeB)
45
- let timesRight = BinaryTree . Node ( minus4, " * " , divide3andB)
43
+ let minus4 = BinaryTree . node ( . empty , " - " , node4)
44
+ let divide3andB = BinaryTree . node ( node3, " / " , nodeB)
45
+ let timesRight = BinaryTree . node ( minus4, " * " , divide3andB)
46
46
47
47
// root node
48
- let tree = BinaryTree . Node ( timesLeft, " + " , timesRight)
48
+ let tree = BinaryTree . node ( timesLeft, " + " , timesRight)
49
49
50
50
print ( tree)
51
51
tree. count // 12
@@ -54,23 +54,23 @@ tree.count // 12
54
54
55
55
extension BinaryTree {
56
56
public func traverseInOrder( process: ( T ) -> Void ) {
57
- if case let . Node ( left, value, right) = self {
57
+ if case let . node ( left, value, right) = self {
58
58
left. traverseInOrder ( process: process)
59
59
process ( value)
60
60
right. traverseInOrder ( process: process)
61
61
}
62
62
}
63
63
64
64
public func traversePreOrder( process: ( T ) -> Void ) {
65
- if case let . Node ( left, value, right) = self {
65
+ if case let . node ( left, value, right) = self {
66
66
process ( value)
67
67
left. traversePreOrder ( process: process)
68
68
right. traversePreOrder ( process: process)
69
69
}
70
70
}
71
71
72
72
public func traversePostOrder( process: ( T ) -> Void ) {
73
- if case let . Node ( left, value, right) = self {
73
+ if case let . node ( left, value, right) = self {
74
74
left. traversePostOrder ( process: process)
75
75
right. traversePostOrder ( process: process)
76
76
process ( value)
0 commit comments