Tree traversal
Breadth-first search
Graph algorithms
Computer science
Data structures

Breadth-first traversal

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Breadth-first traversal (BFT), or breadth-first search (BFS), is a fundamental algorithm used to explore or traverse data structures such as graphs and trees. BFS examines nodes layer by layer, moving horizontally, which contrasts with depth-first search (DFS) that goes as deep down one path as possible before backtracking. BFS is crucial in many applications, including finding the shortest path in unweighted graphs, serialization and deserialization of data structures, and network broadcasting.

Understanding Breadth-first Traversal

Data Structures

The BFS algorithm employs a queue to track nodes yet to be explored. A queue is a first-in, first-out (FIFO) data structure which is ideal for BFS due to its iterative nature compared to the recursion-based DFS.

Algorithm Steps

  1. Initialization: Start from the root node (in a tree) or any arbitrary node (in a graph). Enqueue this node and mark it as visited.
  2. Traversal:
    • While the queue is not empty:
      • Dequeue a node from the front of the queue.
      • Visit and process the node (e.g., print its value).
      • Enqueue all unvisited adjacent nodes, mark them as visited.
  3. Termination: The algorithm terminates when there are no more nodes to process.

Example

Consider a graph represented by the following adjacency list:

  • Shortest Path in Unweighted Graphs: BFS finds the shortest path without weights by expanding nodes level by level until the target node is reached.
  • Network Broadcasting: BFS ensures messages are sent to all nodes, covering each once and preventing redundant messaging.
  • Tree Traversal: Useful for level-order traversal, BFS can explore trees from the root and process nodes layer by layer.
  • Serialization/Deserialization: BFS helps serialize trees into strings for storage, often used to reconstruct the original tree structure.
    • Traversal Order: `A, B, C, D, E, F, G, H`
    • Shortest Path: `A -> B -> E -> H`
  • Enqueue A Queue: [A]
  • Dequeue A, Enqueue B, C Queue: [B, C]
  • Dequeue B, Enqueue D, E Queue: [C, D, E]
  • Dequeue C, Enqueue F, G Queue: [D, E, F, G]
  • Dequeue D Queue: [E, F, G]
  • Dequeue E, Enqueue H Queue: [F, G, H]
  • Dequeue F Queue: [G, H]
  • Dequeue G Queue: [H]
  • Dequeue H Queue: []

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.