Introduction to PHP Recursive Algorithms and application methods

Source: Internet
Author: User

PHP, as the preferred technology for developing dynamic WEB pages, must be kept in mind for its basic knowledge so that it can help programming. Let's take a look at the PHP recursive algorithm.

1. Meanings of calling subprograms:

When the main program is executed to call the sub-program A statement, the system saves some necessary field data, and then runs a goto statement similar to the BASIC language to jump to sub-program A (to make it easier, this process is ignored here ). When subroutine A is executed to call the subroutine B Statement, the system goes to subroutine B as above. After subroutine B executes all the statements, it jumps back to subroutine A to call the next statement of subroutine B (I ignore the returned value processing) after subroutine A is executed, jump back to the next statement of subroutine A called by the main program, and the execution of the main program ends. Make A comparison: When I eat half of my meals (executing the main program), someone calls me (executing subroutine A). When I say half of my speech, the phone starts to ring again (executing subroutine B ), I just need to answer the call first, then talk to someone, and finally finish the meal (I am tired enough to eat J ).

2. Recognize recursive functions

We have learned mathematical induction in high school. PHP recursive algorithms include:

N! We can set n! This definition requires 3 !, We must first find 2 !, Requirement 2 !, You must first calculate 1 !, Requirement 1 !, You must first calculate 0 !, And 0! = 1, so 1! = 0! * 1 = 1, and then calculate 2 !, 3 !. They are represented by functions. We can observe that, except for 0! In addition to subprograms, other subprograms are basically similar. We can design such a subprogram:

Int factorial (int I ){
Int res;
Res = factorial (I-1) * I;
Return res;
}
When the main program statement s = factorial (3) is executed, factorial (3) is executed, but factorial (3) is executed, and factorial (2) is called ), at this time, you should note that, although factorial (3) and factorial (2) are the same code segment, they have two data partitions in the memory! When factorial (2) is executed, factorial (1) is called. When factorial (1) is executed, factorial (0) is called. Each time the factorial function is called, it will add a new data zone in the memory, so these functions that have copied multiple copies can be understood as multiple functions with different names; but we have a problem with this function. When we execute factorial (0), it will call factorial (-1) again )... This creates an endless loop, that is, in the factorial function, we need to ensure that the function is no longer called at the right time, that is, the call statement res = factorial (I-1) * I; is not executed. So the function should be changed:

Int factorial (int I ){
Int res;
If (I> 0) res = factorial (I-1) * I; else res = 1;
Return res;
}
3. How to Use PHP recursive algorithms to solve the problem

For example, calculate s = 1 + 2 + 3 + 4 + 5 + 6 + ...... + N we used to accumulate loops for this problem. To use recursive methods, you must consider the following two points:
1) whether the problem can be converted into a recursive description;
2) whether there is a boundary condition for Recursive termination.

Obviously, both recursive conditions have:

1) s (n) = s (n-1) + n
2) s (1) = 1
So the source code is:

Int progression (int n ){
Int res;
If (n = 1) res = 1 else res = progression (n-1) + n;
Return res;
}
4. Recursive Application

Traversing a binary tree in ascending order

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.