T2 DS Recursion (MConverter - Eu)
T2 DS Recursion (MConverter - Eu)
Recursion
Recursion:
Introduction & definition
Role of stack in recursion
Advantages & disadvantages
Types of recursion
Examples of recursion
Towers of Hanoi game using recursion
Fibonacci Series
What is a Recursion
Definition:
Recursion is a process in which a function calls itself (directly or indirectly),
and the function is called as recursive function.
While writing a recursive function, we should take care of two things to avoid
infinite loop. They are:
• a) Base case: Base case tells when the recursion has to stop. It is also
known as stopping case or terminating condition.
• b) After every call of the recursive function, the control should come closer
to the base case.
Note : Without base case your function will never terminate and stack
overflow problem may arise(run out of memory to hold items in the stack.)
Note : Recursion is one way to decompose a task into smaller subtasks. A
recursive function is one that knows the answer to a specific case; in all other
cases, the function calls itself again
In simple words “Recursion is a process in which the problem is specified in
terms of itself”
Presidency University, Bengaluru
Role of the stack in Recursion:
When the recursive program is executed, the recursive function calls are not
evaluated immediately. They are placed on a stack until the base case/
terminating condition is encountered. After satisfying the terminating
condition the function calls are exe8cuted in reverse order, because the data
structure stack is involved.
Advantages :
• Recursion reduces the length of the code
• Using recursion, certain problems can be solved quite easily. Examples of
such problems are Towers of Hanoi (TOH), Inorder/Preorder/Postorder
Tree Traversals, DFS of Graph, etc.
• Through Recursion one can Solve problems in easy way while its iterative
solution is very big and complex.
Disadvantages :
• Recursive solution is always logical and it is very difficult to trace.(debug
and understand).
• Recursion uses more memory, because the function has to add to the stack
with each recursive call and keep the values there until the call is finished.
• Recursion uses more processor time than iteration.
2. Indirect recursion
If the function f1 calls another function f2 and f2 calls f1 then it is indirect
recursion (or mutual recursion).
Example : add 1 to odd number, subtract 1 from even number program
odd () function handles odd numbers, when odd number is encountered it adds
1 and print that number on the screen.
Even() function handles even numbers, when even number is encountered it
subtracts 1 and print that number on the screen.*/
#include<stdio.h>
void even();
void odd(); // prototypes
int n=1; // global variable
void main()
{
odd();
}
In fibonacci series, current number/ term is the sum of previous two numbers,
for example 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 etc.
The first two numbers of fibonacci series are 0 and 1.
// fibonacci series without recursion// t1 t2 t3
#include<stdio.h> // 0 1 1
void main() // 1 1 2
{ // 1 2 3
int t1=0,t2=1,t3,term,n; // 2 3 5
printf("how many terms \n");
scanf("%d",&n); //10
printf("%d\t%d\t",t1,t2); //printing first two terms, 0 and 1
for(term=3;term<=n;term++)// we have to print from 3rd term
{
t3=t1+t2; // current term is sum of two previous terms
printf(" %d\t",t3);
t1=t2;
t2=t3;
} return; }
Presidency University, Bengaluru
Fibonacci numbers /series using recursion
#include<stdio.h>
int fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return (fibonacci(n-1) + fibonacci(n-2) );
}
void main()
{ int n, i = 0, term;
printf("how many terms \n");
scanf("%d",&n);
printf("fibonacci series\n");
for (term=1;term<= n ;term++ ) // 10 times i=0 1 2 3
{
printf("%d\t", fibonacci(i)); // 0 1 1
i++;
} return ; }
Presidency University, Bengaluru
Towers of Hanoi
Towers of Hanoi is a well-known children’s game, played with three poles (known as
left, center and right poles or towers) and number of different sized disks. (that is
disks with different radii).
Initially, the disks are stacked on the left most pole in the order of decreasing order,
ie., the largest disk on the bottom and smallest disk on the top. (see the figure)
The objective of the game is to transfer the disks from the leftmost pole to the
rightmost pole.
void main()
{
int n;
printf("how many disks \n");
scanf("%d",&n);