removeRoot()的奇怪现象

时间:2014-03-04 22:54:15

标签: java recursion tree binary-tree

当我遇到无法解释的现象时,我正在努力实现自己的TreeSet。编写所需的方法之一是使用public boolean remove(Comparable valueprivate TreeNode remove(TreeNode root, Comparable value)的{​​{1}}。方法包括在下面。

我的问题是两个单独的private TreeNode removeRoot(TreeNode root)方法的两个结果之间的差异,当它们应该做同样的事情时。实施方式不同,但结果为何呢?

为了澄清,每个removeRoot(TreeNode root)都有一个左右孩子,因为它是一棵二叉树。 TreeNode类包含方法:

TreeNode

以下是Tester类,public Comparable getValue() { return value; } public TreeNode getLeft() { return left; } public TreeNode getRight() { return right; } public void setValue(Comparable theNewValue) { value = theNewValue; } public void setLeft(TreeNode theNewLeft) { left = theNewLeft; } public void setRight(TreeNode theNewRight) { right = theNewRight; } public remove()private remove()以及两种结果。假设所有内容都已正确添加到树中。

删除方法

removeRoot()

removeRoot()#1

public boolean remove(Comparable value){
    if(!this.contains(value)){return false;}
    else{
        this.remove(rootVal, value);
        representation=this.toString();
        return true;
    }
}

private TreeNode remove(TreeNode root, Comparable value){
    if(root!=null){
        if(rootVal.getValue().compareTo(value)==0){
            rootVal=this.removeRoot(root);
            count--;
            return rootVal;
        }
        if(root.getLeft()!=null && root.getLeft().getValue().compareTo(value)==0){
            root.setLeft(this.removeRoot(root.getLeft()));
            count--;
            return root;
        }
        else if(root.getRight()!=null && root.getRight().getValue().compareTo(value)==0){
            root.setRight(this.removeRoot(root.getRight()));
            count--;
            return root;
        }
        if(root.getLeft()!=null && root.getValue().compareTo(value)>0){remove(root.getLeft(), value);}
        if(root.getRight()!=null && root.getValue().compareTo(value)<0){remove(root.getRight(), value);}
    }
    return rootVal;
}

输出#1

private TreeNode removeRoot(TreeNode root){
    TreeNode original=root;
    if(root.getLeft()==null){return root.getRight();}
    else if(root.getRight()==null){return root.getLeft();}

    root=root.getRight();
    if(root.getLeft()==null){
        root.setLeft(original.getLeft());
        return root;
    }
    TreeNode previous=null;
    while(root.getLeft()!=null){
        root=root.getLeft();
        if(root.getLeft()!=null && root.getLeft().getLeft()!=null){previous=root;}
    }
    if(previous!=null){previous.setLeft(root.getRight());}
    root.setLeft(original.getLeft());
    root.setRight(original.getRight());
    return root;
}

removeRoot()#2

 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 
Remove 3:
true
 0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 
false
Remove 16:
 0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 25, 
Remove 17:
 0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18, 19, 20, 21, 22, 23, 24, 25, 
Remove 5:
 0, 1, 2, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18, 19, 20, 21, 22, 23, 24, 25, 
Remove 6:
 0, 1, 2, 4, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18, 19, 20, 21, 22, 23, 24, 25, 
Remove 7:
 0, 1, 2, 4, 8, 9, 10, 11, 12, 13, 14, 15, 18, 19, 20, 21, 22, 23, 24, 25, 

输出#2

private TreeNode removeRoot(TreeNode root)
{
    TreeNode temp = root.getRight();
    TreeNode prev = temp;
    if(temp == null) {
        return root.getLeft();
    }
    if(temp.getLeft() == null)
    {
        temp.setLeft(root.getLeft());
        return temp;
    }
    while(temp.getLeft() != null) {
        prev = temp;
        temp = temp.getLeft();
    }
    temp.setLeft(root.getLeft());
    prev.setLeft(temp.getRight());
    temp.setRight(root.getRight());
    return temp;
}

测试人员方法

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 
Remove 3:
true
 0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 
false
Remove 16:
 0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 25, 
Remove 17:
 0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18, 19, 20, 21, 22, 23, 24, 25, 
Remove 5:
 0, 1, 2, 4, 6, 10, 11, 12, 13, 14, 15, 18, 19, 20, 21, 22, 23, 24, 25, 
Remove 6: //java.lang.StackOverflowError: null

0 个答案:

没有答案