zoukankan      html  css  js  c++  java
  • 6.二叉树的深度优先搜索和广度优先搜索代码实现(JavaScript版)

    二叉树的深度优先搜索代码实现(JavaScript版):

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    
    <body>
        <script>
            function Node(value) {
                this.value = value;
                this.left = null;
                this.right = null;
            }
    
            var nodeA = new Node("a");
            var nodeB = new Node("b");
            var nodeC = new Node("c");
            var nodeD = new Node("d");
            var nodeE = new Node("e");
            var nodeF = new Node("f");
            var nodeG = new Node("g");
    
            nodeA.left = nodeB;
            nodeA.right = nodeC;
            nodeB.left = nodeD;
            nodeB.right = nodeE;
            nodeC.left = nodeF;
            nodeC.right = nodeG;
    
            function deepSort(root, target) { //深度搜索二叉树
                if (root == null) return false;
                if (root.value == target) return true;
                var left = deepSort(root.left, target); //搜索左子树
                var right = deepSort(root.right, target); //搜索右子树
    
                return left || right; //target是否在左右子树里
            }
    
            console.log(deepSort(nodeA, "g"));
    
        </script>
    </body>
    
    </html>
    二叉树深度优先搜索

    二叉树的广度优先搜索代码实现(JavaScript版):

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    
    <body>
        <script>
            function Node(value) {
                this.value = value;
                this.left = null;
                this.right = null;
            }
    
            var nodeA = new Node("a");
            var nodeB = new Node("b");
            var nodeC = new Node("c");
            var nodeD = new Node("d");
            var nodeE = new Node("e");
            var nodeF = new Node("f");
            var nodeG = new Node("g");
    
            nodeA.left = nodeB;
            nodeA.right = nodeC;
            nodeB.left = nodeD;
            nodeB.right = nodeE;
            nodeC.left = nodeF;
            nodeC.right = nodeG;
    
            function breadthSort(rootList, target) {
                if (rootList == null || rootList.length == 0) return false;
                var childList = []; //存放下一层的所有子节点
                for (var i = 0; i < rootList.length; i++) {//依次遍历所有节点
                    if (rootList[i].value == target) {
                        return true;
                    } else {
                        childList.push(rootList[i].left);//把节点的左子节点放入
                        childList.push(rootList[i].right);//把节点的右子节点放入
                    }
                }
    
                return breadthSort(childList, target);//在下一层节点中继续搜索
            }
    
            console.log(breadthSort([nodeA], "g"));
        </script>
    </body>
    
    </html>
    二叉树的广度优先搜索
  • 相关阅读:
    性能测试监控指标-数据库
    cpu 故障定位
    ubuntu安装boost
    固定IP下虚拟机网卡配置及ssh
    零基础天池新闻推荐初学-04-排序模型+模型融合的简单学习(TODO 待进一步完善)
    零基础天池新闻推荐初学-04-特征工程(制作特征列和标签列,转为监督学习)
    零基础天池新闻推荐初学-03-多路召回
    零基础天池新闻推荐初学-02-数据分析
    零基础天池新闻推荐初学-01-赛题理解&Baseline
    初学推荐系统-06- GBDT+LR模型
  • 原文地址:https://www.cnblogs.com/lanshanxiao/p/13185959.html
Copyright © 2011-2022 走看看