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

R Recursion (1)

Uploaded by

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

R Recursion (1)

Uploaded by

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

Recursion

 Recursion in the data structure can be


defined as a method through which
problems are broken down into smaller
sub-problems to find a solution. This is
Recursion done by replicating the function on a
smaller scale, making it call itself and
then combining them together to solve
problems.
 Recursion provides a natural approach
in declaring many algorithms and is
very valuable in functional
programming. Developers can
Recursion effectively use recursion instead of
loop functions such as “for loop” or
“while loop”.
 In computer science, recursion is a common
phenomenon used to tackle complex problems by
breaking them down into simpler ones. It allows
functions to call themselves, breaking down
complex tasks into more straightforward,
manageable sub-problems.
 Recursion is particularly useful when working with
Need of data structures like trees, graphs, and linked lists,
Recursion where recursive algorithms can traverse and
manipulate the elements effectively. It is also
essential for searching, sorting, and producing
permutations or combinations. Recursion makes
code easier to comprehend and maintain, allowing
for modular and reusable programming.
 A key feature of a recursive algorithm
is that it calls itself with a smaller
version of the problem as its input. The
What is a algorithm uses a base case to
terminate the recursion when the
Recursive problem is small enough that it can be
Algorithm? solved directly without further
recursion.
 Step 1- Start with defining a recursive function along with
all necessary parameters.

 Step 2- Determine a base condition. In that base


condition, either it will return a specific value or end the
recursive function.

Steps for implementing  Step 3- If the base case condition is not met, it will break
recursion in a function down the large complex problem into subproblems to
are as follows: reduce its size.

 Step 4- The same function recursively calls itself. If there


are multiple recursive calls made, then they are combined
to solve the original problem.

 Step 5- Defining the termination condition is essential so


that it does not create an infinite recursion.
 A recursive function can go infinite like a
loop. To avoid infinite running of recursive
function, there are two properties that a
recursive function must have −
• Base criteria − There must be at least one
base criteria or condition, such that, when

Properties this condition is met the function stops


calling itself recursively.
• Progressive approach − The recursive
calls should progress in such a way that each
time a recursive call is made it comes closer
to the base criteria.
 The following are two procedures that each calculate n
factorial.

 FACTORIAL(FACT, N)

Factorial  This procedure calculates N! and returns the value in


the variable FACT.
without
Recursion  1. If N = 0, then: Set FACT := 1, and Return.
 2. Set FACT := 1. [Initializes FACT for loop.]
 3. Repeat for K = 1 to N.
 Set FACT := K * FACT.
 4. [End of loop.]
 Return.
int main()
{
int f(int);
int N = 5;
cout<<f(N);
}
int f(int N)
{

Factorial int FACT = 1;

without if (N == 0)

Recursion {
return 1;
}
for (int K = 1; K <= N; K++)
{
FACT = FACT*K;
}
return FACT;
}
 FACTORIAL(FACT, N)
 This procedure calculates N! and
returns the value in the variable FACT.

With  1. If N = 0, then: Set FACT := 1, and


Return.
Recursion
 2. Call FACTORIAL(FACT, N - 1).
 3. Set FACT := N * FACT.
 4. Return.
int main()
{
int fun(int);
cout<<fun(5);

Printing }

Numbers
int fun(int x)
{
if(x<=0)
return 1;
else
fun(x-1);
cout<<x;
}
int main()
{
int g(int);
cout<<g(5);
}
int g(int n)
With {

Recursion int f;
if(n==1)
return 1;
else
f=n+g(n-1);
return(f);
int main()
{
int fact(int n);
int f=fact(5);
cout<<f;
}
Factorial int fact(int n)
{
if(n==0||n==1)
return 1;
else
return(n*fact(n-1));
}
 FIBONACCI(FIB, N)
This procedure calculates the NN-th Fibonacci number
and returns the value in the variable FIB.
1.If NN = 0:
Set FIB := 0, and Return.
2.If NN = 1:
Fibonacci Set FIB := 1, and Return.

Series using 3.Call FIBONACCI(FIB1, N - 1)


[Recursively calculates the (N−1)(N−1)-th Fibonacci
recursion number and stores it in FIB1.]
4.Call FIBONACCI(FIB2, N - 2)
[Recursively calculates the (N−2)(N−2)-th Fibonacci
number and stores it in FIB2.]
5.Set FIB := FIB1 + FIB2
[Adds the two previously computed Fibonacci
numbers to get the NN-th Fibonacci number.]
6.Return.

You might also like