• [algorithm] 二叉树的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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    代码模拟实现:

    import java.util.ArrayDeque;
    import java.util.Stack;
    
    public class BinaryTree {
        TreeNode root;
        public BinaryTree(int[] array){
            root = makeBinaryTree(array, 1);
        }
    
    
        /**
         * 递归创建二叉树
         * @param array 数组
         * @param index 索引
         * @return 二叉链表示(二叉树)
         */
        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<TreeNode> 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<TreeNode> 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();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95

    运行结果:

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

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

    深度优先搜索:
                     14
                  /       \
               11          13
              /   \       /    \
            23     53     1      8
              \    /  \   /
               9  45  78  99
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

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

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

  • 相关阅读:
    百战RHCE(第四十七战:运维工程师必会技-Ansible学习2-Ansible安装配置练习环境)
    砥砺的前行|基于labview的机器视觉图像处理|NI Vision Assisant(四)——Color(彩色图) 功能
    2205,2228,2230,2238,2292
    A+轮融资近2亿元,本土线控制动「TOP 1」按下“加速键”
    Java Class07
    联合体UNION的内存占用计算
    项目经理工具箱
    BLE学习(2):广播包报文格式详解
    Codejock Toolkit工具包专业版
    抖音获得抖音商品详情 API 返回值说明
  • 原文地址:https://blog.csdn.net/qq_48759664/article/details/125421439