Hello I basically need to implement a recursive function for insertion of a binary tree. I already implemented the chunk of insertion function (wether it's largest or smaller than root) but there's an aspect of confusion.
public void insert(E data) {
root = insert(root, data);
}
private Node<E> insert(Node<E> value, E data) {
if(value == null) {
return new Node<E>(data);
}
else if (data.compareTo(value.data) > 0 ) {
value.right = insert(value.right, data);
}
else if(data.compareTo(value.data) <= 0) {
value.left = insert(value.left, data);
}
return value;
}
The problem I have is this line:
public void insert(E data) {
root = insert(root, data);
}
Why do I need that line of code? Is the root actively changing? My partner tried explaining to me how it doesn't change except for the first root.
Is the private function for the recursion always returning the first parent root as the root?
Thank you.