Hello!
I've been staring at my code for a while and I really have no idea where I'm going wrong. I'm working on deleting a specific node (for example, the integer 317)....but for some reason I just keep getting a nullpointer expression. I get the error when it goes into if ((hp.getLp() != null) && (hp.getRp() != null)). I've tried numerous things, but I've gotten to the point where I can't tell where it is messing up exactly. Would someone be able to point me in the right direction? Thank you so much!
public void Delete1(int val)
{
root = Delete2(root, val);
}
public Node Delete2(Node hp, int val)
{
//Empty case
if (hp == null)
{
System.out.println("The following data was not found: " + val);
return hp;
}
//Need to go down right tree
if (val < hp.getData())
{
//search further
hp.setLp(Delete2(hp.getLp(), val));
return hp;
}
//Need to go down left tree
else if (val > hp.getData())
{
//search further
hp.setRp(Delete2(hp.getRp(), val));
return hp;
}
else
//(dx == hp.getData() found the node
{
//Node has been found
//else must be dx == hp.getData()
System.out.println("Deleting: " + val);
if ((hp.getLp() == null) && (hp.getRp() == null))
{
return null;
}
if ((hp.getLp() == null) && (hp.getRp() != null))
{
return hp.getRp();
}
if ((hp.getLp() != null) && (hp.getRp() == null))
{
return hp.getLp();
}
if ((hp.getLp() != null) && (hp.getRp() != null))
{
//find the maximum
Node max = FindMax1(hp.getLp());
//Bring max data to hp
hp.setData(max.getData());
hp.setLp(Delete2(hp.getLp(), max.getData()));
return hp;
}
return hp;
}
}
public Node FindMax1(Node hp)
{
int result = Integer.MAX_VALUE;
if(hp.getLp() != null)
{
result = hp.getData();
System.out.println(result);
}
else
{
return null;
}
return null;
}