Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Computer Science Introduction to Algorithms Recursion and Space Complexity Space Complexity

Jeremy Ashcraft
Jeremy Ashcraft
14,786 Points

each recursive call is put in memory on the stack, why do we not factor this into the space complexity calc?

since each recursive call results in a new frame on the stack, thus requiring more memory with each recursive call—why do we not factor in the growing memory requirements caused by recursion when assessing the efficiency of space used in our algorithm?

is the extra memory negligible? is this largely dependant on the language's implementation of recursion? does tail optimization resolve this?

1 Answer

Steven Parker
Steven Parker
229,670 Points

Each frame does add some space, but it's the same amount for each call. Since the actual quantity of bytes isn't used for the purposes of complexity analysis, the fact that a portion of the total is taken up by the frame doesn't change the complexity factor.