April 2017
Intermediate to advanced
316 pages
9h 33m
English
This Tree is far from complete and needs lots of functionality. For instance, we may need to check whether the Tree contains a specific value. To be able to do this, we need to add the following method to our Tree:
static func contains(_ key: Element, tree: Tree<Element>) -> Bool { switch tree { case .leaf(let element): return key == element case node(let lhs, let rhs): return contains(key, tree: lhs) || contains(key, tree: rhs) } }
We can test the contains method as follows:
let isFound = Tree.contains("First", tree: functionalTree) print(isFound) // prints true
Read now
Unlock full access