Question

Im writing code to delete a node in a binary tree.All cases work except the "delete root with 2 children" case. Here's my code:

Main.java

public class Main {

public static void main(String[] args) {
   BinaryTree binaryTree = new BinaryTree();
    binaryTree.add(50);
    binaryTree.add(40);
    binaryTree.add(39);
    binaryTree.add(42);
    binaryTree.add(41);
    binaryTree.add(43);
    binaryTree.add(55);
    binaryTree.add(65);
    binaryTree.add(60);

    binaryTree.inOrderTraversal(binaryTree.root);
    System.out.println();
    binaryTree.removeNode(50);

    binaryTree.inOrderTraversal(binaryTree.root);
}
}

BinaryTree.java

public class BinaryTree {
Node root = null;
Node deleteNode = null;
boolean isLeftChild = false;
Node parent = root;
public void add(int d)
{
    Node newNode =  new Node(d);
    if(root!=null)
    {


        Node futureParent = root;
        while(true)
        {
        if(newNode.data < futureParent.data)      //going left
        {
            if(futureParent.left == null)
            {
                futureParent.left = newNode;
                newNode.parent = futureParent;
                break;
            }
            futureParent = futureParent.left;

        }
        else
        {
            if(futureParent.right == null)
            {
                futureParent.right = newNode;
                newNode.parent = futureParent;
                break;
            }
            futureParent = futureParent.right;
        }

        }

    }
    else
    {
        root = newNode;
    }
}
public void inOrderTraversal(Node node)
{
    if(node!=null)
    {
    inOrderTraversal(node.left);
    System.out.println(node.data);
    inOrderTraversal(node.right);
    }
}

public void findNode(int n)
{

}

public void removeNode(int n)
{


    deleteNode = root;

    while(deleteNode!=null)
    {
        if(n == deleteNode.data)
        {
            break;
        }
        parent = deleteNode;
        if(n < deleteNode.data)
        {

            deleteNode = deleteNode.left;
            if(deleteNode.data == n)
            {
                isLeftChild = true;
                break;

            }
        }
        else
        {
            deleteNode = deleteNode.right;
            if(deleteNode.data == n)
            {
                isLeftChild = false;
                break;
            }
        }
    }

    //Case 1: No children at all
    if((deleteNode.left == null)&&(deleteNode.right == null))
    {
      parent.right = null;

        //incomplete code
    }
    //Case 2:No right child
    else if(deleteNode.right == null)
    {
    deleteNoRightChild();
    }
    //Case 3:No left child
    else if(deleteNode.left == null)
    {
           deleteNoLeftChild();
    }

    //Case 4:Both Children
    else
    {
        Node minRightNode = deleteNode.right;
        while (minRightNode.left != null)
        {
            parent = minRightNode;
            minRightNode = minRightNode.left;
    }
       // minRightNode.parent.left = null;
        deleteNode.data = minRightNode.data;
        deleteNode = minRightNode;
        if(minRightNode.left == null)
        {
            deleteNoLeftChild();
        }
        else if(minRightNode.right == null)
        {
            deleteNoRightChild();
        }
        else if((minRightNode.right == null)&&(minRightNode.left == null))
        {
            minRightNode.parent.left = null;
        }
    }
}

private void deleteNoLeftChild() {
    if(deleteNode == root)
    {
        root = deleteNode.right;
    }
    if(isLeftChild)
    {
        parent.left = deleteNode.right;
    }
    else
    {
        parent.right = deleteNode.right;
    }
}

private void deleteNoRightChild() {
    if(deleteNode == root)
    {    //Case 2.1:deleteNode is root
        root = deleteNode.left;
    }
    if(isLeftChild)
    {
        //Case 2.2: Case 2, and its a left child
        parent.left = deleteNode.left;
    }
    else
    {
        //Case 2.3: Case 2, and its a right child
        parent.right = deleteNode.left;
    }
}
}

Node.java

public class Node {
int data;
Node left;
Node right;
Node parent;


public Node(int d)
{
   data = d;
   left = null;
   right = null;
}
}

As you can see, I am trying to delete 50 in Main.java.I debugged my code and tracked down the location giving out the NullPointerException and its in the deleteNoLeftChild() routine that I wrote.Following is a screenshot: Debugger

Please open image in new tab.

The blue line highlighted in the code window is the point of exception which is strange as the right side of the assignment(deleteNode.right) is not null.

Was it helpful?

Solution

The problem lies within the lines

//Case 4:Both Children
else
{
    Node minRightNode = deleteNode.right;
    while (minRightNode.left != null)
    {
        parent = minRightNode;
        minRightNode = minRightNode.left;
    }
 ... 
}

You are setting the parent only if the minimum right node has a left child. In your scenario this is not true, hence parent not set/ null. You can test this by changing the layout of the tree.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top