zoukankan      html  css  js  c++  java
  • 7.二叉树的比较代码实现(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;
    
            var a = new Node("a");
            var b = new Node("b");
            var c = new Node("c");
            var d = new Node("d");
            var e = new Node("e");
            var f = new Node("f");
            var g = new Node("g");
    
            a.left = b;
            a.right = c;
            b.left = d;
            b.right = e;
            c.left = f;
            c.right = g; 
    
            //严格比较二叉树,左子树和右子树必须完全一样,不可互换
            function compareTree(root1, root2){
                if(root1 == root2) return true;//两棵树是同一棵树
                if((root1 == null && root2 != null) || (root1 != null && root2 == null)) return false;//两棵树有一棵为null,另一棵不是null
                if(root1.value != root2.value) return false;//节点的值不同
                var leftBoolean = compareTree(root1.left, root2.left);//比较左子树
                var rightBoolean = compareTree(root1.right, root2.right);//比较右子树
    
                return leftBoolean && rightBoolean;//左子树和右子树必须都一样
            }
    
            console.log(compareTree(nodeA, a));
    
        </script>
    </body>
    </html>
    严格比较二叉树

    非严格比较二叉树,左子树和右子树可以互换:

    <!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;
    
            var a = new Node("a");
            var b = new Node("b");
            var c = new Node("c");
            var d = new Node("d");
            var e = new Node("e");
            var f = new Node("f");
            var g = new Node("g");
    
            a.right = b;
            a.left = c;
            b.left = d;
            b.right = e;
            c.left = f;
            c.right = g; 
    
            //非严格比较二叉树,左子树和右子树可互换
            function compareTree(root1, root2){
                if(root1 == root2) return true;//两棵树是同一棵树
                if((root1 == null && root2 != null) || (root1 != null && root2 == null)) return false;//两棵树有一棵为null,另一棵不是null
                if(root1.value != root2.value) return false;//节点的值不同
    
                return (compareTree(root1.left, root2.left) && compareTree(root1.right, root2.right) || compareTree(root1.right, root2.left) && compareTree(root1.left, root2.right));//左子树和右子树可以互换
            }
    
            console.log(compareTree(nodeA, a));
    
        </script>
    </body>
    </html>
    非严格比较二叉树
  • 相关阅读:
    Mybatis应用
    MyBatis
    jQuery中.bind() .live() .delegate() .on()的区别
    SpringMVC (八)SpringMVC返回值类型
    SpringMVC(七)参数的自动装配和路径变量
    SpringMVC (六)注解式开发
    SpringMVC (五)视图解析器
    SpringMVC (四)MultiActionController
    SourceTree的使用
    Idea中使用git
  • 原文地址:https://www.cnblogs.com/lanshanxiao/p/13188832.html
Copyright © 2011-2022 走看看