Last active
May 14, 2017 23:11
-
-
Save chriswebb09/8c4090b4823dcfff816cc2d0ad79ed91 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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