Professional Documents
Culture Documents
Types of Recursion01
Types of Recursion01
Types of Recursion01
Expand Post »
Detail About Recursion and its Type
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.
Run-Time Version
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:
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
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.
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. }
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.
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. }
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.
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
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.
Run-Time Version
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:
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
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.
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. }
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.
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. }
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.
Types Of Recursion
Expand Post »
Detail About Recursion and its Type
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.
Run-Time Version
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:
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
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.
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. }
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
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
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. }
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.
Types Of Recursion
Expand Post »
Detail About Recursion and its Type
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.
Run-Time Version
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:
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
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.
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. }
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.
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.
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.
Types Of Recursion
Expand Post »
Detail About Recursion and its Type
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.
Run-Time Version
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:
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
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.
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. }
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.
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.
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. }
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.
http://www.daniweb.com/forums/thread122912.html