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

Data Structures

Data Structures

Uploaded by

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

Data Structures

Data Structures

Uploaded by

swarna Latha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

UNIT II

Introduction to Recursion
What is Recursion?
The 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. Examples of such problems are Towers of
Hanoi (TOH), Inorder/Preorder/Postorder Tree Traversals, DFS of Graph, etc. A
recursive function solves a particular problem by calling a copy of itself and solving
smaller subproblems of the original problems. Many more recursive calls can be
generated as and when required. It is essential to know that we should provide a certain
case in order to terminate this recursion process. So we can say that every time the
function calls itself with a simpler version of the original problem.
Need of Recursion
Recursion is an amazing technique with the help of which we can reduce the length of
our code and make it easier to read and write. It has certain advantages over the
iteration technique which will be discussed later. A task that can be defined with its
similar subtask, recursion is one of the best solutions for it. For example; The Factorial
of a number.
Properties of Recursion:
 Performing the same operations multiple times with different inputs.
 In every step, we try smaller inputs to make the problem smaller.
 Base condition is needed to stop the recursion otherwise infinite loop will occur.
Recursion is defined as a process which calls itself directly or indirectly and the
corresponding function is called a recursive function.
Properties of Recursion:
Recursion has some important properties. Some of which are mentioned below:
 The primary property of recursion is the ability to solve a problem by breaking it
down into smaller sub-problems, each of which can be solved in the same way.
 A recursive function must have a base case or stopping criteria to avoid infinite
recursion.
 Recursion involves calling the same function within itself, which leads to a call
stack.
 Recursive functions may be less efficient than iterative solutions in terms of
memory and performance.
Types of Recursion:
1. Direct recursion: When a function is called within itself directly it is called direct
recursion. This can be further categorised into four types:
 Tail recursion,
 Head recursion,
 Tree recursion and
 Nested recursion.
2. Indirect recursion: Indirect recursion occurs when a function calls another
function that eventually calls the original function and it forms a cycle.

Applications of Recursion:
Recursion is used in many fields of computer science and mathematics, which includes:
 Searching and sorting algorithms: Recursive algorithms are used to search and
sort data structures like trees and graphs.
 Mathematical calculations: Recursive algorithms are used to solve problems such
as factorial, Fibonacci sequence, etc.
 Compiler design: Recursion is used in the design of compilers to parse and
analyze programming languages.
 Graphics: many computer graphics algorithms, such as fractals and the
Mandelbrot set, use recursion to generate complex patterns.
 Artificial intelligence: recursive neural networks are used in natural language
processing, computer vision, and other AI applications.
Advantages of Recursion:
 Recursion can simplify complex problems by breaking them down into smaller,
more manageable pieces.
 Recursive code can be more readable and easier to understand than iterative
code.
 Recursion is essential for some algorithms and data structures.
 Also with recursion, we can reduce the length of code and become more readable
and understandable to the user/ programmer.
Disadvantages of Recursion:
 Recursion can be less efficient than iterative solutions in terms of memory and
performance.
 Recursive functions can be more challenging to debug and understand than
iterative solutions.
 Recursion can lead to stack overflow errors if the recursion depth is too high.
Difference between Recursion and Iteration

A program is called recursive when an entity calls itself. A program is called


iterative when there is a loop (or repetition).

// C++ program to find factorial of given number


#include<bits/stdc++.h>
using namespace std;

// ----- Recursion -----


// method to find factorial of given number
int factorialUsingRecursion(int n)
{
if (n == 0)
return 1;

// recursion call
return n * factorialUsingRecursion(n - 1);
}

// ----- Iteration -----


// Method to find the factorial of a given number
int factorialUsingIteration(int n)
{
int res = 1, i;

// using iteration
for (i = 2; i <= n; i++)
res *= i;

return res;
}
// Driver method
int main()
{
int num = 5;
cout << "Factorial of " << num <<
" using Recursion is: " <<
factorialUsingRecursion(5) << endl;

cout << "Factorial of " << num <<


" using Iteration is: " <<
factorialUsingIteration(5);

return 0;
}

// This code is contributed by mits

Difference between Iteration and Recursion


The following table lists the major differences between iteration and recursion:

Property Recursion Iteration

A set of instructions repeatedly


Function calls itself.
Definition executed.

Application For functions. For loops.

Through base case, where When the termination condition for


Termination there will be no function call. the iterator ceases to be satisfied.
Property Recursion Iteration

Used when code size needs to Used when time complexity needs to
be small, and time complexity be balanced against an expanded
Usage is not an issue. code size.

Code Size Smaller code size Larger Code Size.

Relatively lower time


Very high(generally
Time complexity(generally polynomial-
exponential) time complexity.
Complexity logarithmic).

Space The space complexity is higher


Space complexity is lower.
Complexity than iterations.

Here the stack is used to store


local variables when the Stack is not used.
Stack function is called.

Execution is slow since it has


Normally, it is faster than recursion
the overhead of maintaining
as it doesn’t utilize the stack.
Speed and updating the stack.

Recursion uses more memory Iteration uses less memory as


Memory as compared to iteration. compared to recursion.

Possesses overhead of repeated No overhead as there are no function


Overhead function calls. calls in iteration.
Property Recursion Iteration

If the recursive function does


If the control condition of the
not meet to a termination
iteration statement never becomes
condition or the base case is
false or the control variable does not
not defined or is never reached
reach the termination value, then it
then it leads to a stack
will cause infinite loop. On the
overflow error and there is a
infinite loop, it uses the CPU cycles
Infinite chance that the an system may
again and again.
Repetition crash in infinite recursion.

You might also like