I've written a boolean insert method that inserts values into a binary search tree which inserts the value if the value is not already there and returns true if so, and returns false if the value is already there so inserts nothing. I am trying to convert this iterative method into all recursion with no loops at all but I am having trouble with figuring out how. Any help is appreciated!
public boolean insert(int value) {
Node travel= root, prev= null, newNode;
boolean result= true;
while (travel != null && travel.data != value) {
prev= travel;
if (value < travel.data)
travel= travel.left;
else travel= travel.right;
}
if (travel != null)
result= false;
else
if (root == null)
root= new Node(value);
else
if (value < prev.data)
prev.left= new Node(value);
else prev.right= new Node(value);
return result;
}