Skip to content

Commit 5cb6889

Browse files
author
Chris Pilcher
authored
Merge pull request kodecocodes#193 from chris-pilcher/tree-swift3
Updating Tree to Swift 3
2 parents a603c33 + 310f110 commit 5cb6889

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

Tree/README.markdown

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Nodes have links to their children and usually to their parent as well. The chil
1010

1111
![A tree](Images/ParentChildren.png)
1212

13-
A node without a parent is the *root* node. A node without children is a *leaf* node.
13+
A node without a parent is the *root* node. A node without children is a *leaf* node.
1414

1515
The pointers in a tree do not form cycles. This is not a tree:
1616

@@ -25,15 +25,15 @@ Here's a basic implementation in Swift:
2525
```swift
2626
public class TreeNode<T> {
2727
public var value: T
28-
28+
2929
public var parent: TreeNode?
3030
public var children = [TreeNode<T>]()
3131

3232
public init(value: T) {
3333
self.value = value
3434
}
35-
36-
public func addChild(node: TreeNode<T>) {
35+
36+
public func addChild(_ node: TreeNode<T>) {
3737
children.append(node)
3838
node.parent = self
3939
}
@@ -49,7 +49,7 @@ extension TreeNode: CustomStringConvertible {
4949
public var description: String {
5050
var s = "\(value)"
5151
if !children.isEmpty {
52-
s += " {" + children.map { $0.description }.joinWithSeparator(", ") + "}"
52+
s += " {" + children.map { $0.description }.joined(separator: ", ") + "}"
5353
}
5454
return s
5555
}
@@ -129,7 +129,7 @@ Here's the code:
129129

130130
```swift
131131
extension TreeNode where T: Equatable {
132-
func search(value: T) -> TreeNode? {
132+
func search(_ value: T) -> TreeNode? {
133133
if value == self.value {
134134
return self
135135
}

Tree/Tree.playground/Contents.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class TreeNode<T> {
1010
self.value = value
1111
}
1212

13-
public func addChild(node: TreeNode<T>) {
13+
public func addChild(_ node: TreeNode<T>) {
1414
children.append(node)
1515
node.parent = self
1616
}
@@ -20,7 +20,7 @@ extension TreeNode: CustomStringConvertible {
2020
public var description: String {
2121
var s = "\(value)"
2222
if !children.isEmpty {
23-
s += " {" + children.map { $0.description }.joinWithSeparator(", ") + "}"
23+
s += " {" + children.map { $0.description }.joined(separator: ", ") + "}"
2424
}
2525
return s
2626
}
@@ -71,7 +71,7 @@ teaNode.parent
7171
teaNode.parent!.parent
7272

7373
extension TreeNode where T: Equatable {
74-
func search(value: T) -> TreeNode? {
74+
func search(_ value: T) -> TreeNode? {
7575
if value == self.value {
7676
return self
7777
}

Tree/Tree.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class TreeNode<T> {
88
self.value = value
99
}
1010

11-
public func addChild(node: TreeNode<T>) {
11+
public func addChild(_ node: TreeNode<T>) {
1212
children.append(node)
1313
node.parent = self
1414
}
@@ -18,14 +18,14 @@ extension TreeNode: CustomStringConvertible {
1818
public var description: String {
1919
var s = "\(value)"
2020
if !children.isEmpty {
21-
s += " {" + children.map { $0.description }.joinWithSeparator(", ") + "}"
21+
s += " {" + children.map { $0.description }.joined(separator: ", ") + "}"
2222
}
2323
return s
2424
}
2525
}
2626

2727
extension TreeNode where T: Equatable {
28-
public func search(value: T) -> TreeNode? {
28+
public func search(_ value: T) -> TreeNode? {
2929
if value == self.value {
3030
return self
3131
}

0 commit comments

Comments
 (0)