Types of Recursion01

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 35

Types Of Recursion

Expand Post »
Detail About Recursion and its Type

Here I am going to give a detail about Recursion in C++.


Definition: Recursion is the process where a function is called itself but stack 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
(b) Compile- Time Recursion: By using Template

Each of these can be also divided into following types:

1. Linear Recursion
2. Binary Recursion
3. Tail Recursion
4. Mutual Recursion
5. 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

C++ Syntax (Toggle Plain Text)

1. int Fact(long n)
2. {
3. if(0>n)
4. return -1;
5. if(0 == n)
6. return 1;
7. else
8. {
9. return ( n* Fact(n-1));
10. }
11. }
Winding Process:

Function called Function return

Fact(6) 6*Fact(5)
Fact(5) 5*Fact(4)
Fact(4) 4*Fact(3)
Fact(3) 3* Fact(2)
Fact(2) 2* Fact(1)
Fact(1) 1* Fact(0)
Terminating Point
Fact(0) 1

Unwinding Process

Fact(1) 1*1
Fact(2) 2*1
Fact(3) 3*2*1
Fact(4) 4*3*2*1
Fact(5) 5*4*3*2*1
Fact(6) 6*5*4*3*2*1

Compile-Time Version

C++ Syntax (Toggle Plain Text)

1. // template for Base Condition


2. template <>
3. struct Fact<0>
4. {
5. enum
6. {
7. factVal = 1
8. };
9. };
10.
11. template <int n>
12. struct Fact
13. {
14. // Recursion call by linear method
15. enum
16. {
17. value = n * Fact<n - 1>::factVal
18. };
19. };
To test it how it's working at compile time, just call
cout << Fact<-1>::factVal ;
And compile it then compiler error will come, because no template for -1.

2. Binary Recursion: Binary Recursion is a process where function is called twice at a time
inplace of once at a time. Mostly it's using in data structure like operations for tree as traversal,
finding height, merging, etc.

Example: Fibonacci number

Run Time Version Code:


C++ Syntax (Toggle Plain Text)

1. int FibNum(int n)
2. {
3. // Base conditions
4. if (n < 1)
5. return -1;
6. if (1 == n || 2 == n)
7. return 1;
8.
9. // Recursive call by Binary Method
10. return FibNum(n - 1) + FibNum(n - 2); // At a time two
recursive function called so
11.
// binary
12. }

Compile Time Version Code


C++ Syntax (Toggle Plain Text)

1. // Base Conditions
2. template<>
3. struct FibNum<2>
4. {
5. enum { val = 1 };
6. };
7. template <>
8. struct FibNum<1>
9. {
10. enum { val = 1 };
11. };
12.
13. // Recursive call by Binary Method
14. 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:


C++ Syntax (Toggle Plain Text)

1. int FibNum(int n, int x, int y)


2. {
3. if (1 == n) // Base Condition
4. {
5. return y;
6. }
7. else // Recursive call by Tail method
8. {
9. return FibNum(n-1, y, x+y);
10. }
11. }
Compile Time Version Code

C++ Syntax (Toggle Plain Text)

1. template <int n, int x, int y>


2. struct FibNum
3. {
4. // Recursive call By tail method
5. enum
6. {
7. val = FibNum<n-1, y, (x+y)>::val
8. };
9. };
10.
11. // Base Condition or Termination
12. template<int x, int y>
13. struct FibNum<1, x, y>
14. {
15. enum
16. {
17. val = y
18. };
19. };
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:


C++ Syntax (Toggle Plain Text)

1. bool IsOddNumber(int n)
2. {
3. // Base or Termination Condition
4. if (0 == n)
5. return 0;
6. else
7. // Recursive call by Mutual Method
8. return IsEvenNumber(n - 1);
9. }
10.
11. bool IsEvenNumber(int n)
12. {
13. // Base or Termination Condition
14. if (0 == n)
15. return 1;
16. else
17. // Recursive call by Mutual Method
18. return IsOddNumber(n - 1);
19. }

Compile Time Version Code

C++ Syntax (Toggle Plain Text)

1.
2. // Base Or Termination Conditions
3. template <>
4. struct IsOddNumber<0>
5. {
6. enum
7. {
8. val = 0
9. };
10. };
11. template <>
12. struct IsEvenNumber<0>
13. {
14. enum
15. {
16. val = 1
17. };
18. };
19.
20. // Recursive calls by Mutual Method
21.
22. template <int n>
23. struct IsOddNumber
24. {
25. enum
26. {
27. val = n == 0 ? 0 : IsEvenNumber<n - 1>::val
28. };
29. };
30.
31.
32. template <int n>
33. struct IsEvenNumber
34. {
35. enum
36. {
37. val = n == 0 ? 1 : IsOddNumber<n - 1>::val
38. };
39. };

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:


C++ Syntax (Toggle Plain Text)

1. int Ackermann(int x, int y)


2. {
3. // Base or Termination Condition
4. if (0 == x)
5. {
6. return y + 1;
7. }
8. // Error Handling condition
9. if (x < 0 || y < 0)
10. {
11. return -1;
12. }
13. // Recursive call by Linear method
14. else if (x > 0 && 0 == y)
15. {
16. return Ackermann(x-1, 1);
17. }
18. // Recursive call by Nested method
19. else
20. {
21. return Ackermann(x-1, Ackermann(x, y-1));
22. }
23. }

Compile Time Version Code


C++ Syntax (Toggle Plain Text)

1. // Base Or Termination condition


2. template <int y>
3. struct Ackermann<0, y>
4. {
5. enum { val = y + 1 };
6. };
7.
8. // Recursive Call by Linear Method
9. template <int x>
10. struct Ackermann<x, 0>
11. {
12. enum
13. {
14. val = Ackermann<x-1, 1>::val
15. };
16. };
17.
18. // Recursive Call by Nested Method
19. template <int x, int y>
20. struct Ackermann
21. {
22. Enum
23. {
24. val = Ackermann<x-1, Ackermann<x, y-1> ::val>::val
25. };
26. };

Types Of Recursion
Expand Post »
Detail About Recursion and its Type
Here I am going to give a detail about Recursion in C++.
Definition: Recursion is the process where a function is called itself but stack 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
(b) Compile- Time Recursion: By using Template

Each of these can be also divided into following types:

1. Linear Recursion
2. Binary Recursion
3. Tail Recursion
4. Mutual Recursion
5. 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

C++ Syntax (Toggle Plain Text)

1. int Fact(long n)
2. {
3. if(0>n)
4. return -1;
5. if(0 == n)
6. return 1;
7. else
8. {
9. return ( n* Fact(n-1));
10. }
11. }
Winding Process:

Function called Function return

Fact(6) 6*Fact(5)
Fact(5) 5*Fact(4)
Fact(4) 4*Fact(3)
Fact(3) 3* Fact(2)
Fact(2) 2* Fact(1)
Fact(1) 1* Fact(0)
Terminating Point
Fact(0) 1

Unwinding Process

Fact(1) 1*1
Fact(2) 2*1
Fact(3) 3*2*1
Fact(4) 4*3*2*1
Fact(5) 5*4*3*2*1
Fact(6) 6*5*4*3*2*1

Compile-Time Version

C++ Syntax (Toggle Plain Text)

1. // template for Base Condition


2. template <>
3. struct Fact<0>
4. {
5. enum
6. {
7. factVal = 1
8. };
9. };
10.
11. template <int n>
12. struct Fact
13. {
14. // Recursion call by linear method
15. enum
16. {
17. value = n * Fact<n - 1>::factVal
18. };
19. };
To test it how it's working at compile time, just call
cout << Fact<-1>::factVal ;
And compile it then compiler error will come, because no template for -1.

2. Binary Recursion: Binary Recursion is a process where function is called twice at a time
inplace of once at a time. Mostly it's using in data structure like operations for tree as traversal,
finding height, merging, etc.

Example: Fibonacci number

Run Time Version Code:


C++ Syntax (Toggle Plain Text)

1. int FibNum(int n)
2. {
3. // Base conditions
4. if (n < 1)
5. return -1;
6. if (1 == n || 2 == n)
7. return 1;
8.
9. // Recursive call by Binary Method
10. return FibNum(n - 1) + FibNum(n - 2); // At a time two
recursive function called so
11.
// binary
12. }

Compile Time Version Code


C++ Syntax (Toggle Plain Text)

1. // Base Conditions
2. template<>
3. struct FibNum<2>
4. {
5. enum { val = 1 };
6. };
7. template <>
8. struct FibNum<1>
9. {
10. enum { val = 1 };
11. };
12.
13. // Recursive call by Binary Method
14. 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:


C++ Syntax (Toggle Plain Text)

1. int FibNum(int n, int x, int y)


2. {
3. if (1 == n) // Base Condition
4. {
5. return y;
6. }
7. else // Recursive call by Tail method
8. {
9. return FibNum(n-1, y, x+y);
10. }
11. }
Compile Time Version Code

C++ Syntax (Toggle Plain Text)

1. template <int n, int x, int y>


2. struct FibNum
3. {
4. // Recursive call By tail method
5. enum
6. {
7. val = FibNum<n-1, y, (x+y)>::val
8. };
9. };
10.
11. // Base Condition or Termination
12. template<int x, int y>
13. struct FibNum<1, x, y>
14. {
15. enum
16. {
17. val = y
18. };
19. };
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:


C++ Syntax (Toggle Plain Text)

1. bool IsOddNumber(int n)
2. {
3. // Base or Termination Condition
4. if (0 == n)
5. return 0;
6. else
7. // Recursive call by Mutual Method
8. return IsEvenNumber(n - 1);
9. }
10.
11. bool IsEvenNumber(int n)
12. {
13. // Base or Termination Condition
14. if (0 == n)
15. return 1;
16. else
17. // Recursive call by Mutual Method
18. return IsOddNumber(n - 1);
19. }

Compile Time Version Code

C++ Syntax (Toggle Plain Text)

1.
2. // Base Or Termination Conditions
3. template <>
4. struct IsOddNumber<0>
5. {
6. enum
7. {
8. val = 0
9. };
10. };
11. template <>
12. struct IsEvenNumber<0>
13. {
14. enum
15. {
16. val = 1
17. };
18. };
19.
20. // Recursive calls by Mutual Method
21.
22. template <int n>
23. struct IsOddNumber
24. {
25. enum
26. {
27. val = n == 0 ? 0 : IsEvenNumber<n - 1>::val
28. };
29. };
30.
31.
32. template <int n>
33. struct IsEvenNumber
34. {
35. enum
36. {
37. val = n == 0 ? 1 : IsOddNumber<n - 1>::val
38. };
39. };

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:


C++ Syntax (Toggle Plain Text)

1. int Ackermann(int x, int y)


2. {
3. // Base or Termination Condition
4. if (0 == x)
5. {
6. return y + 1;
7. }
8. // Error Handling condition
9. if (x < 0 || y < 0)
10. {
11. return -1;
12. }
13. // Recursive call by Linear method
14. else if (x > 0 && 0 == y)
15. {
16. return Ackermann(x-1, 1);
17. }
18. // Recursive call by Nested method
19. else
20. {
21. return Ackermann(x-1, Ackermann(x, y-1));
22. }
23. }

Compile Time Version Code


C++ Syntax (Toggle Plain Text)

1. // Base Or Termination condition


2. template <int y>
3. struct Ackermann<0, y>
4. {
5. enum { val = y + 1 };
6. };
7.
8. // Recursive Call by Linear Method
9. template <int x>
10. struct Ackermann<x, 0>
11. {
12. enum
13. {
14. val = Ackermann<x-1, 1>::val
15. };
16. };
17.
18. // Recursive Call by Nested Method
19. template <int x, int y>
20. struct Ackermann
21. {
22. Enum
23. {
24. val = Ackermann<x-1, Ackermann<x, y-1> ::val>::val
25. };
26. };

Types Of Recursion
Expand Post »
Detail About Recursion and its Type

Here I am going to give a detail about Recursion in C++.


Definition: Recursion is the process where a function is called itself but stack 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
(b) Compile- Time Recursion: By using Template

Each of these can be also divided into following types:

1. Linear Recursion
2. Binary Recursion
3. Tail Recursion
4. Mutual Recursion
5. 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

C++ Syntax (Toggle Plain Text)

1. int Fact(long n)
2. {
3. if(0>n)
4. return -1;
5. if(0 == n)
6. return 1;
7. else
8. {
9. return ( n* Fact(n-1));
10. }
11. }

Winding Process:

Function called Function return

Fact(6) 6*Fact(5)
Fact(5) 5*Fact(4)
Fact(4) 4*Fact(3)
Fact(3) 3* Fact(2)
Fact(2) 2* Fact(1)
Fact(1) 1* Fact(0)
Terminating Point
Fact(0) 1

Unwinding Process

Fact(1) 1*1
Fact(2) 2*1
Fact(3) 3*2*1
Fact(4) 4*3*2*1
Fact(5) 5*4*3*2*1
Fact(6) 6*5*4*3*2*1

Compile-Time Version

C++ Syntax (Toggle Plain Text)

1. // template for Base Condition


2. template <>
3. struct Fact<0>
4. {
5. enum
6. {
7. factVal = 1
8. };
9. };
10.
11. template <int n>
12. struct Fact
13. {
14. // Recursion call by linear method
15. enum
16. {
17. value = n * Fact<n - 1>::factVal
18. };
19. };
To test it how it's working at compile time, just call
cout << Fact<-1>::factVal ;
And compile it then compiler error will come, because no template for -1.

2. Binary Recursion: Binary Recursion is a process where function is called twice at a time
inplace of once at a time. Mostly it's using in data structure like operations for tree as traversal,
finding height, merging, etc.

Example: Fibonacci number

Run Time Version Code:


C++ Syntax (Toggle Plain Text)

1. int FibNum(int n)
2. {
3. // Base conditions
4. if (n < 1)
5. return -1;
6. if (1 == n || 2 == n)
7. return 1;
8.
9. // Recursive call by Binary Method
10. return FibNum(n - 1) + FibNum(n - 2); // At a time two
recursive function called so
11.
// binary
12. }

Compile Time Version Code


C++ Syntax (Toggle Plain Text)

1. // Base Conditions
2. template<>
3. struct FibNum<2>
4. {
5. enum { val = 1 };
6. };
7. template <>
8. struct FibNum<1>
9. {
10. enum { val = 1 };
11. };
12.
13. // Recursive call by Binary Method
14. 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:


C++ Syntax (Toggle Plain Text)

1. int FibNum(int n, int x, int y)


2. {
3. if (1 == n) // Base Condition
4. {
5. return y;
6. }
7. else // Recursive call by Tail method
8. {
9. return FibNum(n-1, y, x+y);
10. }
11. }
Compile Time Version Code

C++ Syntax (Toggle Plain Text)

1. template <int n, int x, int y>


2. struct FibNum
3. {
4. // Recursive call By tail method
5. enum
6. {
7. val = FibNum<n-1, y, (x+y)>::val
8. };
9. };
10.
11. // Base Condition or Termination
12. template<int x, int y>
13. struct FibNum<1, x, y>
14. {
15. enum
16. {
17. val = y
18. };
19. };

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:


C++ Syntax (Toggle Plain Text)

1. bool IsOddNumber(int n)
2. {
3. // Base or Termination Condition
4. if (0 == n)
5. return 0;
6. else
7. // Recursive call by Mutual Method
8. return IsEvenNumber(n - 1);
9. }
10.
11. bool IsEvenNumber(int n)
12. {
13. // Base or Termination Condition
14. if (0 == n)
15. return 1;
16. else
17. // Recursive call by Mutual Method
18. return IsOddNumber(n - 1);
19. }

Compile Time Version Code

C++ Syntax (Toggle Plain Text)

1.
2. // Base Or Termination Conditions
3. template <>
4. struct IsOddNumber<0>
5. {
6. enum
7. {
8. val = 0
9. };
10. };
11. template <>
12. struct IsEvenNumber<0>
13. {
14. enum
15. {
16. val = 1
17. };
18. };
19.
20. // Recursive calls by Mutual Method
21.
22. template <int n>
23. struct IsOddNumber
24. {
25. enum
26. {
27. val = n == 0 ? 0 : IsEvenNumber<n - 1>::val
28. };
29. };
30.
31.
32. template <int n>
33. struct IsEvenNumber
34. {
35. enum
36. {
37. val = n == 0 ? 1 : IsOddNumber<n - 1>::val
38. };
39. };

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:


C++ Syntax (Toggle Plain Text)

1. int Ackermann(int x, int y)


2. {
3. // Base or Termination Condition
4. if (0 == x)
5. {
6. return y + 1;
7. }
8. // Error Handling condition
9. if (x < 0 || y < 0)
10. {
11. return -1;
12. }
13. // Recursive call by Linear method
14. else if (x > 0 && 0 == y)
15. {
16. return Ackermann(x-1, 1);
17. }
18. // Recursive call by Nested method
19. else
20. {
21. return Ackermann(x-1, Ackermann(x, y-1));
22. }
23. }

Compile Time Version Code


C++ Syntax (Toggle Plain Text)

1. // Base Or Termination condition


2. template <int y>
3. struct Ackermann<0, y>
4. {
5. enum { val = y + 1 };
6. };
7.
8. // Recursive Call by Linear Method
9. template <int x>
10. struct Ackermann<x, 0>
11. {
12. enum
13. {
14. val = Ackermann<x-1, 1>::val
15. };
16. };
17.
18. // Recursive Call by Nested Method
19. template <int x, int y>
20. struct Ackermann
21. {
22. Enum
23. {
24. val = Ackermann<x-1, Ackermann<x, y-1> ::val>::val
25. };
26. };

Types Of Recursion
Expand Post »
Detail About Recursion and its Type

Here I am going to give a detail about Recursion in C++.


Definition: Recursion is the process where a function is called itself but stack 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
(b) Compile- Time Recursion: By using Template

Each of these can be also divided into following types:


1. Linear Recursion
2. Binary Recursion
3. Tail Recursion
4. Mutual Recursion
5. 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

C++ Syntax (Toggle Plain Text)

1. int Fact(long n)
2. {
3. if(0>n)
4. return -1;
5. if(0 == n)
6. return 1;
7. else
8. {
9. return ( n* Fact(n-1));
10. }
11. }

Winding Process:

Function called Function return

Fact(6) 6*Fact(5)
Fact(5) 5*Fact(4)
Fact(4) 4*Fact(3)
Fact(3) 3* Fact(2)
Fact(2) 2* Fact(1)
Fact(1) 1* Fact(0)
Terminating Point
Fact(0) 1

Unwinding Process

Fact(1) 1*1
Fact(2) 2*1
Fact(3) 3*2*1
Fact(4) 4*3*2*1
Fact(5) 5*4*3*2*1
Fact(6) 6*5*4*3*2*1

Compile-Time Version

C++ Syntax (Toggle Plain Text)

1. // template for Base Condition


2. template <>
3. struct Fact<0>
4. {
5. enum
6. {
7. factVal = 1
8. };
9. };
10.
11. template <int n>
12. struct Fact
13. {
14. // Recursion call by linear method
15. enum
16. {
17. value = n * Fact<n - 1>::factVal
18. };
19. };
To test it how it's working at compile time, just call
cout << Fact<-1>::factVal ;
And compile it then compiler error will come, because no template for -1.

2. Binary Recursion: Binary Recursion is a process where function is called twice at a time
inplace of once at a time. Mostly it's using in data structure like operations for tree as traversal,
finding height, merging, etc.

Example: Fibonacci number


Run Time Version Code:
C++ Syntax (Toggle Plain Text)

1. int FibNum(int n)
2. {
3. // Base conditions
4. if (n < 1)
5. return -1;
6. if (1 == n || 2 == n)
7. return 1;
8.
9. // Recursive call by Binary Method
10. return FibNum(n - 1) + FibNum(n - 2); // At a time two
recursive function called so
11.
// binary
12. }

Compile Time Version Code


C++ Syntax (Toggle Plain Text)

1. // Base Conditions
2. template<>
3. struct FibNum<2>
4. {
5. enum { val = 1 };
6. };
7. template <>
8. struct FibNum<1>
9. {
10. enum { val = 1 };
11. };
12.
13. // Recursive call by Binary Method
14. 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:


C++ Syntax (Toggle Plain Text)
1. int FibNum(int n, int x, int y)
2. {
3. if (1 == n) // Base Condition
4. {
5. return y;
6. }
7. else // Recursive call by Tail method
8. {
9. return FibNum(n-1, y, x+y);
10. }
11. }
Compile Time Version Code

C++ Syntax (Toggle Plain Text)

1. template <int n, int x, int y>


2. struct FibNum
3. {
4. // Recursive call By tail method
5. enum
6. {
7. val = FibNum<n-1, y, (x+y)>::val
8. };
9. };
10.
11. // Base Condition or Termination
12. template<int x, int y>
13. struct FibNum<1, x, y>
14. {
15. enum
16. {
17. val = y
18. };
19. };

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:


C++ Syntax (Toggle Plain Text)
1. bool IsOddNumber(int n)
2. {
3. // Base or Termination Condition
4. if (0 == n)
5. return 0;
6. else
7. // Recursive call by Mutual Method
8. return IsEvenNumber(n - 1);
9. }
10.
11. bool IsEvenNumber(int n)
12. {
13. // Base or Termination Condition
14. if (0 == n)
15. return 1;
16. else
17. // Recursive call by Mutual Method
18. return IsOddNumber(n - 1);
19. }

Compile Time Version Code

C++ Syntax (Toggle Plain Text)

1.
2. // Base Or Termination Conditions
3. template <>
4. struct IsOddNumber<0>
5. {
6. enum
7. {
8. val = 0
9. };
10. };
11. template <>
12. struct IsEvenNumber<0>
13. {
14. enum
15. {
16. val = 1
17. };
18. };
19.
20. // Recursive calls by Mutual Method
21.
22. template <int n>
23. struct IsOddNumber
24. {
25. enum
26. {
27. val = n == 0 ? 0 : IsEvenNumber<n - 1>::val
28. };
29. };
30.
31.
32. template <int n>
33. struct IsEvenNumber
34. {
35. enum
36. {
37. val = n == 0 ? 1 : IsOddNumber<n - 1>::val
38. };
39. };

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:


C++ Syntax (Toggle Plain Text)

1. int Ackermann(int x, int y)


2. {
3. // Base or Termination Condition
4. if (0 == x)
5. {
6. return y + 1;
7. }
8. // Error Handling condition
9. if (x < 0 || y < 0)
10. {
11. return -1;
12. }
13. // Recursive call by Linear method
14. else if (x > 0 && 0 == y)
15. {
16. return Ackermann(x-1, 1);
17. }
18. // Recursive call by Nested method
19. else
20. {
21. return Ackermann(x-1, Ackermann(x, y-1));
22. }
23. }

Compile Time Version Code


C++ Syntax (Toggle Plain Text)
1. // Base Or Termination condition
2. template <int y>
3. struct Ackermann<0, y>
4. {
5. enum { val = y + 1 };
6. };
7.
8. // Recursive Call by Linear Method
9. template <int x>
10. struct Ackermann<x, 0>
11. {
12. enum
13. {
14. val = Ackermann<x-1, 1>::val
15. };
16. };
17.
18. // Recursive Call by Nested Method
19. template <int x, int y>
20. struct Ackermann
21. {
22. Enum
23. {
24. val = Ackermann<x-1, Ackermann<x, y-1> ::val>::val
25. };
26. };

Types Of Recursion
Expand Post »
Detail About Recursion and its Type

Here I am going to give a detail about Recursion in C++.


Definition: Recursion is the process where a function is called itself but stack 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
(b) Compile- Time Recursion: By using Template

Each of these can be also divided into following types:

1. Linear Recursion
2. Binary Recursion
3. Tail Recursion
4. Mutual Recursion
5. 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

C++ Syntax (Toggle Plain Text)

1. int Fact(long n)
2. {
3. if(0>n)
4. return -1;
5. if(0 == n)
6. return 1;
7. else
8. {
9. return ( n* Fact(n-1));
10. }
11. }

Winding Process:

Function called Function return

Fact(6) 6*Fact(5)
Fact(5) 5*Fact(4)
Fact(4) 4*Fact(3)
Fact(3) 3* Fact(2)
Fact(2) 2* Fact(1)
Fact(1) 1* Fact(0)
Terminating Point
Fact(0) 1

Unwinding Process

Fact(1) 1*1
Fact(2) 2*1
Fact(3) 3*2*1
Fact(4) 4*3*2*1
Fact(5) 5*4*3*2*1
Fact(6) 6*5*4*3*2*1

Compile-Time Version

C++ Syntax (Toggle Plain Text)

1. // template for Base Condition


2. template <>
3. struct Fact<0>
4. {
5. enum
6. {
7. factVal = 1
8. };
9. };
10.
11. template <int n>
12. struct Fact
13. {
14. // Recursion call by linear method
15. enum
16. {
17. value = n * Fact<n - 1>::factVal
18. };
19. };
To test it how it's working at compile time, just call
cout << Fact<-1>::factVal ;
And compile it then compiler error will come, because no template for -1.

2. Binary Recursion: Binary Recursion is a process where function is called twice at a time
inplace of once at a time. Mostly it's using in data structure like operations for tree as traversal,
finding height, merging, etc.

Example: Fibonacci number

Run Time Version Code:


C++ Syntax (Toggle Plain Text)

1. int FibNum(int n)
2. {
3. // Base conditions
4. if (n < 1)
5. return -1;
6. if (1 == n || 2 == n)
7. return 1;
8.
9. // Recursive call by Binary Method
10. return FibNum(n - 1) + FibNum(n - 2); // At a time two
recursive function called so
11.
// binary
12. }

Compile Time Version Code


C++ Syntax (Toggle Plain Text)

1. // Base Conditions
2. template<>
3. struct FibNum<2>
4. {
5. enum { val = 1 };
6. };
7. template <>
8. struct FibNum<1>
9. {
10. enum { val = 1 };
11. };
12.
13. // Recursive call by Binary Method
14. 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:


C++ Syntax (Toggle Plain Text)

1. int FibNum(int n, int x, int y)


2. {
3. if (1 == n) // Base Condition
4. {
5. return y;
6. }
7. else // Recursive call by Tail method
8. {
9. return FibNum(n-1, y, x+y);
10. }
11. }
Compile Time Version Code

C++ Syntax (Toggle Plain Text)

1. template <int n, int x, int y>


2. struct FibNum
3. {
4. // Recursive call By tail method
5. enum
6. {
7. val = FibNum<n-1, y, (x+y)>::val
8. };
9. };
10.
11. // Base Condition or Termination
12. template<int x, int y>
13. struct FibNum<1, x, y>
14. {
15. enum
16. {
17. val = y
18. };
19. };

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:


C++ Syntax (Toggle Plain Text)

1. bool IsOddNumber(int n)
2. {
3. // Base or Termination Condition
4. if (0 == n)
5. return 0;
6. else
7. // Recursive call by Mutual Method
8. return IsEvenNumber(n - 1);
9. }
10.
11. bool IsEvenNumber(int n)
12. {
13. // Base or Termination Condition
14. if (0 == n)
15. return 1;
16. else
17. // Recursive call by Mutual Method
18. return IsOddNumber(n - 1);
19. }

Compile Time Version Code

C++ Syntax (Toggle Plain Text)

1.
2. // Base Or Termination Conditions
3. template <>
4. struct IsOddNumber<0>
5. {
6. enum
7. {
8. val = 0
9. };
10. };
11. template <>
12. struct IsEvenNumber<0>
13. {
14. enum
15. {
16. val = 1
17. };
18. };
19.
20. // Recursive calls by Mutual Method
21.
22. template <int n>
23. struct IsOddNumber
24. {
25. enum
26. {
27. val = n == 0 ? 0 : IsEvenNumber<n - 1>::val
28. };
29. };
30.
31.
32. template <int n>
33. struct IsEvenNumber
34. {
35. enum
36. {
37. val = n == 0 ? 1 : IsOddNumber<n - 1>::val
38. };
39. };

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:


C++ Syntax (Toggle Plain Text)

1. int Ackermann(int x, int y)


2. {
3. // Base or Termination Condition
4. if (0 == x)
5. {
6. return y + 1;
7. }
8. // Error Handling condition
9. if (x < 0 || y < 0)
10. {
11. return -1;
12. }
13. // Recursive call by Linear method
14. else if (x > 0 && 0 == y)
15. {
16. return Ackermann(x-1, 1);
17. }
18. // Recursive call by Nested method
19. else
20. {
21. return Ackermann(x-1, Ackermann(x, y-1));
22. }
23. }

Compile Time Version Code


C++ Syntax (Toggle Plain Text)

1. // Base Or Termination condition


2. template <int y>
3. struct Ackermann<0, y>
4. {
5. enum { val = y + 1 };
6. };
7.
8. // Recursive Call by Linear Method
9. template <int x>
10. struct Ackermann<x, 0>
11. {
12. enum
13. {
14. val = Ackermann<x-1, 1>::val
15. };
16. };
17.
18. // Recursive Call by Nested Method
19. template <int x, int y>
20. struct Ackermann
21. {
22. Enum
23. {
24. val = Ackermann<x-1, Ackermann<x, y-1> ::val>::val
25. };
26. };

http://www.daniweb.com/forums/thread122912.html

You might also like