About PHP recursive algorithm and application method Introduction _php Example

Source: Internet
Author: User

PHP as the first technology to develop a dynamic page Web, we must keep in mind the basics of it, so that can help 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 calling subroutine a statement, the system saves some of the necessary field data and then executes a goto statement similar to the basic language and jumps to subroutine a (for simplicity, I ignore the process of parameter passing). When subroutine a executes to the calling subroutine B statement, the system practices as above and jumps to subroutine B. After subroutine B finishes all the statements, jump back to subroutine a call the next statement of the subroutine B statement (I also ignored the return value processing) subroutine a after execution, skip to the main program call subroutine a statement of the next statement, the main program execution to the end. To make a comparison: when I eat (the main program) eating halfway, someone calls me (Execute subroutine a), speaking of half, the phone rang again (Execute subroutine b), I just have to pick up the phone, and then talk to someone, and finally finish the meal (I eat the food is also tired of J).

2. Recognize recursive functions

We all learned math induction in high School, PHP recursive algorithms such as:

Ask N! We can put n! This definition means 3! , we must first find out 2! , Request 2! , we must first ask 1! , request 1! , you must first ask for 0! , and 0!=1, so 1!=0!*1=1, and then seek 2!,3!. Respectively, we can observe that, in addition to calculating 0! Subroutine, other subroutines are basically similar, we can design a subroutine:

int factorial (int i) {
int res;
Res=factorial (I-1) *i;
return res;
}
Then when executing the main program statement s=factorial (3), the factorial (3) is executed, but the factorial (2) is executed in the execution of factorial (3), and then you should be aware that factorial (3) and factorial (2) Although it is the same code snippet, in memory its data area is two copies! The execution of factorial (2) will invoke factorial (1), and factorial (0) when executing factorial (1), and each time a factorial function is invoked, it adds a data area to the memory. So these duplicate functions can be interpreted as a function of many different names, but we have a problem with this function, and when we execute factorial (0), it calls factorial (-1) ... Cause a dead loop, that is, in the factorial function, we want to ensure that the function is not called at the appropriate time, that is, the res=factorial (I-1) *i is not executed, this call statement. 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 the use of PHP recursive algorithm to solve the problem

For example: Ask S=1+2+3+4+5+6+......+n originally this question we used to use cyclic accumulation method. And here, if you want to use the recursive method, you have to consider two points:
1) Whether the problem can be converted into a recursive form of description;
2 whether there is a recursive end of the boundary conditions.

There are obviously two conditions for recursion:

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, the application of recursion

Sequential traversal of binary tree

void Inorder (Bintree T) {
if (T) {
Inorder (T->lchild);
printf ("%c", t->data);
Inorder (T->rchild);
}
}

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.