Congratulations on implementing a breadth-first search algorithm using Python!
Let’s go over the steps taken to create a breadth-first search function:
- Before starting the search, define the tree’s root node as well as a value to search for.
- Initialize a frontier queue, which holds a path for each node to search
- Loop through the frontier queue to check if the node value matches the value we are searching for.
- Continue the search by adding child nodes to the frontier until the goal has been found, or the tree has been completely searched.
You’ve now implemented a breadth-first search using an iterative approach.
Feel free to use the
TreeNode class to make your own tree and try it out on the search.