Relationship between Recursion and stack

Source: Internet
Author: User

 

Recursion and stack
Recursion is an important concept and a heavy programming method. To put it simply, if a function, process, or data structure definition applies itself (as one of the definition items ), this function, process, or data structure is called recursion. For example, a factorial function can be recursively defined as follows:
It must be noted that the recursive definition cannot be a "loop definition ". Therefore, any recursive definition must meet the following two conditions:
① The application of the defined item in the definition (that is, the appearance of the definition item) has a smaller "scale ";
② The definition of the defined item on the minimum "scale" is not recursive.
For example, in the preceding factorial function definition, the defined item n! Application in definition (n-1 )! Has a "scale" (I .e. N-1) smaller than the original (I .e. N ). At the same time, n! The definition on the minimum "scale" (that is, 0) is not recursive (defined directly by the natural number 1 ). These two conditions actually constitute the basic principle of recursive programming. In addition, the part that reflects condition ② is usually written at the beginning of the recursion process.
There are many actual recursive definitions. It is easy to write Recursive Algorithms for solving these problems. The recursive algorithm for calculating factorial functions is as follows:
  Public static int NN (int I) {<br/> if (I = 0) {<br/> return 1; <br/>} else <br/> return I * NN (I-1); <br/>}
Recursive call is triggered by running the recursive process. For example, the recursive call in the execution of F (3) -- the return process is 3-5 (.

F3

Bytes
R Branch

    
① Success
┌ ┈ ┘
Zookeeper

F2
→ Success
Bytes
T Branch

    
② Success
┌ ┈ ┘
Zookeeper

F1
→ Success
Bytes
T Branch

    
③ Random
┌ ┈ ┘
Zookeeper

F0
→ Success

R1 Branch

Bytes

Zookeeper
└ ┈ ┐
6 ⑥ └

T1 Branch
Bytes
Zookeeper

Zookeeper
└ ┈ ┐
2 ⑤ success

T1 Branch
Bytes
Zookeeper

← ┐ ┈
└ ┈ ┐
1 ④ bytes


Bytes
Limit limit 1

Figure 3-5 (a) f (3) execute recursive call-return order

 

 
 
 
 

Top →

 
 
 
3 R

Top →

 
 
2 R
3 R

Top →

 
1 R
2 R

3

R

Top →

 
 
2 R
3 R

Top →

 
 
 
3 R

 
 
 
 

Top →
 

 
Before calling F2

 
After F2 is called
 
After F1 is called
 
After f0 is called
 
After F1 is returned
 
After F2 is returned

Top →

 
Return
After F3

Figure 3-5 (B) status changes of the corresponding Stack

Recursive call-return control is essentially different from non-recursive process control. It can also be implemented by a work stack. For the above process f, the return position R should be set to the internal and Outer Return statements. To distinguish the first level of recursion returned, you can save the called parameters together when the returned position is pushed to the stack. Figure 3-5 (B) shows the State Change Process of the stack corresponding to (.

Address: http://www.nhyz.org/nhxi/nhxi/jiegousuanfa/jiegou22.htm

Welcome:

 

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.