Recursive and tail recursion, recursive tail recursion

Source: Internet
Author: User

Recursive and tail recursion, recursive tail recursion
I believe that if a person asks us for a Fibonacci series, if you have learned the C language, you will certainly say that it is easy to implement it by using recursion, however, if someone asks you to calculate 50th of the Fibonacci numbers and you know about recursion, you probably won't say recursion. If you know more about recursion, you can say that recursion can also be used. 1. recursion first, let's talk about recursion. Simply put, a function needs to call itself to complete a function, which is called recursion. But we need to be clear that recursion is not always calling itself, and we need to give it a stop time. Like a war, you need to know the attack route, but you need to be able to time and retreat in case of an emergency. So our recursion is the same. You need to give him a forward path and a return path. SoWhen a recursive policy is used, a clear recursive termination condition must be defined as a recursive exit.But we all know that recursion has its fatal weakness. During the recursive call process, the system opens a stack for storing the return points and local volumes of each layer, therefore, too many recursion times may cause stack overflow, because it is very inefficient than loop, this is what I mentioned earlier. If you want to calculate 50th of the Fibonacci number, it is not good to directly use recursive methods. Let's take a look at the program:

int Fib(int n){    if( n < 2)         return n;     return (Fib(n-1)+Fib(n-2));}
The code written in this way is very concise. To analyze its execution process, we give n = 5:

In this case, you still can't see the problem. In fact, the figure above is quite a tree structure:

     
The red part will be obtained again later. If the value we give is not 5, it is a larger number, and the number and number of repeated computations and calls will become more. It can be seen that in such a process, we have repeatedly calculated some values, coupled with the repeated opening up of stack space, making it very inefficient, you can try to find 40th 50 Fibonacci numbers.
 
2. tail recursion
Tail recursion is a programming technique. In a recursive function, the result returned by a recursive call is always directly returned, which is called tail recursion. Recursive functions at the end can be used to convert algorithms into function programming languages. In addition, they are easily optimized into common loops from the compiler perspective. This is because from the computer fundamentals, all cycles are implemented by repeatedly moving to the beginning of the Code. If there is tail delivery, you only need to stack one stack, because the computer only needs to change the function parameters and then call them again.
 1 int Fib(int n, int ret1, int ret2) 2   3 { 4     if (n ==0 ) 5     { 6         return ret1; 7     } 8     else 9     {10  return Fib(n - 1, ret2, ret1 +ret2);11     }12 }

 

The execution steps are as follows. Each ret1 request is the current return value. When the number of n is reduced to 0, the ret1 request is the nth number: here we need to pass ret = 0, ret2 = 1; Fib (n-1, ret2, ret1 + ret2) when passing parameters, the original simple recursive stack layers grow exponentially like binary trees, but now the stack layers are like arrays and become linear growth. It is really amazing. It is also very simple to sum up, originally, the stack was expanded first, and then the computing results were collected while the stack was being called, but now it was computed through parameters while calling itself. Ret1 is the number of n, and ret2 is the sum of the number of n and n + 1, which is actually the same as our "iteration. Let's take a look at the Fibonacci sequence method written by iteration. 3. Iterative Method
int Fib(int n){    int num1 = 1;    int num2 = num1;    int num3 = num1;    while (n > 2)    {        num3 = num1 + num2;        num1 = num2;        num2 = num3;        n--;    }    return num3;}

  

We can see that the method implemented by the iterative method is actually the same as our tail recursion method, but the iterative method is easy to understand and compared with the tail recursion, because it does not need to open up stack space, therefore, the three methods are the most efficient. When solving practical problems, we should select appropriate methods based on actual requirements.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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.