【问题标题】:Checking to see if a Binary Tree has the same structures检查二叉树是否具有相同的结构
【发布时间】:2014-05-09 10:52:47
【问题描述】:

我应该使用递归检查二叉树是否与另一棵树具有相同的结构。 我遇到麻烦的部分是弄清楚如何在每个级别检查是否有左侧或右侧并将其与另一棵树进行比较。任何形式的帮助将不胜感激。

public  boolean hasSameStructureAs(BinaryTree tree){
     //If left and right are null they are still similar
    if(leftChild == null && rightChild == null);
        return true;
    if (leftChild == null && rightChild != null)
        return false;
    if (leftChild != null && rightChild == null){
        return false;

//我在下面代码的递归部分遇到问题,我认为我部分正确

    if (!tree.equals(rightChild.left,leftChild.left )) return false;
    if (!tree.equals(rightChild.right, leftChild.right)) return false;

    return true;

这里是一些构造函数和get/set方法。

 public class BinaryTree {
private String data;
private BinaryTree leftChild;
private BinaryTree rightChild;

public BinaryTree(String d) {
    data = d;
    leftChild = null;
    rightChild = null;
}

public BinaryTree(String d, BinaryTree left, BinaryTree right) {
    data = d;
    leftChild = left;
    rightChild = right;
}

public String getData() {
    return data;
}

public BinaryTree getLeftChild() {
    return leftChild;
}

public BinaryTree getRightChild() {
    return rightChild;}

【问题讨论】:

    标签: java recursion data-structures


    【解决方案1】:

    我需要绝对确定您的 .equals 方法,但您似乎没有递归地执行此操作。如果您希望它是递归的,如果两棵树都有左孩子或都有右孩子(或两者都有)递归返回该方法,将这些孩子作为新的“根”传递给检查他们的孩子。

    如果有任何不匹配的点,则返回 false。如果您已经达到所有孩子都是叶子(null 孩子)的地步,则返回 true。

    除此之外,它就像使方法预订递归一样简单。 (访问父母 -> 访问左孩子,如果有的话 -> 访问右孩子,如果有的话,每次访问孩子时都重复这个过程。

    【讨论】:

      【解决方案2】:

      您的BinaryTree 是一个递归数据结构,因此您可以在编写递归函数时按照常用方法递归地进行检查 - 假设它已经编写好了。

      这就是我的意思:假设您的 BinaryTree 类有一个有效的 hasSameStructureAs 方法,您只能在子节点上使用它。您将如何使用它检查当前节点?

      • 检查tree.getLeftChild()this.leftChild 的左子树;如果hasSameStructureAs 返回false,则也返回false。如果两个左子树都是null,继续检查
      • 检查tree.getRightChild()this.rightChild 的右子树;如果hasSameStructureAs 返回false,则也返回false。如果两个右子树都是null,或者hasSameStructureAs返回true,也返回true

      就是这样 - 你完成了!你的方法需要做的就是null检查它的左右子树,并在它们上调用hasSameStructureAs

      【讨论】:

        【解决方案3】:

        假设你没有重写 equals,这个:

        if (!tree.equals(rightChild.left,leftChild.left )) return false;
        

        不会做你想做的事。 equals() 的默认实现只返回 (this == other),并且只有当它们是完全相同的对象(即在内存中的相同位置)时才会如此。

        您实际上还没有任何递归。递归意味着函数调用自身。考虑左右孩子也是树。你已经有一个方法hasSameStructureAs,当完成时,对于结构相同的树将返回true。因此,您的解决方案应该在两棵树的左右子节点上调用 hasSameStructureAs(),并组合从每个“边”获得的值。

        一般来说,递归问题的策略是将其分解为递归案例和一个或多个基本案例。使用树结构,您的基本案例将涉及叶子。树中的叶子是子链接为空的节点。

        假设您有一个布尔值用于临时存储左侧是否相等。

        • 如果 this.leftChild 和 other.leftChild 均为 null,则 leftEq 为 true
        • 如果其中只有一个为空,leftEq 为假
        • 否则leftEq就是this.leftChild.hasSameStructure(other.leftChild)

        您可以对 rightEq 执行相同的操作,然后返回 leftEq AND rightEq。

        我个人认为,让这个问题难以思考的原因之一是您的 hasSameStructure 是从其中一棵树的角度编写的。也许考虑一下如何使用外部方法 hasSameStructure(treeA, treeB) 解决它,然后将其转化为实际要求的内容。

        【讨论】:

          【解决方案4】:

          线索 #1:您的方法称为“hasSameStructureAs”。要使其递归,它需要调用“hasSameStructureAs”。

          线索 #2:您的代码传递了一个名为“tree”的参数,但您甚至没有查看它。

          建议:将 BinaryTree 重命名为 Node(因为它是有子节点的 Node,而不是树),并将“tree”参数重命名为“that”,然后显式使用“this”访问“this”上的字段对象,这样您就可以看到属于“this”与“that”的内容:

          public boolean hasSameStructureAs(Node that)
              {
              // first, only bother to check if the children are different at all
              // (if they are both null, for example, then they will be equal)
              if (this.leftChild != that.leftChild)
                  {
                  // second, since we already know that they are unequal, if either
                  // one of them is null, then the data structure is unequal
                  if (this.leftChild == null || that.leftChild == null)
                      {
                      return false;
                      }
          
                  // third, since neither one is null, let's recursively ask them if
                  // they are the same
                  if (!this.leftChild.hasSameStructureAs(that.leftChild))
                      {
                      return false;
                      }
                  }
              // and so on ..
          

          另外,由于这可能是一个家庭作业,你真的需要学会自己解决一些问题。

          【讨论】:

            【解决方案5】:

            对作业有同样的问题,我就是这样做的,可能不是最有效的,但结果是准确的

            public boolean hasSameStructureAs(BinaryTree tree){
            
            
                if(tree.getLeftChild() == null && this.getLeftChild() == null) {
                    if(tree.getRightChild() == null && this.getRightChild() == null) {
                        return true;}}
            
                if(tree.getLeftChild() == null && this.getLeftChild() == null) {
                    if(tree.getRightChild() != null && this.getRightChild() != null) {
                        return this.getRightChild().hasSameStructureAs(tree.getRightChild());}}
            
                if(tree.getLeftChild() != null && this.getLeftChild() != null) {
                    if(tree.getRightChild() != null && this.getRightChild() != null) {
                        return this.getRightChild().hasSameStructureAs(tree.getRightChild()) &&   this.getLeftChild().hasSameStructureAs(tree.getLeftChild());}}
            
                if(tree.getLeftChild() != null && this.getLeftChild() != null) {
                    if(tree.getRightChild() == null && this.getRightChild() == null) {
                        return this.getLeftChild().hasSameStructureAs(tree.getLeftChild());}}
            
            
                return false;
            
            
            
             }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-04-23
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-05-16
              • 1970-01-01
              相关资源
              最近更新 更多