Linear recursion and tail recursion

Source: Internet
Author: User

Linear recursion is what we usually call recursion. The last step of a linear recursion function is not a recursive operation. Instead, the final condition is substituted into the calculation. During each recursive call, parameters and local variables in the recursive function must be stored in the stack,Stack Overflow occurs when the data volume is large.

Tail recursion, that is, linear iteration. The last operation of the tail recursion function is recursion, that is, before recursion, all operations are executed first. The advantage is that, there is no need to spend a lot of stack space to save parameters and local variables in the last recursion. This is because the previous data has been computed after the last recursion operation and passed to the current recursive function, in this way, the local variables and parameters in the previous recursion will be deleted and the space will be released without stack overflow. However, many compilers do not automatically optimize tail recursion, that is, when the compiler determines whether the current operation is a recursive operation, it does not care whether it is a linear or tail recursion, in this way, local variables and parameters will not be deleted. In addition, tail recursion can generally be converted into loop statements.

Let's use the factorial example to see the difference between the two:
Factorial: 5! = 1*2*3*4*5. The result is 120.
Linear recursion:
Long rescuvie (long N)
{
Return (n = 1 )? 1: N * rescuvie (n-1 );
}
The call process is as follows:
When n = 5
For linear recursion, the recursion process is as follows:
Rescuvie (5)
Start to call
{5 * rescuvie (4 )}
{5*{4 * rescuvie (3 )}}
{5*{4*{3 * rescuvie (2 )}}}
{5*{4*{3*{2 * rescuvie (1 )}}}}
{5*{4*{3*{2*1 }}}}
{5*{4*{3*2 }}}
{5*{4*6 }}
{5*24}
120

Tail recursion:
Long tailrescuvie (long N, long ){
Return (n = 1 )? A: tailrescuvie (n-1, A * n );
}

For tail recursion, the recursion process is as follows:
Tailrescuvie (5)
Tailrescuvie (5, 1)
Tailrescuvie (4, 5)
Tailrescuvie (3, 20)
Tailrescuvie (2, 60)
Tailrescuvie (1,120)
120 Tail recursion is easy to optimize at the compiler level. If the compiler is not optimized, the effect is similar to linear recursion.  Linear recursion and tail recursion are used to solve the peach eating problem of classic monkeys.

On the first day, the monkey picked up a few peaches and immediately ate half of them. If they were not satisfied, they ate one more. In the morning of the next day, I ate half of the remaining peaches.

I ate one. In the future, eat the remaining half of the day before and add one. There is just one remaining in 10th days. How many peaches have monkeys picked on the first day?

Linear recursion 
# Include <iostream> using namespace STD; int allsum (INT day) {If (Day = 10) return 1; elsereturn 2 * allsum (day + 1) + 2 ;} int main () {int sum = allsum (1); cout <"A total of" <sum <"Peaches" <Endl; return 0 ;}

  

Running result:    Implementation of tail recursion 
# Include <iostream> using namespace STD; int allsum (INT day, int total) {If (Day = 10) return total; elsereturn allsum (day + 1, total * 2 + 2);} int main () {int sum = allsum (1, 1 ); cout <"A total of" <sum <"Peaches" <Endl; return 0 ;}

  

Running result:

     For more information about linear recursion and tail recursion depth, seeHttp://blog.zhaojie.me/2009/04/tail-recursion-explanation.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.