Tree Traversal : Breadth First Search (BFS)
Tree Traversal : Breadth First Search (BFS)
Breadth-first search (BFS) is an algorithm for traversing or searching tree data structures. It starts at the tree root (or some arbitrary node of a graph, sometimes referred to as a search key and explores all of the neighbor nodes at the present depth prior to moving on to the nodes at the next depth level.
Steps for Breadth First Search Tree Treaversal
- 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 .
Algorithm
- If the root is NULL, return.
- Otherwise push the root in queue.
- Pop the node from the queue.
- Print the node’s data and add its left and right child.
- Repeat until the queue is empty.
Code for Tree Traversal : Breadth First Search
Code
Code
Run
import java.util.*; //Representing a Node of a Binary tree class Node{ int value; Node left,right; //constructor Node(int value) { this.value=value; left=right=null; } } class BreadthFirstSearch { Node root; //Root of the Binary tree BreadthFirstSearch() { root=null; } /*level order traversal of a binary tree */ public void levelOrder(Node ptr) { if(ptr==null) return ; //Creating a Queue Object Queue queue=new LinkedList(); queue.add(ptr); //adding an element to queue while(!queue.isEmpty()) { Node node=(Node)queue.poll(); //removing an element from queue System.out.print(node.value+" "); if(node.left!=null) queue.add(node.left); //adding an element to queue if(node.right!=null) queue.add(node.right); //adding an element to queue } } } public class Main{ public static void main(String[] args) { BreadthFirstSearch bst=new BreadthFirstSearch(); //creating the nodes of binarytree bst.root=new Node(50); bst.root.left=new Node(30); bst.root.right=new Node(70); bst.root.left.left=new Node(10); bst.root.left.right=new Node(40); bst.root.right.left=new Node(60); bst.root.right.right=new Node(90); bst.levelOrder(bst.root); } }
Output
50 30 70 10 40 60 90
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
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
Introduction to Trees
Binary Trees
- Binary Tree in Data Structures (Introduction)
- Tree Traversals: Inorder Postorder Preorder : C | C++ | Java
- Inorder Postorder PreOrder Traversals Examples
- Tree Traversal without Recursion
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
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- C| C++| Java
- Spiral Order traversal of Tree- C | C++| 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.- C| C++| 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- C | C++ | Java
- Lowest Common Ancestor in Binary Tree. C | C++ | Java
Introduction to Trees
Binary Trees
- Binary Tree in Data Structures (Introduction)
- Tree Traversals: Inorder Postorder Preorder : C | C++ | Java
- Inorder Postorder PreOrder Traversals Examples
- Tree Traversal without Recursion
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
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- C| C++| Java
- Spiral Order traversal of Tree- C | C++| 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.- C| C++| 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- C | C++ | Java
- Lowest Common Ancestor in Binary Tree- C | C++ | Java
Login/Signup to comment