PHP as the preferred technology for developing dynamic page Web, we must keep in mind the basics of it to help with programming. Let's take a look at how the PHP recursive algorithm is going.
1, the meaning of calling subroutine:
When the main program executes to the call subroutine a statement, the system saves some necessary field data, then executes a goto statement similar to the basic language, jumps to subroutine a (for the sake of simplicity, I ignore the parameter passing this process). When subroutine a executes to the Call subroutine B statement, the system practices as above, jumps to subroutine B. After the subroutine B executes all the statements, it jumps back to subroutine a to call the next statement of the subroutine B statement (I ignored the return value processing) When subroutine a executes, jumps back to the main program call subroutine a statement of the next statement, the main program execution to the end. To make a comparison: I ate half of the meal (executing the main program) and someone called me (executing subroutine a), and the phone rang again (executing sub-program B), I just had to answer the phone, and then finish the conversation with someone, and finally finish the meal (I'm tired of eating it too. J).
2. Recognize recursive functions
We've all learned math induction in high School, PHP recursive algorithms such as:
Beg n! We can put n! So the definition is 3! , we must first find out 2! , requires 2! , you must first ask for 1! , requires 1! , you must first ask for 0! , and 0!=1, so 1!=0!*1=1, and then seek 2!,3!. We can observe, in addition to the calculation of 0, the function representation. Subroutines, other subroutines are basically similar, we can design such a subroutine:
int factorial (int i) {
int res;
Res=factorial (I-1) *i;
return res;
}
Then when the main program statement s=factorial (3) is executed, factorial (3) is executed, but in the execution of factorial (3), factorial (2) is called, and it is important to note that factorial (3) and factorial (2) Although it is the same code snippet, in memory It's data area is two copies! The execution of factorial (2) calls factorial (1), executes factorial (1) and calls factorial (0), and each time the factorial function is called, it adds a data area to the memory. So these multiple copies of the function can be seen as a number of different names of functions to understand, but we have a problem with this function, in the execution of factorial (0), it will call factorial (-1) ... Cause a dead loop, that is, in the factorial function, we have to ensure that the function is not called at the appropriate time, that is, do not execute res=factorial (I-1) *i; So the function should be changed to:
int factorial (int i) {
int res;
if (i>0) res=factorial (I-1) *i; else Res=1;
return res;
}
3, how to consider using PHP recursive algorithm to solve the problem
Example: Ask S=1+2+3+4+5+6+......+n originally this question we used to use cyclic accumulation method. In this case, there are two points to be considered in the recursive approach:
1) Can the problem be translated into a recursive form of the description;
2) Whether there is a boundary condition for the end of recursion.
It is clear that the two conditions of recursion are:
1) s (n) =s (n-1) +n
2) s (1) =1
So the source program is:
int progression (int n) {
int res;
if (n=1) Res=1 else res=progression (n-1) +n;
return res;
}
4. Application of recursion
Middle sequence Traversal binary tree
void Inorder (Bintree T) {
if (T) {
Inorder (T->lchild);
printf ("%c", t->data);
Inorder (T->rchild);
}
}