NullPointerException when attempting to re-balance AVL tree after deletion

1.3k Views Asked by At

I am trying to rebalance an AVL tree upon deletion but I get a null pointer error. The error I observed from running the debugger and placing a print statement comes from the balance method. It throws a NullPointerException when I attempt to get difference of height. When inserting into the AVL tree I used the same approach and I was able to balance the tree. But when when trying to delete, I get a NullPointerException. I tested the case when I insert 50, 60, 40 and 20 as this is already a balanced tree. When I tried deleting 60, I got a NullPointerException when trying to rebalance the tree after deleting 60. What did I do wrong?

code: 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;
}

Exception message:

    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)

Error Test.java:128 is when i call the method tree.delete(60);

1

There are 1 best solutions below

0
On

Below is my solution. I forgot to catch the exception if root's leftChild or rightChild was null. Thanks to @stvcisco for catching the error.

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

right before calling the rebalance method.