One Pager Cheat Sheet
- Proficiency in
Breadth First Search(BFS) andDepth First Search(DFS) can greatly improve your ability to solve tree andgraphproblems found in software engineering interviews, and many seemingly unrelated challenges can be mapped astreeorgraphproblems. - A
treeis a hierarchicaldata structurewith a root node and child nodes that can further have their own child nodes connected viaedges, and nodes with no children are calledleafnodes. - We can search a tree using two main techniques:
Breadth First SearchandDepth First Searchwhich have different implementations and benefits. - A tree node without any
descendantsorsubtreeis called a leaf node orterminal node. - Breadth First Search (BFS) is a level-based traversal of a tree's nodes, typically from left to right, allowing for efficient traversal of the right-hand side of the tree.
- We can implement breadth first search to traverse a tree by adding nodes to a
queue, popping items from the queue, printing or processing them, then adding their children to the queue, and repeating until the queue is empty. - The
breadth-first searchalgorithm is implemented in code as shown. - Breadth-First Search (BFS) explores
neighbor nodesbeforedeeper nodesby navigating eachlayerof nodes and their neighbors in any order. - We created a
Tree_Nodeclass and a methodbreadth_first_search(), which takes the root node as input and traverses the tree using BFS. - We create a
dictionarywith keys and values corresponding to a tree's nodes, and use it to initialize aTree_Nodeobject whosevalueis the key of the first element and whosechildrenare its corresponding values. - We can traverse the tree using the
breadth_first_searchmethod by passing in the root node. - You will see the expected sequence
A, B, C, D, E, F, G, H, I, Jin the output, followed by visiting the nodes in order1, 2, 5, 3, 4, 6inPython, JavaScript, C# and Java. - It is recommended to use
BFSin order to find the shortest path between two nodes in an unweighted graph, or when a solution is not far removed from the root of the tree, as opposed to usingDFSin deeper trees with heuristics. Depth First Search(DFS) is a technique for traversing a tree that searches as deeply as possible before moving to neighbor nodes, done through three main methods, such as Preorder Traversal.By adding the left node last to the stack, Preorder Traversal DFS processes the left node first.- The sample code in this article provides implementations of the
Depth-First Searchalgorithm forPython,JavaScript,C#, andJava. - The
depth_first_search()method performs a Preorder Traversal BFS and can be tested using the example tree and its nodes. - We are using In-Order Traversal to traverse the nodes using
left-root-rightlogic in both Breadth-First Search (BFS) and Depth-First Search (DFS) algorithms. - The code outputted a matrix based on DFS algorithm and subsequently, followed a path visiting the elements in order from
1to6. - When the tree is wide but with limited depth and when the need to detect a cycle arises,
DFSis usually a better choice thanBFSin terms of memory efficiency. - BFS is the ideal algorithm for finding the shortest path between any two nodes in an unweighted graph, as it searches level by level and is therefore guaranteed to find the shortest path.

