diff --git a/Tree/Tree.playground/Contents.swift b/Tree/Tree.playground/Contents.swift index d59b7eedb..49cf41054 100644 --- a/Tree/Tree.playground/Contents.swift +++ b/Tree/Tree.playground/Contents.swift @@ -1,30 +1,9 @@ //: Playground - noun: a place where people can play -public class TreeNode { - public var value: T - - public weak var parent: TreeNode? - public var children = [TreeNode]() - - public init(value: T) { - self.value = value - } - - public func addChild(_ node: TreeNode) { - children.append(node) - node.parent = self - } -} - -extension TreeNode: CustomStringConvertible { - public var description: String { - var s = "\(value)" - if !children.isEmpty { - s += " {" + children.map { $0.description }.joined(separator: ", ") + "}" - } - return s - } -} +// last checked with Xcode 9.0b4 +#if swift(>=4.0) +print("Hello, Swift 4!") +#endif let tree = TreeNode(value: "beverages") diff --git a/Tree/Tree.playground/Sources/Tree.swift b/Tree/Tree.playground/Sources/Tree.swift new file mode 100644 index 000000000..fa49f25e9 --- /dev/null +++ b/Tree/Tree.playground/Sources/Tree.swift @@ -0,0 +1,39 @@ +public class TreeNode { + public var value: T + + public weak var parent: TreeNode? + public var children = [TreeNode]() + + public init(value: T) { + self.value = value + } + + public func addChild(_ node: TreeNode) { + children.append(node) + node.parent = self + } +} + +extension TreeNode: CustomStringConvertible { + public var description: String { + var s = "\(value)" + if !children.isEmpty { + s += " {" + children.map { $0.description }.joined(separator: ", ") + "}" + } + return s + } +} + +extension TreeNode where T: Equatable { + public func search(_ value: T) -> TreeNode? { + if value == self.value { + return self + } + for child in children { + if let found = child.search(value) { + return found + } + } + return nil + } +} diff --git a/Tree/Tree.playground/timeline.xctimeline b/Tree/Tree.playground/timeline.xctimeline deleted file mode 100644 index bf468afec..000000000 --- a/Tree/Tree.playground/timeline.xctimeline +++ /dev/null @@ -1,6 +0,0 @@ - - - - - diff --git a/Tree/Tree.swift b/Tree/Tree.swift index fa49f25e9..08cb61eb5 100644 --- a/Tree/Tree.swift +++ b/Tree/Tree.swift @@ -1,39 +1,40 @@ public class TreeNode { - public var value: T + public var value: T - public weak var parent: TreeNode? - public var children = [TreeNode]() + public weak var parent: TreeNode? + public var children = [TreeNode]() - public init(value: T) { - self.value = value - } + public init(value: T) { + self.value = value + } - public func addChild(_ node: TreeNode) { - children.append(node) - node.parent = self - } + public func addChild(_ node: TreeNode) { + children.append(node) + node.parent = self + } } extension TreeNode: CustomStringConvertible { - public var description: String { - var s = "\(value)" - if !children.isEmpty { - s += " {" + children.map { $0.description }.joined(separator: ", ") + "}" + public var description: String { + var s = "\(value)" + if !children.isEmpty { + s += " {" + children.map { $0.description }.joined(separator: ", ") + "}" + } + return s } - return s - } } extension TreeNode where T: Equatable { - public func search(_ value: T) -> TreeNode? { - if value == self.value { - return self + public func search(_ value: T) -> TreeNode? { + if value == self.value { + return self + } + for child in children { + if let found = child.search(value) { + return found + } + } + return nil } - for child in children { - if let found = child.search(value) { - return found - } - } - return nil - } } +