O'Reilly logo

Swift Functional Programming - Second Edition by Dr. Fatih Nayebi

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Empty

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> {  ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required