Lecture 9
Lecture 9
Recursion
In Java, Recursion is a process in which a function calls itself directly or indirectly is called recursion
and the corresponding function is called a recursive function. Using a recursive algorithm, certain
problems can be solved quite easily. A few Java recursion examples are Towers of Hanoi (TOH),
Inorder/Preorder/Postorder Tree Traversals, DFS of Graph, etc.
2
Recursion Example
Adding two numbers together is easy to do, but adding a range of numbers is more complicated. In the
following example, recursion is used to add a range of numbers together by breaking it down into the
simple task of adding two numbers:
3
Explanation of an example
When the sum() function is called, it adds parameter k to the sum of all numbers smaller than k and
returns the result. When k becomes 0, the function just returns 0. When running, the program follows
these steps:
4
Halting(base) Condition
Just as loops can run into the problem of infinite looping, recursive functions can run into the problem
of infinite recursion. Infinite recursion is when the function never stops calling itself. Every recursive
function should have a halting condition, which is the condition where the function stops calling itself.
In the previous example, the halting condition is when the parameter k becomes 0.
It is helpful to see a variety of different examples to better understand the concept. In this example,
the function adds a range of numbers between a start and an end. The halting condition for this
recursive function is when end is not greater than start:
5
Example
6
Attention
The developer should be very careful with recursion as it can be quite easy to slip into writing a
function which never terminates, or one that uses excess amounts of memory or processor power.
However, when written correctly recursion can be a very efficient and mathematically-elegant
approach to programming.
7
Base Condition in Recursion
In the recursive program, the solution to the base case is provided and the solution to the bigger
problem is expressed in terms of smaller problems. The idea is to represent a problem in terms of one
or more smaller sub-problems and add base conditions that stop the recursion. For example, we
compute factorial n if we know the factorial of (n-1). The base case for factorial would be n = 0. We
return 1 when n = 0.
8
Factorial Using Recursion
The classic example of recursion is the computation of the factorial of a number. The factorial of a
number N is the product of all the numbers between 1 and N. The below-given code computes the
factorial of the numbers: 3, 4, and 5.
9
Fibonacci Series
Fibonacci Numbers are the numbers is the integer sequence where Fib(N) = Fib(N-2) + Fib(N-1). Below is
the example to find 3,4,5.
Fib(3) = Fib(2) + Fib(1) = Fib(1) + 0 + 1 = 1+1 = 2
Fib(4) = Fib(3) + Fib(2) = 2+1 = 3
Fib(5) = Fib(4) + Fib(3) = 3 + 2 = 5
10
Fibonacci Series
11
Stack Overflow error
If the base case is not reached or not defined, then the stack overflow problem may arise. Let us take
an example to understand this.
12
How is memory allocated to different
function calls in recursion?
When any function is called from main(), the memory is allocated to it on the stack. A recursive
function calls itself, the memory for the called function is allocated on top of memory allocated to the
calling function and a different copy of local variables is made for each function call. When the base
case is reached, the function returns its value to the function by whom it is called and memory is de-
allocated and the process continues.
Let us take the example of recursion by taking a simple function.
13
Explanation of an example
When printFun(3) is called from main(), memory is allocated to printFun(3), a local variable test is
initialized to 3, and statements 1 to 4 are pushed on the stack as shown below diagram. It first prints
‘3’.
In statement 2, printFun(2) is called and memory is allocated to printFun(2), a local variable test is
initialized to 2, and statements 1 to 4 are pushed in the stack. Similarly, printFun(2) calls printFun(1)
and printFun(1) calls printFun(0). printFun(0) goes to if statement and it return to printFun(1).
The remaining statements of printFun(1) are executed and it returns to printFun(2) and so on. In the
output, values from 3 to 1 are printed and then 1 to 3 are printed.
14
Advantages/disadvantages of Recursive
Programming
The recursive program has greater space requirements than the iterative program as all functions will
remain in the stack until the base case is reached.
It also has greater time requirements because of function calls and returns overhead.
15
Used links
1)https://www.w3schools.com/java/java_recursion.asp
2)https://www.geeksforgeeks.org/recursion-in-java/
16