How do implement a breadth first traversal?

前端 未结 9 1333
萌比男神i
萌比男神i 2020-11-28 21:34

This is what I have. I thought pre-order was the same and mixed it up with depth first!

import java.util.LinkedList;
import java.util.Queue;

public class Ex         


        
相关标签:
9条回答
  • 2020-11-28 22:02

    It doesn't seem like you're asking for an implementation, so I'll try to explain the process.

    Use a Queue. Add the root node to the Queue. Have a loop run until the queue is empty. Inside the loop dequeue the first element and print it out. Then add all its children to the back of the queue (usually going from left to right).

    When the queue is empty every element should have been printed out.

    Also, there is a good explanation of breadth first search on wikipedia: http://en.wikipedia.org/wiki/Breadth-first_search

    0 讨论(0)
  • 2020-11-28 22:07
    //traverse
    public void traverse()
    {
        if(node == null)
            System.out.println("Empty tree");
        else
        {
            Queue<Node> q= new LinkedList<Node>();
            q.add(node);
            while(q.peek() != null)
            {
                Node temp = q.remove();
                System.out.println(temp.getData());
                if(temp.left != null)
                    q.add(temp.left);
                if(temp.right != null)
                    q.add(temp.right);
            }
        }
    }
    

    }

    0 讨论(0)
  • 2020-11-28 22:10

    Breadth first is a queue, depth first is a stack.

    For breadth first, add all children to the queue, then pull the head and do a breadth first search on it, using the same queue.

    For depth first, add all children to the stack, then pop and do a depth first on that node, using the same stack.

    0 讨论(0)
  • 2020-11-28 22:12
    public static boolean BFS(ListNode n, int x){
            if(n==null){
               return false;
           }
    Queue<ListNode<Integer>> q = new Queue<ListNode<Integer>>();
    ListNode<Integer> tmp = new ListNode<Integer>(); 
    q.enqueue(n);
    tmp = q.dequeue();
    if(tmp.val == x){
        return true;
    }
    while(tmp != null){
        for(ListNode<Integer> child: n.getChildren()){
            if(child.val == x){
                return true;
            }
            q.enqueue(child);
        }
    
        tmp = q.dequeue();
    }
    return false;
    }
    
    0 讨论(0)
  • 2020-11-28 22:17

    This code which you have written, is not producing correct BFS traversal: (This is the code you claimed is BFS, but in fact this is DFS!)

    //  search traversal
      public void breadth(TreeNode root){
          if (root == null)
              return;
    
          System.out.print(root.element + " ");
          breadth(root.left);
          breadth(root.right);
     }
    
    0 讨论(0)
  • 2020-11-28 22:17

    For implementing the breadth first search, you should use a queue. You should push the children of a node to the queue (left then right) and then visit the node (print data). Then, yo should remove the node from the queue. You should continue this process till the queue becomes empty. You can see my implementation of the BFS here: https://github.com/m-vahidalizadeh/foundations/blob/master/src/algorithms/TreeTraverse.java

    0 讨论(0)
提交回复
热议问题