• [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, 一行一行看!!! 不要不懂装懂!!!

  • 相关阅读:
    Verilog开源项目——百兆以太网交换机(三)Hash模块设计
    【python】with...as语句——基于上下文管理器的操作
    企业数据现状分析:为什么需要实时数据?如何高效挖掘实时数据价值?
    Deep Learning-深度学习(二)
    Oracle IO是否为Direct IO
    机器学习笔记自最优化理论与方法(十一)无约束优化问题——关于共轭方向法重要特征的相关证明
    ADG级联备库环境PSU应用验证
    Flink sql 实现 -connection-clickhouse的 source和 sink
    Linux安装Jenkins
    手机数据恢复应用程序有哪些?手机数据恢复免费软件排名TOP 9
  • 原文地址:https://blog.csdn.net/qq_48759664/article/details/125421439