Depth-First and Breadth-First Search
🎯 Introduction
Learn to understand and implement depth-first search (DFS) and breadth-first search (BFS) using trees. You will distinguish their approaches and use cases and learn to handle tree-shaped data in frontend projects, including component trees and menu structures.
🌳 What Is a Tree Structure?
Trees appear frequently in frontend development, including DOM trees, component trees, and directory trees.
A tree consists of nodes, each with zero or more children. The nodes form a hierarchy with no cycles.
Here is a simple tree:
A
/ \
B C
/ \ \
D E F
- Node A is the root, where the tree begins.
- Nodes B and C are children of A.
- Nodes D and E are children of B; F is a child of C.
🔍 Depth-First Search (DFS) on a Tree
Depth-first search explores each branch as far as possible before moving on.
Starting at the root, visit A, then A's first child B, then B's first child D.
D has no children, so backtrack to B and visit its second child, E.
After E, backtrack to A, visit its second child C, and finally visit C's child F.
The traversal order is: A → B → D → E → C → F
🛠️ Recursive DFS Implementation
Represent the tree with an object:
const tree = {
value: 'A',
children: [
{
value: 'B',
children: [
{ value: 'D', children: [] },
{ value: 'E', children: [] },
],
},
{
value: 'C',
children: [{ value: 'F', children: [] }],
},
],
};
The corresponding depth-first traversal code is:
function dfs(node) {
console.log(node.value);
node.children.forEach(child => {
dfs(child);
});
}
dfs(tree);
The output is:
A
B
D
E
C
F
🔍 Breadth-First Search (BFS) on a Tree
Breadth-first search visits nodes level by level.
Visit the root A first, then all of A's children, B and C, then their children, D, E, and F.
The traversal order is: A → B → C → D → E → F
🛠️ Queue-Based BFS Implementation
function bfs(root) {
const queue = [root];
while (queue.length > 0) {
const node = queue.shift();
console.log(node.value);
node.children.forEach(child => {
queue.push(child);
});
}
}
bfs(tree);
The output is:
A
B
C
D
E
F
🔄 Comparing DFS and BFS for Tree Traversal
- DFS follows one path to its end, then backtracks to visit other paths.
- BFS visits nodes level by level, finishing each level before moving to the next.
- DFS suits tasks that require exploring deeply and then backtracking, such as finding a particular node or evaluating an expression tree.
- BFS suits level-by-level processing, such as rendering hierarchical menus or finding a matching node closest to the root.
❓ Questions and Answers
Q1: How does DFS avoid visiting nodes more than once? A tree has no cycles, so repeated visits are not an issue when following child references. In a graph, use a set to track visited nodes and avoid infinite loops.
Q2: Why does BFS use a queue? A queue preserves first-in, first-out order, which matches level-by-level traversal.
Q3: Which is faster, DFS or BFS? It depends on the problem. BFS is generally preferable for finding a shortest path in an unweighted structure; both have the same time complexity when traversing an entire tree.
🎉 You have learned depth-first search (DFS), breadth-first search (BFS), and tree traversal! Keep building on these skills as you learn frontend development!
Algorithmic Techniques
Explore recursion, divide and conquer, greedy algorithms, dynamic programming, and two-pointer sliding windows through simple explanations and common frontend scenarios.
What Is Front-End Engineering?
An introduction to front-end engineering, its core practices, and its benefits for beginners
