Good morning. I am trying to draw the trace that follows this recursive fibonacci Java code to see exactly how recursivity works in this case. I do not quite understand the exact operation in this particular case since in the return it is assumed that it is returned and it leaves the function in which it is but in the return recursively enters 2 methods that in turn if it is not 0 or one returns again 2 recursive methods coming out of that method in question.
The code is as follows:
public int fibonacci(int n) {
if ((n == 0) || (n == 1)) // base cases
return n;
else
// recursion step
return fibonacci(n - 1) + fibonacci(n - 2);
}
Could someone explain to me the trace that follows the program and on each pass the result of n at the end of each function for each time it is called?
Does the return really cause that there is not a stack leaving the function and calling in turn to 2 functions as a tail recursion?