Breadth-First Search (BFS) in a binary tree has a single standard approach for traversing the tree: level-order traversal. This means BFS visits each node level by level, starting from the root and moving down to the leaves. Unlike DFS (which includes Preorder, Inorder, and Postorder traversals), BFS does not have different variations in terms of traversal order for binary trees.
BFS Algorithm Code for Binary Tree (Level-Order Traversal)
The most common implementation of BFS in a binary tree uses a queue to explore each level one at a time. Here’s how the algorithm works:
- Start at the root of the tree.
- Enqueue the root node.
- While the queue is not empty:
- Dequeue a node.
- Process the node (e.g., print or collect its value).
- Enqueue its left and right children (if they exist).
- Repeat until all nodes are processed.
BFS (Level-Order Traversal) Java Implementation:
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
// Definition for a binary tree node. Defines the structure of a node in the binary tree, with integer value `val`, and references to left and right child nodes.
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x;}
}
public class BinaryTreeTraversals {
public static void breadthFirstSearch(TreeNode root) {
if (root == null) return;
// A queue is used to keep track of nodes at each level. We enqueue the root node first and then, for every node we dequeue, we enqueue its left and right children (if they exist). The queue ensures that nodes are processed level by level.
Queue<TreeNode> queue = new LinkedList<>();
// Start at the root of the tree, and enqueue the root node
queue.add(root);
while (!queue.isEmpty()) {
TreeNode currentNode = queue.poll(); // Dequeue the front node
System.out.print(currentNode.val + " "); // Process the current node
// Enqueue left child of the current node if it exists
if (currentNode.left != null) {
queue.add(currentNode.left);
}
// Enqueue right child of the current node if it exists
if (currentNode.right != null) {
queue.add(currentNode.right);
}
}
}
public static void main(String[] args) {
// Constructing the example binary tree
// 1
// / \
// 2 3
// / \ \
// 4 5 6
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(5);
root.right.right = new TreeNode(6);
// Breadth-First Search(BFS, Level-Order Traversal): 1 2 3 4 5 6
System.out.print("Breadth-First Search(BFS, Level-Order Traversal): ");
BinaryTreeTraversals.breadthFirstSearch(root);
}
}
Explanation of BFS Code:
- Queue: A queue is used to keep track of nodes at each level. We enqueue the root node first and then, for every node we dequeue, we enqueue its left and right children (if they exist). The queue ensures that nodes are processed level by level.
- Time Complexity: The time complexity of BFS is O(n), where n is the number of nodes in the tree. This is because every node is processed once.
- Space Complexity: The space complexity is O(w), where w is the maximum width of the tree (the maximum number of nodes at any level). In the worst case (for example, in a full binary tree), the queue might need to store half of the nodes at the last level, so the space complexity can be O(n/2) = O(n).
Does BFS Have Variations Like Preorder, Inorder, and Postorder?
No, BFS (in the context of a binary tree) does not have different variations like DFS does with Preorder, Inorder, and Postorder. BFS is always level-order traversal, where nodes are visited level by level, starting from the root. The traversal order is determined by the structure of the tree, and there is no recursive variation like in DFS.