0
votes

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

1
how are you adding nodes to the tree? - attaboy182
Your left and right children are probably null, but without seeing the insertion method, it is hard to tell why. - Fjotten
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
Wait a sec to edit - D.A.G.D
Possible duplicate of What is a Null Pointer Exception, and how do I fix it? - user719662

1 Answers

0
votes

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