尝试在删除后重新平衡AVL树时出现NullPointerException

时间:2015-06-17 00:45:14

标签: java avl-tree

我试图在删除时重新平衡AVL树,但是我得到一个空指针错误。我在运行调试器和放置print语句时观察到的错误来自balance方法。当我试图获得高度差异时,它会抛出NullPointerException。当插入AVL树时,我使用了相同的方法,我能够平衡树。但是当我尝试删除时,我得到一个NullPointerException。当我插入50,60,40和20时,我测试了这个案例,因为这已经是一棵平衡的树了。当我尝试删除60时,在删除60后尝试重新平衡树时,我得到了NullPointerException。我做错了什么?

代码:AVLTree.java

public void delete(T data){
    root = delete(root, data); //error line 130
}
private AVLNode<T> delete(AVLNode<T> root, T data){
    if(root==null){
        return root;
    }
    if(data.compareTo(root.getData())<0){
        root.setLeftChild(delete(root.getLeftChild(), data));
    }
    //error line 140 when attempting to delete 60
    else if(data.compareTo(root.getData())>0){
        root.setRightChild(delete(root.getRightChild(), data));
    }
    else{
        if(root.isLeaf()){
            root=null;
        }
        else if(root.getLeftChild()==null){
            root = root.getRightChild();
        }
        else if(root.getRightChild()==null){
            root = root.getLeftChild();
        }
        else{
            T value = findMin(root.getRightChild());
            root.setValue(value);
            root.setRightChild(delete(root.getRightChild(), root.getData()));
        }
    }
    root = rebalance(root);
    updateHeight(root); //error AVLTree.java line 160
    return root;
}
//gets the difference of the left and right node.
private int balance(AVLNode<T> root) {
    return getHeight(root.getLeftChild())-getHeight(root.getRightChild()); //get null pointer exception when attempting to get the difference..
}
//updates the height of the tree.
private void updateHeight(AVLNode<T> root){
    if((root.getLeftChild()==null) && (root.getRightChild()!=null)){
        root.setHeight(root.getRightChild().getHeight()+1);
    }
    //highlights error here:AVLTree.java line 251
    else if((root.getLeftChild() !=null)&&(root.getRightChild()==null)){
        root.setHeight(root.getLeftChild().getHeight()+1);
    }
    else
        root.setHeight(Math.max(getHeight(root.getLeftChild()), getHeight(root.getRightChild()))+1);
}

//re-balances the tree.
private AVLNode<T> rebalance(AVLNode<T> root){
    int difference = balance(root);
    if (difference > 1){
        if(balance(root.getLeftChild())>0){
            root = rotateRight(root);
        }
        else{
            root = rotateLeftRight(root);
        }
    }
    else if(difference < -1){
        if(balance(root.getRightChild())<0){
            root = rotateLeft(root);
        }
        else{
            root = rotateRightLeft(root);
        }
    }
    return root;
}

异常消息:

    java.lang.NullPointerException
    at avl.AVLTree.updateHeight(AVLTree.java:251)
    at avl.AVLTree.delete(AVLTree.java:160)
    at avl.AVLTree.delete(AVLTree.java:140)
    at avl.AVLTree.delete(AVLTree.java:130)
    at avl.Test.main(Test.java:28)

错误Test.java:128是我调用方法tree.delete(60);

的时候

1 个答案:

答案 0 :(得分:1)

以下是我的解决方案。 如果root的leftChild或rightChild为null,我忘了捕获异常。感谢@stvcisco捕获错误。

if(root==null) {
  return root;
  }

在调用重新平衡方法之前。