@@ -20,33 +20,33 @@ Here's how you could implement a general-purpose binary tree in Swift:
20
20
21
21
``` swift
22
22
public indirect enum BinaryTree <T > {
23
- case Node (BinaryTree<T>, T, BinaryTree<T>)
24
- case Empty
23
+ case node (BinaryTree<T>, T, BinaryTree<T>)
24
+ case empty
25
25
}
26
26
```
27
27
28
28
As an example of how to use this, let's build that tree of arithmetic operations:
29
29
30
30
``` swift
31
31
// leaf nodes
32
- let node5 = BinaryTree.Node (. Empty , " 5" , .Empty )
33
- let nodeA = BinaryTree.Node (. Empty , " a" , .Empty )
34
- let node10 = BinaryTree.Node (. Empty , " 10" , .Empty )
35
- let node4 = BinaryTree.Node (. Empty , " 4" , .Empty )
36
- let node3 = BinaryTree.Node (. Empty , " 3" , .Empty )
37
- let nodeB = BinaryTree.Node (. Empty , " b" , .Empty )
32
+ let node5 = BinaryTree.node (. empty , " 5" , .empty )
33
+ let nodeA = BinaryTree.node (. empty , " a" , .empty )
34
+ let node10 = BinaryTree.node (. empty , " 10" , .empty )
35
+ let node4 = BinaryTree.node (. empty , " 4" , .empty )
36
+ let node3 = BinaryTree.node (. empty , " 3" , .empty )
37
+ let nodeB = BinaryTree.node (. empty , " b" , .empty )
38
38
39
39
// intermediate nodes on the left
40
- let Aminus10 = BinaryTree.Node (nodeA, " -" , node10)
41
- let timesLeft = BinaryTree.Node (node5, " *" , Aminus10)
40
+ let Aminus10 = BinaryTree.node (nodeA, " -" , node10)
41
+ let timesLeft = BinaryTree.node (node5, " *" , Aminus10)
42
42
43
43
// intermediate nodes on the right
44
- let minus4 = BinaryTree.Node (. Empty , " -" , node4)
45
- let divide3andB = BinaryTree.Node (node3, " /" , nodeB)
46
- let timesRight = BinaryTree.Node (minus4, " *" , divide3andB)
44
+ let minus4 = BinaryTree.node (. empty , " -" , node4)
45
+ let divide3andB = BinaryTree.node (node3, " /" , nodeB)
46
+ let timesRight = BinaryTree.node (minus4, " *" , divide3andB)
47
47
48
48
// root node
49
- let tree = BinaryTree.Node (timesLeft, " +" , timesRight)
49
+ let tree = BinaryTree.node (timesLeft, " +" , timesRight)
50
50
```
51
51
52
52
You need to build up the tree in reverse, starting with the leaf nodes and working your way up to the top.
@@ -57,10 +57,10 @@ It will be useful to add a `description` method so you can print the tree:
57
57
extension BinaryTree : CustomStringConvertible {
58
58
public var description: String {
59
59
switch self {
60
- case let .Node (left, value, right):
60
+ case let .node (left, value, right):
61
61
return " value: \( value ) , left = [" + left.description + " ], right = ["
62
62
+ right.description + " ]"
63
- case .Empty :
63
+ case .empty :
64
64
return " "
65
65
}
66
66
}
@@ -92,9 +92,9 @@ Another useful method is counting the number of nodes in the tree:
92
92
``` swift
93
93
public var count: Int {
94
94
switch self {
95
- case let .Node (left, _ , right):
95
+ case let .node (left, _ , right):
96
96
return left.count + 1 + right.count
97
- case .Empty :
97
+ case .empty :
98
98
return 0
99
99
}
100
100
}
@@ -112,23 +112,23 @@ Here is how you'd implement that:
112
112
113
113
``` swift
114
114
public func traverseInOrder (process : (T) -> Void ) {
115
- if case let .Node (left, value, right) = self {
115
+ if case let .node (left, value, right) = self {
116
116
left.traverseInOrder (process : process)
117
117
process (value)
118
118
right.traverseInOrder (process : process)
119
119
}
120
120
}
121
121
122
122
public func traversePreOrder (process : (T) -> Void ) {
123
- if case let .Node (left, value, right) = self {
123
+ if case let .node (left, value, right) = self {
124
124
process (value)
125
125
left.traversePreOrder (process : process)
126
126
right.traversePreOrder (process : process)
127
127
}
128
128
}
129
129
130
130
public func traversePostOrder (process : (T) -> Void ) {
131
- if case let .Node (left, value, right) = self {
131
+ if case let .node (left, value, right) = self {
132
132
left.traversePostOrder (process : process)
133
133
right.traversePostOrder (process : process)
134
134
process (value)
0 commit comments