0

I have a project in which I have to create a bst. So my problem is that I have to create a boolean method in which I check if the node is the Right/Left child of his Father.

public class TreeNode 
{
  private int data;
  private TreeNode right;
  private TreeNode left;
  private TreeNode parent;

 **assesor and mutator methods**

  public TreeNode (int data)
  {
    this.data = data;
    right = null;
    left = null;
  }
  public boolean isRightChild (TreeNode parent)
  {
    if (parent.right.data == data)      
    {
        return true;
    }
    return false;
  }

  public boolean isLeftChild (TreeNode parent)
  {
    if (parent.left.data == data)       
    {
        return true;
    }
    return false;
  }
}

and I call this method in another class with this...

node2.isRightChild(parent)

or

node2.isLeftChild(parent)

in compiler it shows me NullPointerException the insertion method (in another class) is...

public void insert(int data)
{
    TreeNode element = new TreeNode (data);
    if (node == null)
    {
        node = element;
        return;
    }
    TreeNode node2 = node;
    TreeNode parent = null;
    while(true)
    {
        parent = node2;
        if (data < node2.getData())
        {
            node2 = node2.getLeft();
            if (node2 == null)
            {
                parent.setLeft(element);
                return;
            }
        }else
        {
            node2 = node2.getRight();
            if(node2 == null)
            {
                parent.setRight(element);
                return;
            }
        }
    }
}

UPDATE: My remove method is the following...

public boolean remove(int data)
{
    TreeNode parent = node;
    TreeNode node2 = node;

    //searching through the nodes
    while(node2.getData() != data)
    {
        parent = node2;
        if(node2.getData() > data)
        {
            node2 = node2.getLeft();
        }else
        {
            node2 = node2.getRight();
        }
    }
    //empty node
    if (node == null)
    {
        return false;
    }
    //leaf
    if(node2.isLeaf(data))
    {
        if(node2 == node)
        {
            node = null;
        } 
        if(node2.isRightChild(parent))
        {
            parent.setRight(null);
        }else
        {
            parent.setLeft(null);
        }
    }

    //node with 1 child
    else if((node2.getLeft() == null))
    {
        if (node2 == node)
        {
            node = node2.getRight();
        }else if(parent.isRightChild(node2))
        {
            parent.setRight(null);
        }else
        {
            parent.setLeft(null);
        }
    }
    else if((node2.getRight() == null))
    {
        if(node2 == node)
        {
            node = node2.getLeft();
        }else if (node2.isLeftChild(parent))
        {
            parent.setLeft(null);
        }else
        {
            parent.setLeft(null);
        }
    }
    //node with 2 children
    else if ((node2.getLeft() != null) && (node2.getRight() !=null))
    {
        TreeNode min = minFromRightSide(node2);
        if(node2 == node)
        {
            node = min;
        }else if(node2.isLeftChild(parent))
        {
            parent.setLeft(min);
        }else
        {
            parent.setRight(min);
        }
        min.setLeft(node2.getLeft());
    }
    return true;
}

So finishing the problem is that with this remove method when node has one child instead of removing 1 node it removes this node AND his child... So when I try to remove the child later I obviously have NullPointerException

D.A.G.D
  • 23
  • 1
  • 11
  • how are you adding nodes to the tree? – attaboy182 May 03 '16 at 20:10
  • Your `left` and `right` children are probably null, but without seeing the insertion method, it is hard to tell why. – Fjotten May 03 '16 at 20:12
  • I insert a number from main and it compares it with the others... I follow the logic of BST... The problem actually is on remove a node which have 1 child! So I have to use this method.. – D.A.G.D May 03 '16 at 20:15
  • Wait a sec to edit – D.A.G.D May 03 '16 at 20:19
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) –  May 05 '16 at 20:24
  • its not a duplicate... see the edit – D.A.G.D May 05 '16 at 20:37

1 Answers1

0

You are checking incorrectly. the NullPointerException is being thrown because when there isn't a left or right child it is pointing to nothing.

Try this

public boolean isRightChild (TreeNode parent)
  {
    if (parent.right != null) //checks if a right child exists     
    {
        return true;
    }
    remove(parent);
    return false;
  }

  public boolean isLeftChild (TreeNode parent)
  {
    if (parent.left != null)  //checks if a left child exists     
    {
        return true;
    }
    remove(parent);
    return false;

  }

UPDATE: If you wanted to remove the node here is a method that can do that. (I use it for my BST)

private void remove(Node node)
   {
      E theData;
      Node parent, replacement;
      parent = findParent(node);
      if ((node.left != null) && (node.right != null))
      {
         replacement = node.right;
         while (replacement.left != null)
            replacement = replacement.left;
         theData = replacement.data;
         remove(replacement);
         node.data = theData;
      }
      else
      {
         if ((node.left == null) && (node.right == null))
            replacement = null;
         else if (node.left == null)
            replacement = node.right;
         else
            replacement = node.left;
         if (parent==null)
            root = replacement;
         else if (parent.left == node)
            parent.left = replacement;
         else
            parent.right = replacement;
      }
   }
sbowde4
  • 767
  • 1
  • 4
  • 23
  • actually I have 2 cases which are using this method... one of them doesnt remove the node and the other removes this node with its children.... – D.A.G.D May 04 '16 at 13:20
  • You aren't removing the node in your original post when finding the children, if you wanted to remove the node you should write a remove method and then call it. – sbowde4 May 04 '16 at 20:03
  • First of all thanks for your effort.... Apart from that... I made my own remove method which has the problem I told in the previous comment... I update my original post so you can see it... Also i could use your method which i think it will work but the project asks for the remove method to use an Integer (data)... – D.A.G.D May 05 '16 at 20:19