Skip to content

Commit

Permalink
[Swift 4] Update Tree
Browse files Browse the repository at this point in the history
  • Loading branch information
remlostime committed Jul 30, 2017
1 parent 5ea9de2 commit e36e711
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 57 deletions.
29 changes: 4 additions & 25 deletions Tree/Tree.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,30 +1,9 @@
//: Playground - noun: a place where people can play

public class TreeNode<T> {
public var value: T

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

public init(value: T) {
self.value = value
}

public func addChild(_ node: TreeNode<T>) {
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<String>(value: "beverages")

Expand Down
39 changes: 39 additions & 0 deletions Tree/Tree.playground/Sources/Tree.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
public class TreeNode<T> {
public var value: T

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

public init(value: T) {
self.value = value
}

public func addChild(_ node: TreeNode<T>) {
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
}
}
6 changes: 0 additions & 6 deletions Tree/Tree.playground/timeline.xctimeline

This file was deleted.

53 changes: 27 additions & 26 deletions Tree/Tree.swift
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
public class TreeNode<T> {
public var value: T
public var value: T

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

public init(value: T) {
self.value = value
}
public init(value: T) {
self.value = value
}

public func addChild(_ node: TreeNode<T>) {
children.append(node)
node.parent = self
}
public func addChild(_ node: TreeNode<T>) {
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
}
}

0 comments on commit e36e711

Please sign in to comment.