April 2017
Intermediate to advanced
316 pages
9h 33m
English
We can implement a helper method to generate empty BSTs as follows:
static func empty() -> BST { return .leaf}
The following is a computed property to check whether the BST is empty:
var isEmpty: Bool { switch self{ case .leaf: return true case .node(_, _, _): return false }}
Let's test these functions and computed properties:
let emptyBST = BST<Int>.empty()print(emptyBST.isEmpty)
In the preceding code, we created an empty BST and checked whether it is empty using the isEmpty property. Obviously, the result is going to be true.
This BST implementation is far from complete and needs to be improved by implementing methods to check whether it is a BST.
At the end, our BST becomes the following:
enum BST<Element: Comparable> { ...Read now
Unlock full access