Recursion: Base Case and Decomposition

Beginner Algorithms and Data Structures English
Also available: Deutsch
Created by Best · 16.08.2026 at 09:13 UTC

Recursion solves a problem by calling the same problem on smaller instances. Every recursive method needs at least one base case that answers without self-calling, and one recursive case that guarantees progress toward the base case.

Example: Length of a list: null -> 0; otherwise 1 + length of the rest.

Without a base case or without reduction, infinite recursion occurs.

$$\mathrm{fact}(n)=n\cdot\mathrm{fact}(n-1),\quad \mathrm{fact}(0)=1$$

Where used

File tree walks, DOM/JSON trees, parsers, divide-and-conquer, backtracking. Anywhere data is recursively structured, recursion is the direct mapping of the structure onto control flow.

Depth

A recursive definition requires a base case and a progress measure. For every recursive call, this measure must become smaller in a well-founded order. Only then does termination follow from the structure of the problem.

The base case is not merely a technical termination. It establishes the smallest significance of the function, from which larger results are built. An incorrect return there distorts all higher levels.

Correctness can often be shown inductively: The base case holds true, and assuming correct smaller results, the recursion step produces the correct larger result.

Difficulty levels

  1. Identify the base case and recursive step.
  2. Specify a strictly descending termination measure.
  3. Link an inductive proof to the implementation.

Pitfalls

An achievable base case is not sufficient if some paths do not reduce the measure. Also, a base case checked too late can already trigger an invalid access.

University approvals: 0
Tasks
Question 1

What must each recursive solution contain?

Question 2

What property provides a viable termination argument?

Question 3

Calculate the length of a linked list recursively.

Hint

The list end is the reference null. A recursive call to the static method uses the same method signature.

Starter code is prefilled; replace TODO blocks with your solution.
1 test case will be used for grading
Run checks runtime behavior only. Final correctness is evaluated when you submit.
Card Info
  • Topic: Algorithms and Data Structures
  • Difficulty: Beginner
  • Completed: 0 users
Creator
Best
Best
BestBuddy