One article to understand DFS, BFS

1. Depth First Search

The method of depth-first traversal of the graph is to start from a vertex v in the graph:
(1) Visit the vertex v;
(2) Start from the unvisited neighboring points of v in turn, and perform depth-first traversal on the graph; until the graph and v The vertices with the same path are visited;
(3) If there are still vertices in the graph that have not been visited at this time, start from an unvisited vertex, and re-execute the depth-first traversal until all vertices in the graph have been visited. .

First look at a binary tree:
Insert picture description here

For this binary tree, a depth-first search for it is a pre-order traversal: the result of the traversal is: 1 2 4 8 5 3 6 7

When performing a DFS search on this binary tree, let's first create such a binary tree

package test;

public class DFSBinaryTree {
    
    
    public static void main(String[] args) {
    
    
        //创建节点
        Node root=new Node(1);
        Node node2=new Node(2);
        Node node3=new Node(3);
        Node node4=new Node(4);
        Node node5=new Node(5);
        Node node6=new Node(6);
        Node node7=new Node(7);
        Node node8=new Node(8);
        //连接节点
        root.left=node2;
        root.right=node3;
        node2.left=node4;
        node2.right=node5;
        node3.left=node6;
        node3.right=node7;
        node4.left=node8;
        
    }
    
}

class Node{
    
    
    Node left;
    Node right;
    int val;
    public Node(int val)
    {
    
    
        this.val=val;
    }
} 
  • Recursive form of DFS
 void DFS(Node root)
    {
    
         
        if(root==null)
          return;
         System.out.print(root.val+"  ");
         DFS(root.left);
         DFS(root.right);
    }

Called in the main function:

    DFSBinaryTree t=new DFSBinaryTree();
    t.DFS(root);

Complete code:

package test;

public class DFSBinaryTree {
    
    
    public static void main(String[] args) {
    
    
        //创建节点
        Node root=new Node(1);
        Node node2=new Node(2);
        Node node3=new Node(3);
        Node node4=new Node(4);
        Node node5=new Node(5);
        Node node6=new Node(6);
        Node node7=new Node(7);
        Node node8=new Node(8);
        //连接节点
        root.left=node2;
        root.right=node3;
        node2.left=node4;
        node2.right=node5;
        node3.left=node6;
        node3.right=node7;
        node4.left=node8;

        DFSBinaryTree t=new DFSBinaryTree();
        t.DFS(root);
        
    }
     void DFS(Node root)
    {
    
         
        if(root==null)
          return;
         System.out.print(root.val+"  ");
         DFS(root.left);
         DFS(root.right);
    }
    
}

class Node{
    
    
    Node left;
    Node right;
    int val;
    public Node(int val)
    {
    
    
        this.val=val;
    }
}

Insert picture description hereYou can see that the output result is the same as the pre-order traversal result we wrote before

  • Non-recursive form of DFS

The non-recursive form of DFS uses a stack. For the first time, the root node is pushed into the stack, and then the right child node is pressed, which is the right child node, and then the left child node is pressed, and then the stack is continuously pushed, and the stack is accessed.

 void DFSNotRecursive(Node root)
    {
    
    
        Stack<Node> st=new Stack<>();
        st.push(root);
        while(!st.isEmpty())
        {
    
    
            Node top=st.pop();//获取栈顶节点
            System.out.print(top.val+"  ");

            if(top.right!=null)//先压右子节点
             st.push(top.right);


            if(top.left!=null)
               st.push(top.left);

        }
    }
    

Insert picture description hereThe result is consistent with the recursive form of DFS

2. Breadth first search

Breadth-first search generally adopts a non-recursive form of queue. For a binary tree, breadth-first search can achieve layer sequence traversal. Taking the binary tree in this article as an example, the result of layer sequence traversal should be: 1 2 3 4 5 6 7 8

void BFS(Node root)
    {
    
    
        Queue<Node> q=new LinkedList<>();
        q.offer(root);//将节点添加到队尾
        while(!q.isEmpty())
        {
    
    
            Node cur=q.poll();//获取队首节点并将其移除队列
            System.out.print(cur.val+"  ");
            if(cur.left!=null)
               q.offer(cur.left);

            if(cur.right!=null)
               q.offer(cur.right);
            

        }
    }

Insert picture description here
Full text code:

package test;

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

public class DFSBinaryTree {
    
    
    public static void main(String[] args) {
    
    
        //创建节点
        Node root=new Node(1);
        Node node2=new Node(2);
        Node node3=new Node(3);
        Node node4=new Node(4);
        Node node5=new Node(5);
        Node node6=new Node(6);
        Node node7=new Node(7);
        Node node8=new Node(8);
        //连接节点
        root.left=node2;
        root.right=node3;
        node2.left=node4;
        node2.right=node5;
        node3.left=node6;
        node3.right=node7;
        node4.left=node8;

        DFSBinaryTree t=new DFSBinaryTree();
        t.DFS(root);
        System.out.println();
        t.DFSNotRecursive(root);

        System.out.println();
        t.BFS(root);
        
    }
     void DFS(Node root)
    {
    
         
        if(root==null)
          return;
         System.out.print(root.val+"  ");
         DFS(root.left);
         DFS(root.right);
    }

    void DFSNotRecursive(Node root)
    {
    
    
        Stack<Node> st=new Stack<>();
        st.push(root);
        while(!st.isEmpty())
        {
    
    
            Node top=st.pop();//获取栈顶节点
            System.out.print(top.val+"  ");

            if(top.right!=null)//先压右子节点
             st.push(top.right);


            if(top.left!=null)
               st.push(top.left);



        }
       
    }
    void BFS(Node root)
    {
    
    
        Queue<Node> q=new LinkedList<>();
        q.offer(root);//将节点添加到队尾
        while(!q.isEmpty())
        {
    
    
            Node cur=q.poll();//获取队首节点并将其移除队列
            System.out.print(cur.val+"  ");
            if(cur.left!=null)
               q.offer(cur.left);

            if(cur.right!=null)
               q.offer(cur.right);
            

        }
    }
    
}

class Node{
    
    
    Node left;
    Node right;
    int val;
    public Node(int val)
    {
    
    
        this.val=val;
    }
}

Guess you like

Origin blog.csdn.net/qq_43478694/article/details/114978649