Recursion and tail recursion

Source: Internet
Author: User



C allows a function to call itself, a procedure called recursion (recursion).
The simplest recursive form is to put a recursive call statement at the end of the function just before the return statement. This form is called tail recursion or end recursion, because recursive calls appear at the tail of the function. Because recursion is the equivalent of a loop statement , it is the simplest recursive form.
Recursion must contain statements that can terminate recursive calls!
Recursion is a bit of a simple way to provide some programming problems, but the disadvantage is that some recursive algorithms will quickly deplete the computer's memory resources. At the same time, using recursive programs is difficult to read and maintain


Consulted the experts, indeed for the "tail recursion" situation, that is, the function body used in the variable does not need to save the stack, Gcc-o2 can be optimized, it will be expanded into a loop , but if there are branches in the recursive function, such as the implementation of the path traversal. There is a branch to save the condition variable, you need to press the stack, this situation will not open. This is the case for most of the actual coding.



650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7A/F7/wKioL1bDSnKBgxc2AAC55hB-XQg309.png "title=" capture. PNG "alt=" Wkiol1bdsnkbgxc2aac55hb-xqg309.png "/>

/* Lists the files or folders under the specified directory, including the contents of subdirectories. This is the list of all content in the specified directory. Because there are directories in the directory, just use the same function that lists the directory functions to complete. You can also call this feature again if the directory appears in the listing process. That is, the function itself calls itself. This form of representation, or programming technique, is called recursion. Recursion to note: 1, limit conditions. 2, pay attention to the number of recursion. Try to avoid memory overflow. */import java.io.*;class filedemo3 {    public static void  Main (String[] args)      {        file  dir = new file ("D:\\testdir");         //showdir ( dir,0);         //tobin (6);         //int n = getsum (8000);         // System.out.println ("n=" +n);         system.out.println (Dir.delete ());     }    public static string getlevel (Int level)     {        StringBuilder Sb = new stringbuilder ();         sb.append ("|--");         for (int x=0; x<level; x++)          {            // Sb.append ("|--");             sb.insert (0, "|    ");        }         Return sb.tostring ();    }    public static void  Showdir (File dir,int level)     {                 system.out.println (Getlevel (level) +dir.getname ());         level++;        file[] files  = dir.listfiles ();         for (int x=0; x<files.length; x++)          {             if (Files[x].isdirectory ())                  showdir (Files[x],level);             else                 System.out.println (Getlevel (level) +files[x]);        }     }    public static int getsum (int n)      {        if (n==1)              return 1;        return n+ Getsum (n-1);     }    public static void tobin (Int num)     {         if (num>0)         {             tobin (NUM/2);             system.out.println (num%2);         }    }//can not end the recursion, became the loop     public static void method ()     {        method ();     }         }


This article is from the "bit accumulation" blog, please be sure to keep this source http://tianxingzhe.blog.51cto.com/3390077/1742590

Recursion and tail recursion

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.