You are on page 1of 6

M.

SANTOSH KUMAR DATASTRUCTURES USING C++


Unit – II

1. What is Recursion? Explain its terminology and variants (types of recursion).

Recursion:
1. A set of instructions that performs logical operations, which could be very complex and numerous in
number can be grouped together as functions.
2. Functions may call themselves or other functions and the called function in turn may call the calling
function this process is called recursion and such functions are called recursive functions.
3. A function is called using a function name and its parameters.
4. When a function calls itself directly or indirectly it is called a recursive call.
5. To find factorial of a number ‘n’
n! = 1 x 2 x 3 x 4 x . . . . . . x n (or) n! = n x (n-1) x . . . . . 1
Algorithm:
1. Start
2. Let ‘n’ is the number to find the factorial, fact = 1
3. While (n>1)
begin
fact = fact x n
n = n -1
end
4. Stop
 It is possible to give a recursive definition for factorial, the mathematical function that can be defined
n! = n x (n-1)!
 The recursive definition of factorial
a) if n = 0 or 1, factorial of n = 1
b) otherwise factorial of n is n x factorial (n-1)
long int factorial (int n)
{
if(n==0 || n==)
return 1;
else
return n x factorial (n-1);
}
 In the second return statement, the function calls itself, a recursive function includes end condition.
 The recursive function stops when the value of n is 0 or 1, it the value of ‘n’ is less than 1 the recursive
1
M. SANTOSH KUMAR DATASTRUCTURES USING C++
function will execute infinitely. Therefore, the condition is replaced
long int factorial (int n)
{
if(n<0)
return;
if(n==0 || n==)
return 1;
else
return n x factorial (n-1);
}
Terminology:
1. Recursive depth:
1. The number of times a function calls itself is called as the recursive depth.
2. Winding and unwinding:
1. Winding is the process when the function calls itself, pushes in to the stack.
2. Unwinding is the process when the function pops value from the stack after the end condition.
3. Stack overflow:
1. Each time the function calls itself, it stores one or more values on to the stack. Stack can accommodate
limited amount of memory.
4. Endless recursion:
1. The function with high recursive depth may crash because of non-availability of memory this condition is
called as “stack overflow”.
Types of recursion (variants of recursion):
1. The recursive functions are classified into
a) Direct recursion
b) Indirect recursion
c) Linear recursion
d) Tree recursion
e) Tail recursion
2. Recursive function has the following behavior
i) A function calls itself (direct recursion)
ii) A function calls another function which in turn calls the caller function (indirect recursion)
iii) The function call is part of same processing instruction that makes a recursive call.
1. Direct recursion:
1. A recursive function is said to be direct recursive, if it calls itself (calling and called functions are same)
2
M. SANTOSH KUMAR DATASTRUCTURES USING C++
if(n<0)
return;
if(n==0 || n==)
return 1;
else
return n x factorial (n-1);
2. Indirect recursion:
1. A function is said to be indirectly recursive, if it calls another function, which in turn calls it
long int factorial (int n)
{
if(n<0)
return;
if(n==0 || n==)
return 1;
else
return n x dummy (n-1);
}
int dummy(int n)
{
factorial (n);
}
3. Linear recursion:
1. A linear recursive function is a function that only makes a single call to itself each time the function runs.
if(n<0)
return;
if(n==0 || n==)
return 1;
else
return n x factorial (n-1);
4. Tree recursion:
1. In a recursive function if there is another recursive call in the set of operations to be completed after the
recursion is over.
int Fibonacci (int n)
{
if(n<=2)
3
M. SANTOSH KUMAR DATASTRUCTURES USING C++
return 1;
else
return Fibonacci(n-1) + Fibonacci(n-2);
}
5. Tail recursion:
1. A recursive function is said to be tail recursive if there are no pending operations to be performed on
return from a recursive call.
if(n<0)
return;
if(n==0 || n==)
return 1;
else
return n x factorial (n-1);

2. What is recurrence?

Recurrence:
1. A recurrence is a well-defined mathematical function, it is applied on itself.
2. The Fibonacci series 0, 1, 1, 2, 3, 5, 8, 13, . . . . . . . . . .
3. The first two numbers of the series are 0 and 1, to get succeeding number, it is the sum of preceding two
numbers.
4. To define a function f(n) that returns Fibonacci number, the base case that can be represented by
f(1) = 0 and f(2) = 1
To get nth Fibonacci number, we can get it by adding (n-1) and (n-2)
f(n) = f(n-1) + f(n-2)

3. Explain differences between Recursion and Iteration (Recursion vs Iteration)

Basic for
Recursion Iteration
comparison
1. Basic The statement in a body of function Allows the set of instructions to be
calls the function it self executed repeatedly

2. Format In recursive function base case and It includes initialization, condition,


end condition is specified updating the control variable (++ or --)
and execution of statements within the
loop

4
M. SANTOSH KUMAR DATASTRUCTURES USING C++
3. Termination A conditional statement is included The iteration statement is repeatedly
in the body of the function to force executed until a certain condition is
the function to return without reached.
recursion call being executed

4. Stack The stack is used to store the set of Does not use stack
local variables and parameters each
time the function is called.

5. Speed Executed in slow manner Executed in speed manner

6. overhead Recursion possesses the overhead of No overhead of repeated function calls


repeated function calls

7. Size of code Recursion reduces the size of the Iteration makes the code longer
code

Tower of Hanoi:
Writing recursive code:
Write the function header, it will provides the task of the function.
Decompose the problem into sub problems. Identify clearly the non-recursive case of the problem (it must
be small). The function is evaluated with the non-recursive code which is called as base case or end
condition.
Write recursive code to solve sub problems which is similar to original.
The end condition is stopping portion of the recursive algorithm.
Introduction:
Tower of Hanoi is a mathematical puzzle which consists of three towers (pegs) and more than one disc.
The discs are in different sizes from smaller to bigger the top most disc is smaller and the bottom most disc
is bigger.
Rules:
1) Only one disc can be moved among the towers at any given time.
2) Only top disc can be removed.
3) No large disc can sit over a small disc.
To solve tower of Hanoi the minimum number of steps 2n – 1.
for example 3 discs the value will be 7.
Algorithm:
1. To implement an algorithm for Tower of Hanoi, we mark three towers with the name source, destination
5
M. SANTOSH KUMAR DATASTRUCTURES USING C++
and auxiliary.
2. If the source tower has only one disc, disc can be moved from source to destination.
3. If the source tower contains two discs
a. Move smaller disc from source to auxiliary.
b. Move the larger disc from source to destination.
c. Move the smallest disc from auxiliary to destination
4. The tower of Hanoi algorithm divides the discs into two parts.
a. The largest disc is nth disc
b. The remaining smallest discs are considered as n-1 discs.
5. Tower of Hanoi algorithm contains the following steps
a. move n-1 discs from source to auxiliary
b. move nth disc from source to destination
c. move n-1 discs from auxiliary to destination
For example:
The source tower contains 3 discs the following are the steps.
a. move the disc 1 (smaller) from source to destination
b. move the disc 2 (medium) from source to auxiliary
c. move the disc 1 from destination to auxiliary
d. move the disc 3 (larger) from source to destination
e. move the disc1 from auxiliary to source
f. move the disc 2 from auxiliary to destination
g. move the disc 1 from source to destination

You might also like