栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

[算法] 二叉树的DFS与BFS算法 (Java) -- 痛定思痛 彻底搞懂

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

[算法] 二叉树的DFS与BFS算法 (Java) -- 痛定思痛 彻底搞懂

二叉树的DFS与BFS算法 (Java) 1.概念 ①DFS (深度优先搜索)

维基百科读一遍
定义看完, 看一遍gif

②BFS (广度优先搜索)

维基百科读一遍
gif看一遍

2. 算法实现

二叉树节点结构:

public class TreeNode {
    int value;
    TreeNode left;
    TreeNode right;
    public TreeNode(int value){
        this.value = value;
    }
}

代码模拟实现:

import java.util.ArrayDeque;
import java.util.Stack;

public class BinaryTree {
    TreeNode root;
    public BinaryTree(int[] array){
        root = makeBinaryTree(array, 1);
    }


    
    public static TreeNode makeBinaryTree(int[] array, int index){
        if (index < array.length){
            // index为根节点索引
            int value = array[index];
            // 排除为0的节点
            if (value != 0){
                TreeNode treeNode = new TreeNode(value);
                array[index] = 0;
                // 递归
                treeNode.left = makeBinaryTree(array, index * 2);
                treeNode.right = makeBinaryTree(array, index * 2 + 1);
                return treeNode;
            }
        }
        return null;
    }

    
    public void DFS(){
        if (root == null){
            System.out.println("empty tree");
            return;
        }

        // 栈, 先进后出
        Stack stack = new Stack<>();
        stack.push(root);
        while(stack.isEmpty() == false){
            // 移除堆栈顶部对象,并作为次函数的值返回该对象
            TreeNode node = stack.pop();
            System.out.print(node.value + "  ");
            if (node.right != null){
                stack.push(node.right);
            }
            if (node.left != null){
                stack.push(node.left);
            }
        }
        System.out.println();
    }


    
    public void BFS(){
        if (root == null){
            System.out.println("empty tree");
            return;
        }
        // 队列, 先进先出
        ArrayDeque treeNodes = new ArrayDeque<>();
        treeNodes.add(root);
        while (treeNodes.isEmpty() == false){
            // 删除队列中的第一个元素,并返回该元素的值
            TreeNode node = treeNodes.remove();
            System.out.print(node.value + "  ");
            if (node.left != null){
                treeNodes.add(node.left);
            }
            if (node.right != null){
                treeNodes.add(node.right);
            }
        }
        System.out.println();
    }


    public static void main(String[] args) {
        int[] arr = {0,14,11,13,23,53,1,8,0,9,45,78,99,0,0};
        BinaryTree tree = new BinaryTree(arr);
        tree.DFS();
        tree.BFS();
    }
}

运行结果:

深度优先搜索: 
14  11  23  9  53  45  78  13  1  99  8  
广度优先搜索: 
14  11  13  23  53  1  8  9  45  78  99    

其实二叉树可以看成这样:

深度优先搜索:
                 14
              /       
           11          13
          /          /    
        23     53     1      8
              /     /
           9  45  78  99

痛定思痛, 不搞懂你永远都不懂!!!

看不懂的去debug, 一行一行看!!! 不要不懂装懂!!!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/988331.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号