Log in from a computer to take this course

You'll need to log in from a computer to start Introduction to Non-linear Data Structures in Swift. But you can practice or keep up your coding streak with the Codecademy Go app. Download the app to get started.

apple storegoogle store
Learn

Our implementation of a searching function will provide the user with a boolean value as to whether the value is in the tree or not. Since it provides the user with this type of answer, we will call the function contains(_:). Similar to how the add function we created was implemented, we will have a private variation, contains(_:startingAt:) that will do the heavy lifting (recursion) behind the scenes.

Since this private function will use a standard recursive algorithm, let’s talk about our base case:

  • When the node we are searching is nil, that is we’ve searched all the way to the end of a tree and can’t find the element, we will return false.
  • If we reach a node that is not nil and its value is neither less than or greater than the value, it must equal the value and we return true.

If we don’t reach a base case, call the function on the left child if the value is less than the parent or the right child if it is greater than the parent.

The public function will take a value from the user and simply call the private function beginning at the root of the tree.

Instructions

1.

We’ll begin the private function first. Below the multiline comment for contains in the private functions area, create a new function, contains(_:startingAt:) that has two parameters, value of type T with no argument label, and node of which will be a BinaryNode<T> optional and labeled startingAt. The function will return a Bool. Inside the function body, return false.

2.

Inside the function, above the return statement, create a guard-let statement. It should unwrap node as parent. In the else clause it should return false.

This statement is responsible for handling our first base case, getting to a non-existent node.

3.

Underneath the guard-let statement, create a new variable, valueFound and set it equal to false.

Change the return statement at the end of the function from false to valueFound.

4.

Between valueFounds declaration/initialization and the return statement you will create an if-else statement that will run our recursive logic and final base case:

  • if the value is less than the parent’s data, valueFound will equal the result of calling contains on the value, starting at the parent’s left child.
  • else-if the value is greater than the parent’s data, valueFound will equal the result of calling contains on the value, starting at the parent’s right child.
  • else, we’ve found the value and valueFound equals true.
5.

Below the comments for contains in the public functions section, create a new function, contains(_:) that has one parameter, value of type T, it should omit an argument label. The function returns a Bool.

Inside the function body, call the private contains(_:startingAt:) function we just created, passing in value and root.

6.

With our contains functions complete, add a few more numbers of your choosing to the numberTree at the bottom of the file and make a few calls to your public contains(_:) function. Make sure to wrap the function call inside a print statement so you can see the result.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?