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>
    二叉树的广度优先搜索
  • 相关阅读:
    时间控件My97DatePicker,实现时间的选择,具体运用
    OnClientClick事件和验证控件同时用的时候,会有问题
    根据Eval()函数绑定的值,来显示GridView中的控件的方法
    打印部分页面内容(Javascript)
    U盘装系统中bios怎么设置USB启动(图文教程)
    TextBox控件只允许输入数字(转)
    JS模态窗体的运用,以及相关注意事项(有用到window.returnValue)
    控件TextBox与验证控件相结合产生的控件(运用)
    linq 实现动态 orderby(根据参数名排序)
    yii 多个数据库链接
  • 原文地址:https://www.cnblogs.com/lanshanxiao/p/13185959.html
Copyright © 2011-2022 走看看