Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Ch.

Recursion
Recursion: An act of calling a function within its body is known as recursion.
void Rec()

{
.
.
.
Rec();

Recursion and Iteration: Any problem that is solved through iteration can also be solved with recursion and vice-
versa.

Example: To find GCD of two numbers a and b (Where b>a), We can use any of the following method.

Using Iteration Using Recursion


int gcd (int a, int b) int gcd (int a, int b)
{ {
while( a!=0) if (a!=0)
{ {
int r=b%a; int r=b%a;
b=a; return gcd(r,a);
a=r; }
} else
return b; return b;
} }

Difference between Iteration and Recursion


Iteration Recursion
1. In case of Iteration, the statement(s) is 1. In case of Recursion, the statement(s) is
executed repeatedly using Same Memory executed repeatedly by invoking same method
location. within itself and for each recursive call, fresh
memory is required.

2. Iteration executes faster than Recursion. 2. Due to function call overheads, Recursion
runs/executes slower as compared to Iteration.

Base case: The pre-defined condition given in a recursive function that is used to terminate recursive process is
known as Base Case.

Recursive case: Recursive case contains statement(s) to be executed repeatedly.


Example: To find GCD/HCF of two numbers using recursive technique.
Version 1 Version 2
int gcd (int a, int b) int gcd (int a, int b)
{ {
if (a==0) if (a!=0)
{ // Base case { // Recursive
return b; int r=b%a; case
} return gcd(r,a);
else }
{ // Recursive else
int r=b%a; { // Base case
case
return gcd(r,a); return b;
} }
}
}

Finite Recursion: Calling and invoking same function within itself in the presence of Base case is called Finite
Recursion.

Infinite Recursion: Calling and invoking same function within itself in the absence of Base case is called Finite
Recursion.

Finite Recursion Infinite Recursion


void Print(int x) void Print(int x)
{ {
if(x==0) // Base Case System.out.println(x);
{ Print(x-1);
System.out.println(“End reached”); }
}
else
{
System.out.println(x);
Print(x-1);
}
}

Direct Recursion: When a function calls itself within the same function repeatedly, it is called the direct recursion.

Indirect Recursion: Indirect recursion occurs when a function is called not by itself but by another function that it
called (either directly or indirectly). In other words, when a function is mutually called by another function in a
circular manner, the function is called an indirect or mutual recursion function.

Direct Recursion Indirect Recursion


b
// To return a using recursion //To print numbers from 1 to N using indirect
recursion
int power (int a, int b)
{ // Prints n, increments n and calls fun2()
if( b==0) void fun1(int n)
{ {
return 1; if (n <= N)
} {
else System.out.print(n);
{ n++;
return a*power (a, b-1); fun2(n);
} }
} else
{
return;
}
}

// Prints n, increments n and calls fun1()


void fun2(int n)
{
if (n <= N)
{
System.out.print(n);
n++;
fun1(n);
}
else
{
return;
}
}

Tail Recursion: A recursive function is called the tail-recursive if the recursive call is the last statement executes by
the function. After that, there is no statement is left to call the recursive function.

Head (No Tail) Recursion: A function is called the non-tail or head recursive if the recursive call will be the first
statement in the function. It means there should be no statement before the recursive calls.

Tail Recursion Head Recursion


// To print reverse of a number // To print Binary Equivalent of a decimal
number
void Reverse(int num) void Binary(int num)
{ {
if (num !=0) if (num !=0)
{ {
System.out.print(num%10); Binary(num/2);
Reverse(num/10); System.out.print(num%2);
} }
} }

Linear Recursion: A function is called the linear recursive if the function makes a single call to itself at each time the
function runs and grows linearly in proportion to the size of the problem.

Tree (Non-Linear) Recursion: A function is called the tree recursion, in which the function makes more than one call
to itself within the recursive function.

Linear Recursion Non-Linear Recursion


void DecimalToBinary(int n) int fibboTerm (int n)
{ {
if(n!=0) if(n==1)
{ return 0;
int d=n%2; else if(n==2)
DecimalToBinary(n/2); return 1;
System.out.print(d); else
} {
} return fibboTerm(n-1) + fibboTerm(n-2);
}
}
Q: Write a function to find ab using recursive technique. Also show the dry run by taking one example.

Recursive function Dry Run


int power (int a, int b) for a=3 and b=4
{ power(3, 4)
if(b==0)
81
return 1; 3* power(3,3)
else
27
return a*power (a, b-1); 3* power(3,2)
}
9
3* power(3, 1)

3
3* power(3, 0) // base case
1

hence it will return 81

Q: Write a function to find n! using recursive technique. Also show the dry run by taking one example.

Recursive function Dry Run


int fact (int n) for n=5
{ fact(5)
if(n<2)
120
return 1; 5* fact(4)
else
24
return n*fact (n-1); 4* fact(3)
}
6
3*fact (2)

2
2* fact (1) // base case
1

hence it will return 120

Points to remember:
1. A problem that can be solved through iteration can also be solved using recursive technique and vice-versa.

2. Recursion means simply avoid loops.

3. To make a function as recursive function there must be at least one base case and one recursive case.

4. Statement(s) before recursive call executes in FIFO (First In First Out) order whereas statement(s) after recursive
call executes in LIFO (Last In First Out) order.

You might also like