Modules / Algorithms / Breadth-First Search

Breadth-First Search (BFS)

Explore hierarchical data structures by inserting, deleting, and traversing nodes in a Binary Search Tree.

CANVAS VIEW

Deconstructing Tree Traversals

How to visit every node in a tree, explained. Use this guide after interacting with the visualizer.

Quick Context: What is a Traversal?

A tree traversal is a process of visiting (checking or updating) each node in a tree data structure, exactly once. Since trees are not linear, there are multiple ways to explore them. The four main strategies—Inorder, Preorder, Postorder, and Level Order (BFS)—each provide a unique sequence and have specific use cases.

The Core Idea: Depth vs. Breadth

Traversals are split into two main categories:

  • Depth-First Search (DFS): This strategy goes as deep as possible down one branch before backtracking. Inorder, Preorder, and Postorder are all types of DFS. They use a stack (often the implicit function call stack in recursion) to keep track of nodes.
  • Breadth-First Search (BFS): This strategy explores the tree level by level, visiting all nodes at the current depth before moving to the next. Level Order Traversal is BFS. It uses a queue to manage which node to visit next.

Guided Experiments with the Visualizer

  1. Inorder (Left, Root, Right): Click the "Inorder" button. Notice the animation visits the leftmost node, then its parent, then the right child. For a Binary Search Tree (like this one), an inorder traversal visits the nodes in ascending sorted order. This is its most famous property.
  2. Preorder (Root, Left, Right): Click "Preorder". The traversal starts at the root, then goes down the left subtree, then the right subtree. This is useful for creating a copy of a tree or for expressions where the operator comes before the operands (e.g., Polish notation).
  3. Postorder (Left, Right, Root): Click "Postorder". The traversal visits the children before the parent. This is useful for deleting nodes in a tree, as you can safely delete a node after you have processed its children.
  4. Level Order (BFS): Click "Level (BFS)". Watch how the animation highlights nodes row by row, from top to bottom and left to right. This is Breadth-First Search. It's excellent for finding the shortest path between two nodes in a tree.

Pseudocode & Data Structures

// DFS (Recursive - Inorder) function inorder(node) {
  if (node === null) return;
  inorder(node.left);
  print(node.value);
  inorder(node.right);
}
// BFS (Iterative - Level Order) function levelOrder(root) {
  let queue = [root];
  while (queue.length > 0) {
    let node = queue.shift();
    print(node.value);
    if (node.left) queue.push(node.left);
    if (node.right) queue.push(node.right);
  }
}

Notice how DFS naturally lends itself to recursion (using the call stack), while BFS uses an explicit queue data structure.

Key Takeaways

  • Inorder: For a BST, gives nodes in sorted order.
  • Preorder: Useful for copying trees. The root is always the first element.
  • Postorder: Useful for deleting trees. The root is always the last element.
  • Level Order (BFS): Explores level by level. Finds the shortest path from the root to any other node.
Cheat sheet

Breadth First Search

A tree traversal is a process of visiting (checking or updating) each node in a tree data structure, exactly once. Since trees are not linear, there are multiple ways to explore them. The four main strategies—Inorder, Preorder, Postorder, and Level Order (BFS)—each provide a unique sequence and have specific use cases.

ALGORITHMS · vizlearn.in/dsa/breadth_first_search.html