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>
    非严格比较二叉树
  • 相关阅读:
    Android.mk添加第三方jar包
    关于回调函数
    Ubuntu下GIT服务器的安装与配置
    三星I9100在Ubuntu下用Adb调试
    Android检测网络是否可用和主机是否可达
    keepalived配置文件解析系列之(一)keepalived简介及配置文件介绍
    keepalived配置文件解析系列之(三)配置文件解析过程
    C语言中的位域(bitfield)概念
    popen和变长参数库函数(va_xxx)用法举例及命令执行失败情况探讨
    《深入理解Linux内核》条目式笔记 _3
  • 原文地址:https://www.cnblogs.com/lanshanxiao/p/13188832.html
Copyright © 2011-2022 走看看