I am implementing different methods in a binary search tree and am stuck on the insert method as it just doesn't seem to work.
I have been trying to implement the insert method for a while now but nothing seems to work it's always returning null. This method takes a user and adds it to the database. Using the User class.
public boolean beFriend(User friend) throws IllegalArgumentException {
User node = friend;
if (friend == null) {
throw new IllegalArgumentException();
}
if(root == friend) {
return false;
} else if(root.getKey() < friend.getKey()) {
if(root.getLeft() != null) {
root.setLeft(friend);
return true;
} else {
root.setLeft(node);
return true;
}
} else { if(root.getRight() != null) {
root.setRight(friend);
} else {
root.setRight(node);
return true;
}
}
return false;
}
I expect the User friend to be added to the database and output its details but the output that I am currently getting is null.