Data Structures
Data Structures
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
// recursion call
return n * factorialUsingRecursion(n - 1);
}
// 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;
return 0;
}
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.