Recursion and the call stack
Trace a recursive function over a linked list as frames pushed and popped on the call stack, and use the trace to give its depth and space.
It opens on
This function adds up the linked list 4, 9, 2 by calling itself. Counting the first call, how many calls to total are made before the answer comes back?
Why it matters
Every recursive algorithm in the rest of the course, from tree traversals to merge sort and graph search, runs on this stack, and a RecursionError in a real program is the stack running out.
Good to know first
- Stacks: push adds on top and pop removes the top, so the last item pushed leaves first
- Singly linked lists: a Node has data and next, and the last node's next is None
- Recursion: base case and recursive case: a recursive function stops at a base case and otherwise calls itself on something smaller
- Scope and the call stack: each call has its own local names
Step by step
Frames on the call stack
Trace total on a linked list as frames pushed and popped, and say how many calls are made and how deep the stack gets.
The first call has node at the 4. Line 4 cannot add until total(node.next) gives back a number, so this call pauses with 4 + ? still to do, and a second frame for the 9 is pushed on top. It pauses too, with 9 + ? waiting.
the stack column with the frames 4+? and 9+?
A common mix-up
The recursion adds the numbers on the way down, so the answer is ready when the base case is reached.
In fact: The base case only returns 0; the additions wait in the paused frames and are done one at a time as the frames are popped.
Work before and after the call
Write a recursive function over a linked list from a base case and a call on node.next, and tell from where its work sits whether it runs on the way down or on the way back.
show(head) prints 4 and then calls show on the 9, which prints 9, then the 2 prints 2, and the call on None does nothing. The prints happen on the way down, in list order, so show prints 4, 9, 2.
A common mix-up
A line after the recursive call runs as soon as the call is made.
In fact: It waits in its frame, and runs only when the call above it has returned.
What the stack costs
State the number of calls, the deepest stack and the extra space of a recursive function on a list of n nodes, and tell when a loop must be used.
CPython allows 1000 frames by default, so on a list of 1000 nodes the run stops with a RecursionError, maximum recursion depth exceeded, and gives no answer.
the longer lists and the 1000 row
A common mix-up
Recursion costs no memory, because it creates no list.
In fact: Every call still waiting is a frame on the stack, so the space is Θ(n) for a list of n nodes.
Try it
For the list 5, 1, what does total return, how many calls are made, and how deep does the stack get?Answer
6; 3 calls; 3 frames
Hint: One call for each node and one for None
What does this print on the list 3, 6: def both(node): if node is not None: print(node.data); both(node.next); print(node.data)?Answer
3 6 6 3
Hint: The first print is before the call and the second after it
total is called on a list of 250 nodes. How many frames are on the stack at the deepest, and does it fail with the default limit?Answer
251 frames; it does not fail, since 251 is under 1000
Hint: n + 1 frames
The frame for the 9 calls total on its node.next. What is that node, and what is on the stack, bottom to top, once that call has started?Answer
Node.next is the 2, and the stack is the frames for 4, 9 and 2, with the 2 on top.
In show_rev the print waits below the recursive call. On the list 4, 9, 2, when the call on None returns, which frame prints first, and what does show_rev print in all?Answer
The frame for the 2 prints first, then the 9, then the 4, so it prints 2, 9, 4.
