2021年4月7日星期三

Maximum depth of a theoretical function stack due to a recursive function?

If we have a theoretical stack memory that never gets full and we have a simple recursive function

recurse(n):      if n > 0:          recurse(n-1)          recurse(n-2)      return  

Is it reasonable to argue that the theoretical stack has at most n stack frames at any point in the execution of recurse(n) since it is impossible for recurse(k) to be on top of recurse(i) if 0 <= k < i <= n, since this implies that recurse(i) called recurse(k) (which is impossible based on the function body). If my reasoning is correct, then the maximum depth must be the case when the function stack looks like the following:

(BOTTOM-MOST)|recursion(n)|recursion(n-1)|...|recursion(2)|recursion(1) (TOP-MOST)

https://stackoverflow.com/questions/66988823/maximum-depth-of-a-theoretical-function-stack-due-to-a-recursive-function April 07, 2021 at 11:03PM

没有评论:

发表评论