StackOverflow错误,有人可以告诉我为什么我的代码会生成它吗?

时间:2016-06-14 16:20:59

标签: java

我正在尝试编写一个递归函数,告诉我树是否是另一棵树的子树,但它给了我一个stackoverflow错误。我目测检查了代码,但我没有看到任何错误!因此,我在这里屈服于我的命运。谢谢你的帮助。

PS:BinaryTreeNode只是一个普通的TreeNode     BinaryTwinNode只是一个包含两个BinaryTreeNodes的类。     我正在使用Eclipse Java

以下是我用来对这些方法运行的代码:

    BinarySearchTree healthyTree = new BinarySearchTree();
    BinaryTreeNode no1 = new BinaryTreeNode(1);
    BinaryTreeNode no2 = new BinaryTreeNode(2);
    BinaryTreeNode no3 = new BinaryTreeNode(3);
    BinaryTreeNode no4 = new BinaryTreeNode(4);
    BinaryTreeNode no5 = new BinaryTreeNode(5);

    healthyTree.insert(no1);
    healthyTree.insert(no2);
    healthyTree.insert(no3);
    healthyTree.insert(no4);
    healthyTree.insert(no5);

    BinarySearchTree betterTree = new BinarySearchTree();
    betterTree.insert(no3);
    betterTree.insert(no1);
    betterTree.insert(no2);
    betterTree.insert(no4);
    betterTree.insert(no5);

   boolean tru = betterTree.isSubtree(healthyTree.root, (new BinarySearchTree(no5)).root);

以下是错误消息:

 java.lang.StackOverflowError
at java.util.LinkedList.addFirst(LinkedList.java:293)
at algorithim.BinarySearchTree.matchingTrees(BinarySearchTree.java:811)
at algorithim.BinarySearchTree.isSubtree(BinarySearchTree.java:784)
at algorithim.BinarySearchTree.isSubtree(BinarySearchTree.java:789)
at algorithim.BinarySearchTree.isSubtree(BinarySearchTree.java:788)
at algorithim.BinarySearchTree.isSubtree(BinarySearchTree.java:789)
at algorithim.BinarySearchTree.isSubtree(BinarySearchTree.java:789)
at algorithim.BinarySearchTree.isSubtree(BinarySearchTree.java:788)
at algorithim.BinarySearchTree.isSubtree(BinarySearchTree.java:789)
at algorithim.BinarySearchTree.isSubtree(BinarySearchTree.java:789)
at algorithim.BinarySearchTree.isSubtree(BinarySearchTree.java:788)
......

isSubtree,matchingTrees方法详细信息:

public boolean isSubtree(BinaryTreeNode originNode, BinaryTreeNode opposeNode){
    if(opposeNode == null)
        return true;

    if(originNode == null)
        return false;

    //System.out.println("its a match? "+matchingTrees(originNode, opposeNode));
    //perform work


    if(matchingTrees(originNode, opposeNode)){
        return true;
    }else{

        return isSubtree(originNode.left, opposeNode)||
        isSubtree(originNode.right, opposeNode);
    }


 }

 /**
  * return true if origin matches the differ
  * @param origin
  * @param differ
  * @return
  */
 public boolean matchingTrees(BinaryTreeNode origin, BinaryTreeNode differ){

     if(origin == null)
         return false;
     if(differ == null)
         return true;


     LinkedList<BinaryTwinNode> queue = new LinkedList<BinaryTwinNode>();
     queue.addFirst(new BinaryTwinNode(origin, differ));
     BinaryTwinNode temp;

     while(queue.isEmpty() == false){    
        temp = queue.removeLast();

        if(temp.origin.value != temp.oppose.value){
            return false;
        }

        if(temp.origin.left != null && temp.oppose.left != null){
            queue.addFirst(new BinaryTwinNode(temp.origin.left, temp.oppose.left));

        }else if(temp.origin.left == null && temp.oppose.left != null){
            return false;
        }else if(temp.origin.left != null && temp.oppose.left == null){
            return false;
        }

        if(temp.origin.right != null && temp.oppose.right != null){
            queue.addFirst(new BinaryTwinNode(temp.origin.right, temp.oppose.right));

        }else if(temp.origin.right == null && temp.oppose.right != null){
            return false;
        }else if(temp.origin.right != null && temp.oppose.right == null){
            return false;
        }
     }
     //default
     return true;
 }

0 个答案:

没有答案