Documentos de Académico
Documentos de Profesional
Documentos de Cultura
frame will be out of limit because function call will be infinite times. So a
termination condition is mandatory to a recursion.
In C++, Recursion can be divided into two types:
(a) Run- Time Recursion: Normal as in C
(
Compile- Time Recursion: By using Template
Linear Recursion
Binary Recursion
Tail Recursion
Mutual Recursion
Nested Recursion
1. Linear Recursion: This recursion is the most commonly used. In this recursion
a function call itself in a simple manner and by termination condition it
terminates. This process called 'Winding' and when it returns to caller that is
called 'Un-Winding'. Termination condition also known as Base condition.
Example: Factorial calculation by linear recursion
Run-Time Version
01
02
03
04
05
06
07
08
09
10
11
int Fact(long n)
{
if(0>n)
return -1;
if(0 == n)
return 1;
else
{
return ( n* Fact(n-1));
}
Winding Process:
1*1
2*1
3*2*1
4*3*2*1
5*4*3*2*1
6*5*4*3*2*1
Compile-Time Version
01
02
03
04
05
06
07
08
09
10
struct Fact<0>
{
enum
{
factVal = 1
};
};
11
12
13
14
15
16
17
18
19
int FibNum(int n)
{
// Base conditions
if (n < 1)
05
return -1;
06
if (1 == n || 2 == n)
07
return 1;
08
09
10
11
binary
12 }
// Base Conditions
template<>
struct FibNum<2>
{
enum { val = 1 };
};
template <>
struct FibNum<1>
{
enum { val = 1 };
};
// Recursive call by Binary Method
template <int n>
15
struct FibNum
16
{
17
enum { val= FibNum<n - 1>::val + FibNum<n - 2>::val };
18 };
3. Tail Recursion: In this method, recursive function is called at the last. So it's
more efficient than linear recursion method. Means you can say termination
point will come(100%) only you have to put that condition.
Example: Fibonacci number
Run Time Version Code:
01
02
03
04
05
06
07
08
09
10
11
// Base Condition
return y;
}
else
{
return FibNum(n-1, y, x+y);
}
}
4. Mutual Recursion: Functions calling each other. Let's say FunA calling FunB
and FunB calling FunA recursively. This is not actually not recursive but it's
doing same as recursive. So you can say Programming languages which are not
supporting recursive calls, mutual recursion can be applied there to fulfill the
requirement of recursion. Base condition can be applied to any into one or more
than one or all functions.
Example: To find Even Or Odd number
Run Time Version Code:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
bool IsOddNumber(int n)
{
// Base or Termination Condition
if (0 == n)
return 0;
else
// Recursive call by Mutual Method
return IsEvenNumber(n - 1);
}
bool IsEvenNumber(int n)
{
// Base or Termination Condition
if (0 == n)
return 1;
else
// Recursive call by Mutual Method
return IsOddNumber(n - 1);
}
03
04
05
06
07
08
09
10
struct IsOddNumber<0>
{
enum
{
val = 0
};
};
11
12
13
struct IsEvenNumber<0>
{
enum
template <>
14
15
16
17
18
19
20
{
val = 1
};
};
// Recursive calls by Mutual Method
21
22
23
24
25
26
27
28
29
struct IsOddNumber
{
enum
{
val = n == 0 ? 0 : IsEvenNumber<n - 1>::val
};
};
30
31
32
33
34
35
36
37
38
struct IsEvenNumber
{
enum
{
val = n == 0 ? 1 : IsOddNumber<n - 1>::val
};
};
3. Nested Recursion: It's very different than all recursions. All recursion can be
converted to iterative (loop) except nested recursion. You can understand this
recursion by example of Ackermann function.
Example: Ackermann function
Run Time Version Code:
01
02
03
04
05
06
07
08
09
10
11
12
||
y < 0)
{
return -1;
}
13
14
15
16
17
18
19
20
21
22
23
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26