I am trying to write a boolean recursive method that inserts values into a binary search tree. It returns true if the value is not already there and inserts it and returns false if there already is that same value there and the list does not get changed. I think this works for the most part, but it fails to return false repetitively when multiple values are already there. For example, if the tree already contains 3 9 6 7 and I try to insert 3, it returns false as it is the first time. But then when I try to insert 9 6 or 7, it always returns true, which it should not as those values are already there.
public boolean insert(int value) {
Node travel;
travel=insert(value,root);
if (travel==null)
return false;
root=travel;
return true;
}
private Node insert(int value, Node travel) {
if (travel==null) {
travel=new Node(value);
return travel;
}
if (value>travel.data) {
travel.right=(insert(value,travel.right));
return travel;
}
if(value<travel.data) {
travel.left=(insert(value,travel.left));
return travel;
}
return null;
}