Skip to content

Instantly share code, notes, and snippets.

@chriswebb09
Last active May 14, 2017 23:11
Show Gist options
  • Save chriswebb09/8c4090b4823dcfff816cc2d0ad79ed91 to your computer and use it in GitHub Desktop.
Save chriswebb09/8c4090b4823dcfff816cc2d0ad79ed91 to your computer and use it in GitHub Desktop.
class Node<T: Equatable> {
var value: T
weak var parent: Node<T>?
var children: [Node<T>] = []
init(value: T) {
self.value = value
}
func add(child: Node<T>) {
children.append(child)
child.parent = self
}
func search(value: T) -> Node? {
if value == self.value {
return self
}
for child in children {
if let valueFound = child.search(value: value) {
return valueFound
}
}
return nil
}
}
class Tree<T: Equatable> {
var root: Node<T>
init(root: Node<T>) {
self.root = root
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment