Initializing the search function is the first step to the main algorithm. The parameter of the function should include the following two pieces of data needed to perform the search:

  • the root node of the tree
  • the value being searched for

Besides the parameters, the initial function code can take care of the case if the value is not found. To do this, we will write the last line of the function first: return None. From the implementation of the previous exercise, we know that when the value goal_path is None the message No path found is output.



Within bfs.py, initialize a function called bfs(). We’ll use this function to implement our search algorithm.

Inside the body of bfs(), return None.


The search function will require two pieces of data as input, the root node of the tree and the value being searched for.

Add two parameters to the bfs() function definition: root_node and goal_value.


Now in main.py, import the bfs() function from bfs.py.


Lastly, replace the None assigned to goal_path with a call to bfs(). Pass sample_root_node as the first argument and "Z" as the second argument.

When you run main.py this time, the output should be the same. The difference is that goal_path is assigned a None value from the call to bfs().

Sign up to start coding

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?