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)
没有评论:
发表评论