Skip to content

Commit e36e711

Browse files
committed
[Swift 4] Update Tree
1 parent 5ea9de2 commit e36e711

File tree

4 files changed

+70
-57
lines changed

4 files changed

+70
-57
lines changed

Tree/Tree.playground/Contents.swift

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,9 @@
11
//: Playground - noun: a place where people can play
22

3-
public class TreeNode<T> {
4-
public var value: T
5-
6-
public weak var parent: TreeNode?
7-
public var children = [TreeNode<T>]()
8-
9-
public init(value: T) {
10-
self.value = value
11-
}
12-
13-
public func addChild(_ node: TreeNode<T>) {
14-
children.append(node)
15-
node.parent = self
16-
}
17-
}
18-
19-
extension TreeNode: CustomStringConvertible {
20-
public var description: String {
21-
var s = "\(value)"
22-
if !children.isEmpty {
23-
s += " {" + children.map { $0.description }.joined(separator: ", ") + "}"
24-
}
25-
return s
26-
}
27-
}
3+
// last checked with Xcode 9.0b4
4+
#if swift(>=4.0)
5+
print("Hello, Swift 4!")
6+
#endif
287

298
let tree = TreeNode<String>(value: "beverages")
309

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
public class TreeNode<T> {
2+
public var value: T
3+
4+
public weak var parent: TreeNode?
5+
public var children = [TreeNode<T>]()
6+
7+
public init(value: T) {
8+
self.value = value
9+
}
10+
11+
public func addChild(_ node: TreeNode<T>) {
12+
children.append(node)
13+
node.parent = self
14+
}
15+
}
16+
17+
extension TreeNode: CustomStringConvertible {
18+
public var description: String {
19+
var s = "\(value)"
20+
if !children.isEmpty {
21+
s += " {" + children.map { $0.description }.joined(separator: ", ") + "}"
22+
}
23+
return s
24+
}
25+
}
26+
27+
extension TreeNode where T: Equatable {
28+
public func search(_ value: T) -> TreeNode? {
29+
if value == self.value {
30+
return self
31+
}
32+
for child in children {
33+
if let found = child.search(value) {
34+
return found
35+
}
36+
}
37+
return nil
38+
}
39+
}

Tree/Tree.playground/timeline.xctimeline

Lines changed: 0 additions & 6 deletions
This file was deleted.

Tree/Tree.swift

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,40 @@
11
public class TreeNode<T> {
2-
public var value: T
2+
public var value: T
33

4-
public weak var parent: TreeNode?
5-
public var children = [TreeNode<T>]()
4+
public weak var parent: TreeNode?
5+
public var children = [TreeNode<T>]()
66

7-
public init(value: T) {
8-
self.value = value
9-
}
7+
public init(value: T) {
8+
self.value = value
9+
}
1010

11-
public func addChild(_ node: TreeNode<T>) {
12-
children.append(node)
13-
node.parent = self
14-
}
11+
public func addChild(_ node: TreeNode<T>) {
12+
children.append(node)
13+
node.parent = self
14+
}
1515
}
1616

1717
extension TreeNode: CustomStringConvertible {
18-
public var description: String {
19-
var s = "\(value)"
20-
if !children.isEmpty {
21-
s += " {" + children.map { $0.description }.joined(separator: ", ") + "}"
18+
public var description: String {
19+
var s = "\(value)"
20+
if !children.isEmpty {
21+
s += " {" + children.map { $0.description }.joined(separator: ", ") + "}"
22+
}
23+
return s
2224
}
23-
return s
24-
}
2525
}
2626

2727
extension TreeNode where T: Equatable {
28-
public func search(_ value: T) -> TreeNode? {
29-
if value == self.value {
30-
return self
28+
public func search(_ value: T) -> TreeNode? {
29+
if value == self.value {
30+
return self
31+
}
32+
for child in children {
33+
if let found = child.search(value) {
34+
return found
35+
}
36+
}
37+
return nil
3138
}
32-
for child in children {
33-
if let found = child.search(value) {
34-
return found
35-
}
36-
}
37-
return nil
38-
}
3939
}
40+

0 commit comments

Comments
 (0)