![]() |
Detail About Recursion and its Type
Article is originally written by Zeeshan Amjad
IntroductionHere 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. Many complex problem can be solved by recursion in a simple code. But it's too much costly than iterative. because in every recursion call one stack frame will formed.You all already know that about it's cost. but if problem is very complex than no way to solve except recursion. BackgroundFirst recursion came into mathematics and then came into Computer science. Idea of it's use that first broke your problem into subproblems and solve it by using recursion. The codeIn 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 Code:
int Fact(long n)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 Code:
// template for Base Conditioncout << 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 Code:
int FibNum(int n)Code:
// Base ConditionsExample: Fibonacci number Run Time Version Code Code:
int FibNum(int n, int x, int y)Code:
template <int n, int x, int y>Example: To find Even Or Odd number Run Time Version Code Code:
bool IsOddNumber(int n)Code:
// Base Or Termination ConditionsExample: Ackermann function Run Time Version Code Code:
int Ackermann(int x, int y)Code:
// Base Or Termination conditionReferences"Teaching recursion using recursively generated geometric design" by Aaron Gordon |
Re: Detail About Recursion and its Type
Really good article, impressive I must say.
|
Re: Detail About Recursion and its Type
very good article
|
Re: Detail About Recursion and its Type
nice one
|
Re: Detail About Recursion and its Type
|
Re: Detail About Recursion and its Type
Quote:
|
Re: Detail About Recursion and its Type
Winner of Article of the month for May 2008
|
Re: Detail About Recursion and its Type
hi i am hire
|
Re: Detail About Recursion and its Type
Thank you.
|
Re: Detail About Recursion and its Type
thx.....so good
|
| All times are GMT +5.5. The time now is 00:36. |