Recursion and binary trees

A-Level Computer Science · Recursion, OOP and File Handling

What makes a recursive routine work

Every recursive routine needs two things: a base case that stops the recursion, and a general case that calls itself with a value moving towards the base case. Missing either one gives infinite recursion, which fills the stack and crashes — that is why the stack matters here.

How the stack makes it possible

Each call pushes the return address and local variables onto the call stack. The calls unwind in reverse order, so the deepest call returns first. This is also the cost: recursion uses more memory than an equivalent loop, and deep recursion can cause stack overflow.

Recursion versus iteration

Recursion produces shorter, clearer code for problems that are naturally self-similar — tree traversal, directory structures, divide-and-conquer sorts. Iteration is faster and uses constant memory. For factorial or a simple sum, a loop is the better answer, and saying so shows judgement rather than enthusiasm.

Binary trees

A node holds data plus a left pointer and a right pointer. In a binary search tree, everything in the left subtree is smaller than the node and everything in the right subtree is larger. Searching therefore discards half the tree at each step, giving $O(\log_2 n)$ on a balanced tree — but $O(n)$ if items are inserted already sorted, because the tree degenerates into a list.

The three traversals

Pre-order: root, left, right — used to copy a tree. In-order: left, root, right — on a binary search tree this outputs the data in sorted order, which is the fact examiners test. Post-order: left, right, root — used to delete a tree safely and to evaluate expression trees.

Cay nhi phan tim kiem — trai NHO hon, phai LON hon502871153990Duyet GIUA (trai - goc - phai): 15 · 28 · 39 · 50 · 71 · 90=> duyet giua mot cay nhi phan tim kiem luon cho day DA SAP XEP
Remembering one sentence is enough for most tree questions: the IN-ORDER traversal of a binary search tree always gives an ascending sorted sequence.

Worked example: tracing recursion and tree traversal

(a) Trace the following function with $n=4$:

FUNCTION F(n)
  IF n <= 1 THEN RETURN 1
  ELSE RETURN n * F(n - 1)
  ENDIF
ENDFUNCTION

(b) Given a binary search tree created by inserting, in order: $40, 25, 60, 15, 30, 55, 75$. State the results of the pre-order, in-order and post-order traversals.

Solution

(a) Trace: notice that the calls go DOWN first, and only then do the returns come UP.

$F(4)$: $4>1$ so it calls $4\times F(3)$; it cannot be computed yet, it must wait.
$F(3)$: calls $3\times F(2)$
$F(2)$: calls $2\times F(1)$
$F(1)$: $1\le1$ ⇒ base case, returns $1$

Now unwind:

$F(2)=2\times1=2$
$F(3)=3\times2=6$
$F(4)=4\times6=\mathbf{24}$

At its deepest the stack holds four calls at the same time. This is exactly why recursion uses more memory than a loop: a FOR loop computing a factorial uses a single variable.

(b) Build the tree. $40$ is the root. $25<40$ so it goes left; $60>40$ goes right; $15<25$ so it goes to the left of $25$; $30>25$ so it goes to the right of $25$; $55<60$ so it goes to the left of $60$; $75>60$ so it goes to the right of $60$.

Structure: root $40$, left child $25$ (with $15$ and $30$), right child $60$ (with $55$ and $75$).

Pre-order (root, left, right): $40,\ 25,\ 15,\ 30,\ 60,\ 55,\ 75$

In-order (left, root, right): $15,\ 25,\ 30,\ 40,\ 55,\ 60,\ 75$

Post-order (left, right, root): $15,\ 30,\ 25,\ 55,\ 75,\ 60,\ 40$

Quick check: the in-order traversal must give an ascending sequence, and here it does. If your in-order result is not ascending, then either the tree was built wrongly or the traversal order is wrong; no further checking is needed.

Common mistake

Writing a recursive function with no base case, or with one that the call does not move toward (for example calling F(n) instead of F(n-1)). The result is infinite recursion and a stack overflow. The mark scheme always has a separate mark for the base case.

Key takeaway

Recursion needs a base case and a call that moves toward it; it uses stack space so it is not always better than a loop. For a binary search tree, remember that in-order gives a sorted sequence, and remember that the tree degenerates into a list if you insert already sorted data.

← Program design, testing and maintenance · All A-Level Computer Science lessons · Practice A-Level Computer Science questions (free, instant scoring) · Object-oriented programming →