Skip to content

Commit

Permalink
Merge pull request #193 from chris-pilcher/tree-swift3
Browse files Browse the repository at this point in the history
Updating Tree to Swift 3
  • Loading branch information
Chris Pilcher committed Sep 6, 2016
2 parents a603c33 + 310f110 commit 5cb6889
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
12 changes: 6 additions & 6 deletions Tree/README.markdown
Expand Up @@ -10,7 +10,7 @@ Nodes have links to their children and usually to their parent as well. The chil

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

A node without a parent is the *root* node. A node without children is a *leaf* node.
A node without a parent is the *root* node. A node without children is a *leaf* node.

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

Expand All @@ -25,15 +25,15 @@ Here's a basic implementation in Swift:
```swift
public class TreeNode<T> {
public var value: T

public var parent: TreeNode?
public var children = [TreeNode<T>]()

public init(value: T) {
self.value = value
}
public func addChild(node: TreeNode<T>) {

public func addChild(_ node: TreeNode<T>) {
children.append(node)
node.parent = self
}
Expand All @@ -49,7 +49,7 @@ extension TreeNode: CustomStringConvertible {
public var description: String {
var s = "\(value)"
if !children.isEmpty {
s += " {" + children.map { $0.description }.joinWithSeparator(", ") + "}"
s += " {" + children.map { $0.description }.joined(separator: ", ") + "}"
}
return s
}
Expand Down Expand Up @@ -129,7 +129,7 @@ Here's the code:

```swift
extension TreeNode where T: Equatable {
func search(value: T) -> TreeNode? {
func search(_ value: T) -> TreeNode? {
if value == self.value {
return self
}
Expand Down
6 changes: 3 additions & 3 deletions Tree/Tree.playground/Contents.swift
Expand Up @@ -10,7 +10,7 @@ public class TreeNode<T> {
self.value = value
}

public func addChild(node: TreeNode<T>) {
public func addChild(_ node: TreeNode<T>) {
children.append(node)
node.parent = self
}
Expand All @@ -20,7 +20,7 @@ extension TreeNode: CustomStringConvertible {
public var description: String {
var s = "\(value)"
if !children.isEmpty {
s += " {" + children.map { $0.description }.joinWithSeparator(", ") + "}"
s += " {" + children.map { $0.description }.joined(separator: ", ") + "}"
}
return s
}
Expand Down Expand Up @@ -71,7 +71,7 @@ teaNode.parent
teaNode.parent!.parent

extension TreeNode where T: Equatable {
func search(value: T) -> TreeNode? {
func search(_ value: T) -> TreeNode? {
if value == self.value {
return self
}
Expand Down
6 changes: 3 additions & 3 deletions Tree/Tree.swift
Expand Up @@ -8,7 +8,7 @@ public class TreeNode<T> {
self.value = value
}

public func addChild(node: TreeNode<T>) {
public func addChild(_ node: TreeNode<T>) {
children.append(node)
node.parent = self
}
Expand All @@ -18,14 +18,14 @@ extension TreeNode: CustomStringConvertible {
public var description: String {
var s = "\(value)"
if !children.isEmpty {
s += " {" + children.map { $0.description }.joinWithSeparator(", ") + "}"
s += " {" + children.map { $0.description }.joined(separator: ", ") + "}"
}
return s
}
}

extension TreeNode where T: Equatable {
public func search(value: T) -> TreeNode? {
public func search(_ value: T) -> TreeNode? {
if value == self.value {
return self
}
Expand Down

0 comments on commit 5cb6889

Please sign in to comment.