File tree Expand file tree Collapse file tree 4 files changed +70
-57
lines changed Expand file tree Collapse file tree 4 files changed +70
-57
lines changed Original file line number Diff line number Diff line change 1
1
//: Playground - noun: a place where people can play
2
2
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
28
7
29
8
let tree = TreeNode < String > ( value: " beverages " )
30
9
Original file line number Diff line number Diff line change
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
+ }
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1
1
public class TreeNode < T> {
2
- public var value : T
2
+ public var value : T
3
3
4
- public weak var parent : TreeNode ?
5
- public var children = [ TreeNode < T > ] ( )
4
+ public weak var parent : TreeNode ?
5
+ public var children = [ TreeNode < T > ] ( )
6
6
7
- public init ( value: T ) {
8
- self . value = value
9
- }
7
+ public init ( value: T ) {
8
+ self . value = value
9
+ }
10
10
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
+ }
15
15
}
16
16
17
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: " , " ) + " } "
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
22
24
}
23
- return s
24
- }
25
25
}
26
26
27
27
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
31
38
}
32
- for child in children {
33
- if let found = child. search ( value) {
34
- return found
35
- }
36
- }
37
- return nil
38
- }
39
39
}
40
+
You can’t perform that action at this time.
0 commit comments