What is tail recursion? tail recursion

Source: Internet
Author: User

What is tail recursion? tail recursion

Before learning about tail recursion, first understand the end call.

In computer science, tail call refers to the case where the last action in a function is a function call: that is, the returned value of this call is directly returned by the current function. In this case, the call position is the end position. (From Wikipedia)

The above explanation is from Wikipedia. This topic describes what a tail call is. For example:

function foo(data) {    a(data);    return b(data);}

  

Here, both a (data) and B (data) are function calls, but B (data) is the last run before the function returns, so it is also called the tail position. For example:

function foo1(data) {    return a(data) + 1;}function foo2(data) {    var ret = a(data);    return ret;}function foo3(data) {    var ret = a(data);    return (ret === 0) ? 1 : ret;}

  

This is not an end call. For foo1, the last action is a + 1 operation, not a direct function call. For foo3, it is the result returned by computation, or a tail call ., Foo2 is not the end call.

An important feature of tail call is that it can update a stack frame instead of adding a new stack frame to the call stack.

What is tail recursion:

If a function is called at the end (or another function called at the end), this is called tail recursion, which is a special case of recursion. In the form, as long as the last return statement returns a complete function, it is tail recursion. Note: tail call is not necessarily a recursive call, but tail recursion must be a tail call.

Next, we will use the Fibonacci series and factorial to further understand the meaning of tail recursion.

Fibonacci Series

The conventional Fibonacci sequence algorithm may be like this:

int fib(int n) {    if (n <= 2) {        return 1;    }    return fib(n - 1) + fib(n - 2);}

  

In the above recursive calculation, the final return operation is an addition operation. So it is not tail recursion.

If tail recursion is like this:

/** Calculate the value of the nth Fibonacci series @ param n nth number @ param acc1 nth number @ param acc2 nth and NTH + 1 sum @ return returns the Fibonacci number of columns */int tailfib (int n, int acc1, int acc2) {if (n <2) {return acc1;} return tailfib (n-1, acc2, acc1 + acc2 );}

  

For example, to calculate the value of the 10th-bit Fibonacci series, you only need fib (, 1.

Take a look at the test results. The test procedure is as follows:

Int main (int argc, const char * argv []) {clock_t start, finish; start = clock (); printf ("Calculation Result: % d \ n ", fib (45); finish = clock (); printf ("elapsed time -------- % lu \ n", finish-start); start = clock (); printf ("Calculation Result: % d \ n", tailfib (45, 1, 1); finish = clock (); printf ("Time consumed -------- % lu \ n ", finish-start); return 0 ;}

  

The calculation result is as follows:

Calculation Result: 1134903170 elapsed time -------- 5540692 Calculation Result: 1134903170 elapsed time -------- 4 Program ended with exit code: 0

  

Efficiency can be imagined.

 

Factorial

The conventional factorial calculation method may be as follows:

int fac(int n) {    if (n == 1) {        return 1;    }    return fac(n-1) * n;}

Complexity: O (n)

The tail recursion algorithm is as follows:

int tailfac(int n,int sum) {    if (n == 1) {        return sum;    }    return fac(n-1, n * sum);}

Complexity: O (1)

Test the efficiency. The test procedure is as follows:

Int main (int argc, const char * argv []) {clock_t start, finish; start = clock (); printf ("Calculation Result: % d \ n ", fac (16); finish = clock (); printf ("elapsed time -------- % lu \ n", finish-start); start = clock (); printf ("Calculation Result: % d \ n", tailfac (16,1); finish = clock (); printf ("Time consumed -------- % lu \ n ", finish-start); return 0 ;}

  

Test results:

Computing Result: 2004189184 elapsed time -------- 31 Calculation Result: 2004189184 elapsed time -------- 2

  

The tail recursion efficiency is relatively high, but I personally think it is difficult to understand the tail recursion algorithm. You need to mark the role of each input parameter. Otherwise, the algorithm may not be used when you are new to it.

 

Reference: Ruan Yifeng's network log Wikipedia

Reprinted please mark Source: http://www.cnblogs.com/zhanggui/p/7722541.html

Related Article

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.