二进制搜索树删除方法不起作用

时间:2016-09-13 04:18:34

标签: java binary-search-tree

public class BSTNode <E extends Comparable<E>> {

private E value;
private BSTNode<E> left;
public BSTNode<E> right;
....


public BSTNode<E>remove(E item) {
    if(item.equals(this.value)){

        return replacementSubtreeFromChildren(this.left, this.right);

    }  
    if (item.compareTo(this.value)<0){
     this.left = this.left.remove(item);
    }
    else{this.right = this.right.remove(item);
    }
    return this;
}

private BSTNode<E> replacementSubtreeFromChildren(BSTNode<E> left,       BSTNode<E> right) {
    if(left==null && right==null){      
        return null;
    }
    else if(left!=null && right==null){    
        return this.left;
    }
    else if(left==null && right!=null){        
        return this.right;       
    }
    else{
     E getleft=this.right.getLeftmostNode().getValue();
     this.value = getleft;
     this.right = right.remove(getleft);


    }
    return this;
}

/**
 *  Returns the leftmost node in the subtree formed by the receiver. 
 *  
 *  COMPLETION
 *  
 *  HINT: The code is very simple. Just keep descending left branches, 
 *  until it is no longer possible. 
 * 
 * @returns a reference to the leftmost node, starting from the receiver.    
 *                                          
 */
private BSTNode<E> getLeftmostNode() {
   if (this.left == null) {
    return this;
   }
   else{
       return this.left.getLeftmostNode();
  }

由于某种原因,此方法不会从树中删除。 任何人都可以帮我找到为什么这样做吗?

我已经对它进行了测试并且正在访问所有方法,我怀疑我的指针位置错误,但找不到位置。

1 个答案:

答案 0 :(得分:0)

这对于本学期的维多利亚大学的comp103作业6来说是