Unit - 4 Recursion
Unit - 4 Recursion
Introduction:
Recursion is defined as defining anything in terms of itself. Recursion is used to solve the problem involving
iteration (multiple expectations) in reserve order loop structure helps to carry out certain tasks a number of times
fill the given condition is satisfied. But how to make a complete function execution repeatedly in terms of itself.
Recursion is an alternative to iteration the technique is useful both for definition of mathematical function and
data structure for example: factorial of number, fibbonices number, Tower of Hanoi, cheese etc.
Need of Recursion
Recursion is a 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. 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.
2) Indirect Recursion
When a function is mutually called by another function in a circular manner, the function is called
an indirect recursion function.
Structure of the indirect recursion
void indirectRecFun1()
{
// Some code...
indirectRecFun2();
// Some code...
}
void indirectRecFun2()
{
// Some code...
indirectRecFun1();
// Some code...
}
int factorial(int n)
{
if(n==0)
return 1;
else
return n*factorial(n-1);
}
void main()
{
int num;
clrscr();
printf("Enter the number to be insert: ");
scanf("%d",&num);
printf("\n %d",factorial(num));
getch();
}
//Fibonacci Series
//Using Recursive Function
#include<stdio.h>
#include<conio.h>
int fibo(int n)
{
if (n==1||n==2)
return 1;
else
return (fibo(n-1)+fibo(n-2));
}
for(i=0;i<num;i++)
{
printf("\n %d",fibo(num));
}
getch();
}
Basic Recursion is the process of calling a In iteration, there is a repeated execution of the
function itself within its own code. set of instructions. In Iteration, loops are used
to execute the set of instructions repetitively
until the condition is false.
Termination The termination condition is defined Here, the termination condition is defined in
within the recursive function. the definition of the loop.
Code size The code size in recursion is smaller The code size in iteration is larger than the
than the code size in iteration. code size in recursion.
Infinite If the recursive function does not meet Iteration will be infinite, if the control condition
to a termination condition, it leads to of the iteration statement never becomes false.
an infinite recursion. There is a chance On infinite loop, it repeatedly used CPU cycles.
of system crash in infinite recursion.
Usage Recursion is generally used where It is used when we have to balance the time
there is no issue of time complexity, complexity against a large code size.
and code size requires being small.
Time It has high time complexity. The time complexity in iteration is relatively
complexity lower. We can calculate its time complexity by
finding the no. of cycles being repeated in a
loop.
Stack It has to update and maintain the stack. There is no utilization of stack.
Memory It uses more memory as compared to It uses less memory as compared to recursion.
iteration.
Disadvantages of recursion
1. Not all problems have recursion solution.
2. Recursion problems are not efficient for memory utilization and execution speed of problems.
In data structure like linked list, stacks, trees, quicksort, recursion is more handly. In certain problems like
Tower of Hanoi, recursion may be the only solution.
It is a game problem. There will be three different sized disks. Each disk has a hole in the center. So that it can
be stacked on any of the towers. Call three towers as x,y and z. At the beginning of the games, the disks are
stacked on the x towers, that is the largest sized disk on the bottom and the smallest sized disk on top. For n=3
disks.
Recursive Definition
It is clear that the movement of disk is from x tower to the y tower. And the z tower is used for intermediate
assuming there is only one disk its movement is straightforward that is:
➢ Move the disk from x tower to y tower. Now consider n = 2 disks. The transferring of these two disks to
the y tower is done as follows.
➢ Move the first disk from the x tower to the z tower. Next, move the disk stacked on the z tower to the y
tower. Since it holds true for the number of disks, n = 1 and n = 2 therefore we can generalized this and
prove that it holds for all disks.
Assuming that initially all disk are stored on the x tower and transferring all these to the y tower can be
recursive defined as follows:
i) Move the top (n-1) disk from the x tower to the z tower.
ii) Move the nth disk to z tower.
iii) Move the (n-1) disk stacked on the z tower to the y tower.
The solution to find the greatest common divisor (GCD) for the given two numbers by using the recursive
function is as follows −
Algorithm
Refer an algorithm given below to find the greatest common divisor (GCD) for the given two numbers by using
the recursive function.
Step 1 − Define the recursive function.
Step 2 − Read the two integers a and b.
[Data Structure and Algorithm ©Raj Kumar Shrestha] Page 7
Step 3 − Call recursive function.
a. if i>j
b. then return the function with parameters i,j
c. if i==0
d. then return j
e. else return the function with parameters i,j%i.
//program to implement gcd using recursion using c.
#include <stdio.h>
#include<conio.h>
int gcd(int n1, int n2);
int main() {
int n1, n2, div;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
div=gcd(n1,n2);
printf("G.C.D of %d and %d is %d.", n1, n2, div);
getch();
return 0;
}
Search Tree:
In computer science, a search tree is data structure in whose nodes data values can be stored from some ordered
set, which is such that in an in-order traversal of the tree the nodes are visited in ascending order of the stored
values. Each sub-tree of a search tree is by itself again a search tree.
A search tree is a data structure used in computer programming to contain and organizes a list of data. Each
search tree is comprised of an ordered set of nodes. These nodes can be connected to zero or more other nodes.
The individual nodes contain some data as well as links to any other nodes. The data that is contained in the
nodes of the tree is very often ordered in some way to allow efficient algorithms to search for, insert and
remove nodes with ease.
The nodes of a search tree are described with following terms. The top of tree, where the first node is located, is
called the root. If a node contains links to sub-nodes, that node is known as a parent. Nodes that are beneath the
parent are called children, and any node that has no child nodes is called a leaf. So, a root node is identified
because it does not have a parent, and leaf nodes will have no children.
A program is able to move through a tree searching for data by starting at a particular node, performing a
conditional check and then moving to the next logical node if the required data is not present. Depending on the
data structure used, this search can take a variable amount of time. If the search tree is organized during the
process of adding and removing nodes, the search can be very fast. When a tree is assembled randomly, is
unsorted or allows multiple parents, the search trees can take a very long time.
[Data Structure and Algorithm ©Raj Kumar Shrestha] Page 8
Advantages of recursion
• The code may be easier to write.
• To solve such problems which are naturally recursive such as tower of Hanoi.
• Reduce unnecessary calling of function.
• Extremely useful when applying the same solution.
• Recursion reduce the length of code.
• It is very useful in solving the data structure problem.
• Stacks evolutions and infix, prefix, postfix evaluations etc.
Disadvantages of recursion
• Recursive functions are generally slower than non-recursive function.
• It may require a lot of memory space to hold intermediate results on the system stacks.
• Hard to analyze or understand the code.
• It is not more efficient in terms of space and time complexity.
• The computer may run out of memory if the recursive calls are not properly checked.