File tree Expand file tree Collapse file tree 3 files changed +12
-12
lines changed Expand file tree Collapse file tree 3 files changed +12
-12
lines changed Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ Nodes have links to their children and usually to their parent as well. The chil
10
10
11
11
![ A tree] ( Images/ParentChildren.png )
12
12
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.
14
14
15
15
The pointers in a tree do not form cycles. This is not a tree:
16
16
@@ -25,15 +25,15 @@ Here's a basic implementation in Swift:
25
25
``` swift
26
26
public class TreeNode <T > {
27
27
public var value: T
28
-
28
+
29
29
public var parent: TreeNode?
30
30
public var children = [TreeNode< T> ]()
31
31
32
32
public init (value : T) {
33
33
self .value = value
34
34
}
35
-
36
- public func addChild (node : TreeNode<T>) {
35
+
36
+ public func addChild (_ node : TreeNode<T>) {
37
37
children.append (node)
38
38
node.parent = self
39
39
}
@@ -49,7 +49,7 @@ extension TreeNode: CustomStringConvertible {
49
49
public var description: String {
50
50
var s = " \( value ) "
51
51
if ! children.isEmpty {
52
- s += " {" + children.map { $0 .description }.joinWithSeparator ( " , " ) + " }"
52
+ s += " {" + children.map { $0 .description }.joined ( separator : " , " ) + " }"
53
53
}
54
54
return s
55
55
}
@@ -129,7 +129,7 @@ Here's the code:
129
129
130
130
``` swift
131
131
extension TreeNode where T: Equatable {
132
- func search (value : T) -> TreeNode? {
132
+ func search (_ value : T) -> TreeNode? {
133
133
if value == self .value {
134
134
return self
135
135
}
Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ public class TreeNode<T> {
10
10
self . value = value
11
11
}
12
12
13
- public func addChild( node: TreeNode < T > ) {
13
+ public func addChild( _ node: TreeNode < T > ) {
14
14
children. append ( node)
15
15
node. parent = self
16
16
}
@@ -20,7 +20,7 @@ extension TreeNode: CustomStringConvertible {
20
20
public var description : String {
21
21
var s = " \( value) "
22
22
if !children. isEmpty {
23
- s += " { " + children. map { $0. description } . joinWithSeparator ( " , " ) + " } "
23
+ s += " { " + children. map { $0. description } . joined ( separator : " , " ) + " } "
24
24
}
25
25
return s
26
26
}
@@ -71,7 +71,7 @@ teaNode.parent
71
71
teaNode. parent!. parent
72
72
73
73
extension TreeNode where T: Equatable {
74
- func search( value: T ) -> TreeNode ? {
74
+ func search( _ value: T ) -> TreeNode ? {
75
75
if value == self . value {
76
76
return self
77
77
}
Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ public class TreeNode<T> {
8
8
self . value = value
9
9
}
10
10
11
- public func addChild( node: TreeNode < T > ) {
11
+ public func addChild( _ node: TreeNode < T > ) {
12
12
children. append ( node)
13
13
node. parent = self
14
14
}
@@ -18,14 +18,14 @@ extension TreeNode: CustomStringConvertible {
18
18
public var description : String {
19
19
var s = " \( value) "
20
20
if !children. isEmpty {
21
- s += " { " + children. map { $0. description } . joinWithSeparator ( " , " ) + " } "
21
+ s += " { " + children. map { $0. description } . joined ( separator : " , " ) + " } "
22
22
}
23
23
return s
24
24
}
25
25
}
26
26
27
27
extension TreeNode where T: Equatable {
28
- public func search( value: T ) -> TreeNode ? {
28
+ public func search( _ value: T ) -> TreeNode ? {
29
29
if value == self . value {
30
30
return self
31
31
}
You can’t perform that action at this time.
0 commit comments