forked from kodecocodes/swift-algorithm-club
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5ea9de2
commit e36e711
Showing
4 changed files
with
70 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
|