Tree Traversal : Breadth First Search (BFS)

Tree Traversal : Breadth First Search in Java

Here we will discuss Breadth First Search in Java, also known as Level Order Traversal, is one of the most important ways to traverse a tree. It visits nodes level by level, starting from the root.

This article explains BFS in a simple and beginner-friendly way, including its approach, algorithm, Java implementation, examples, and practical insights

Breadth First Search in Java

Tree Traversal: Breadth First Search

BFS is a tree traversal technique where:

  • Nodes are visited level by level
  • All nodes at the same depth are processed before moving deeper

Example Tree:

        1
       / \
      2   3
     / \   \
    4   5   6

BFS Traversal:

1 → 2 → 3 → 4 → 5 → 6

Steps for Breadth First Search Tree Treaversal

Before practicing Breadth First Search in Java we will go through Step by step process for performing BFS alongwith comprehensive Algorithm explanation:

  • Step 1 : Push the root i.e. 50 to the queue.
  • Step 2 : Pop the element 50 from the queue and print it.
  • Step 3 : Now, Add it’s left and right child i.e. add 30 and 70 to queue.
  • Step 4 : Again pop the front element i.e. 30 from queue and print it .
  • Step 5 :  Add it’s left and right child i.e. 10 and 40 in the queue.
  • Step 6 : Pop the element 70 from the queue and  print it.
  • Step 7 : add it’s left and right child i.e. 60 and 90.
  • Step 8 : Now pop all the elements from the queue and print them as there is no child of these elements.

      Therefore the printing sequence will be 50 30 70 10 40 60 90 .
tree traversal breadth first search

Algorithm for Breadth First Search in Java

Algorithm for performing Breadth First Search in Java programming:

  1. If root is null → return
  2. Create a queue
  3. Add root to queue
  4. While queue is not empty:
    • Remove node from queue

    • Print node value

    • If left child exists → add to queue

    • If right child exists → add to queue

Learn DSA

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Java Code for Breadth First Search (BFS)

Common Insights on Breadth First Search in Java

  1. BFS is also called Level Order Traversal
  2. Uses queue instead of recursion
  3. Works for both trees and graphs
  4. Guarantees shortest path in unweighted graphs
  5. Easy to modify for level based problems

Best Practices:

  • Always check for null root
  • Use Queue interface instead of concrete class
  • Prefer LinkedList or ArrayDeque for queue
  • Avoid unnecessary object creation
  • Use BFS when level information is important

Frequently Asked Questions

Answer:

It is a traversal technique where nodes are visited level by level using a queue.

Answer:

Because it processes all nodes at one level before moving to the next.

Answer:

A queue (FIFO) is used to maintain traversal order.

Answer:

It is O(n) since every node is visited once.

Answer:

Use BFS when shortest path or level wise traversal is required.

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion :  C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal LIne by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric  C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree. CC++ | Java

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion : C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal Line by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric – C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree- CC++ | Java