f (int n){
if (n<=0){
return 1;
}
return f(n-1) + f(n-1);
}
Suppose we did f(4). My thought was that it would be O(2^n), since then in order to find f(n-1) + f(n-1) we would have to push f(n-1) = f(3) to the call stack twice, and then f(2) four times the call stack, etc. However, the book I got this from says that is is O(n). Why is that true?
Let's imagine evaluating this for f(4) (the example you consider). Here's how it would go. The stack starts by looking like
Then the calculation of f(4) recurs to `f(3), and the stack looks like
Then we keep going down and we eventually get to
Then, we can calculate f(0) as 1, and the last call returns. The penultimate call (the one to compute f(1)), then wants to compute the second copy of f(0), and the stack goes to:
Then that returns, and so the computation of f(1) can return, and we get to
and from there the stack becomes:
and it will continue computing f(1) as before.
The point is that the stack only ever gets as deep as n, even though (ultimately) 2^n operations will be performed. So the time complexity is O(2^n) but the space complexity is only O(n).