Breadth-First Search (BFS) in Binary Tree

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:

  1. Start at the root of the tree.
  2. Enqueue the root node.
  3. 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).
  4. 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

快乐李同学(李俊德-大连理工大学)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值