0% found this document useful (0 votes)
3 views

Recursion Stack Applications

Recursion Stack Applications

Uploaded by

rishabhjangid33
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Recursion Stack Applications

Recursion Stack Applications

Uploaded by

rishabhjangid33
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Recursion:

Some computer programming languages allow a module or function to call itself. This
technique is known as recursion. In recursion, a function α either calls itself directly or calls

i
lin
a function β that in turn calls the original function α. The function α is called recursive
function.

a
Sh
Example − a function that calls another

ith
Example − a function calling function which in turn calls it again.
itself.
W int function1(int value1) {
rn
int function(int value) { if(value1 < 1)
a
if(value < 1) return;
Le

return; function2(value1 - 1);


function(value - 1); printf("%d ",value1);
}
printf("%d ",value); int function2(int value2) {
} function1(value2);
}
1
Properties
A recursive function can go infinite like a loop. To avoid infinite
running of recursive function, there are two properties that a

i
recursive function must have −

lin
Base criteria − There must be at least one base criteria or condition,

a
Sh
such that, when this condition is met the function stops calling itself
recursively.

ith
Progressive approach − The recursive calls should progress in such a
W
way that each time a recursive call is made it comes closer to the
rn
base criteria.
a
Le

2
Let us consider a problem that a programmer have to determine the sum of first n natural
numbers, there are several ways of doing that but the simplest approach is simply add the
numbers starting from 1 to n. So the function simply looks like,

i
lin
approach(1) – Simply adding one

a
by one

Sh
f(n) = 1 + 2 + 3 +……..+ n

ith
approach(2) – Recursive adding
f(n) = 1 n=1 W
rn
f(n) = n + f(n-1) n>1
a
Le

3
What is base condition in recursion?
Solution is Provided for Base condition and bigger problem is divided into
smaller problems

i
lin
int fact(int n)

a
{

Sh
if (n < = 1) // base case
return 1;

ith
else

}
return n*fact(n-1);
W
a rn
Le

4
1. “Recursion" is technique of solving any problem by calling same function again and
again until some breaking (base) condition where recursion stops and it starts
calculating the solution from there on. For eg. calculating factorial of a given number

i
lin
2. Thus in recursion last function called needs to be completed first.

a
Sh
3. Now Stack is a LIFO data structure i.e. ( Last In First Out) and hence it is used to
implement recursion.

ith
4. The High level Programming languages, such as Pascal , C etc. that provides support

W
for recursion use stack for book keeping.
rn
5. In each recursive call, there is need to save the
a
Le

i. current values of parameters,


ii. local variables and
iii. the return address (the address where the control has to return from the call).

6. Also, as a function calls to another function, first its arguments, then the return
address and finally space for local variables is pushed onto the stack.
5
Why Stack Overflow error occurs in recursion?
Deep Recursion may cause system stack overflow. There is no space in system stack for
making a new function call and program may crash.
Or

i
If the base case is not reached or not defined, then the stack overflow problem may arise.

a lin
Let us take an example to understand this.

Sh
int fact(int n)

ith
{
// wrong base case (it may cause stack overflow).
if (n == 100)
return 1; W
a rn
else
Le

return n*fact(n-1);
}

If fact(10) is called, it will call fact(9), fact(8), fact(7) and so on but the number will never
reach 100. So, the base case is not reached. If the memory is exhausted by these
functions on the stack, it will cause a stack overflow error. .
6
Types of Recursion

1.Direct and Indirect Recursion

i
lin
2.Head Recursion

a
Sh
3.Tail Recursion

ith
4.Excessive Recursion
5.Nested Recursion W
a rn
Le

7
What is the difference between direct and indirect recursion?
A function fun is called direct recursive if it calls the same function fun. A function fun is
called indirect recursive if it calls another function say fun_new and fun_new calls fun
directly or indirectly.

i
// An example of direct recursion // An example of indirect recursion

lin
void indirectRecFun1()

a
void directRecFun() {

Sh
{ // Some code...
// Some code....

ith
indirectRecFun2();
directRecFun();

// Some code...
W // Some code...
rn
}
} void indirectRecFun2()
a

{
Le

// Some code...

indirectRecFun1();

// Some code...
} 8
Head Recursion: If the function call is the first statement in the function then it is called
Head Recursion.

Example:

i
// C program showing Head Recursion

lin
#include <stdio.h>
Void Fun()

a
void fun(int n)
{

Sh
{
if(<condition>) if (n > 0) {
{

ith
Fun(); // First statement in the function

}
<Statements>
W fun(n - 1);
rn
} printf("%d ", n);
a

}
Le

}
int main()
{
int x = 3;
fun(x);
return 0;
9
}
Tail Recursion: A recursive function is tail recursive when recursive call is the last
thing executed by the function.

For example the following C++ function print() is tail recursive.

i
lin
// An example of tail recursive function

a
void print(int n)

Sh
{
if (n < 0) return;

ith
cout << " " << n;

W
// The last executed statement is
rn
recursive call
a
print(n-1);
Le

10
Nested Recursion: In this recursion, a recursive function will pass the parameter as a
recursive call. That means “recursion inside recursion”.
// C program to show Nested Recursion
#include <stdio.h>

i
int fun(int n)

lin
{

a
if (n > 100)

Sh
return n - 10;

ith
// A recursive function passing parameter as a recursive call or recursion
// inside the recursion

}
return fun(fun(n + 11));
W
rn
int main()
a

{
Le

int r;
r = fun(95);
printf("%d\n", r);
return 0;
}
11
int fun(int n)
{
if (n > 100)
return n - 10;

i
lin
return fun(fun(n + 11));
}

a
Sh
ith
W
rn
a
Le

12
Excessive Recursion: If a recursive function calling itself more than one time for same
parameter than it is called as excessive recursion. Usually tree recursion are excessive
recursion.

i
a lin
Sh
unsigned long Fib(unsigned long n)
{

ith
if (n < 2)

W
return n;
rn
else
a
return Fib(n-2) + Fib(n-1);
Le

13
Advantages of recursive programming:

1. Recursion can reduce time complexity.

i
lin
2. Recursion adds clarity and reduces the time needed to write and debug code.

a
3. Recursion is better at tree traversal.

Sh
ith
W
a rn
Le

14
Disadvantages of recursive programming:

1. Recursion uses more memory: The recursive program has greater space requirements
than iterative program as all functions will remain in the stack until the base case is

i
lin
reached. It also has greater time requirements because of function calls and returns
overhead.

a
2. Recursion can be slow.

Sh
ith
W
a rn
Le

15

You might also like