PHP recursive algorithm instance Program-PHP source code

Source: Internet
Author: User
Although the recursive algorithm is not a high-performance algorithm, we use a lot of recursion, such as Directory Traversal or tree structure, it will be useful, let's take a look at the analysis and examples of recursive algorithms. Although the recursive algorithm is not a high-performance algorithm, we use a lot of recursion, such as Directory Traversal or tree structure, it will be useful, let's take a look at the analysis and examples of recursive algorithms.

Script ec (2); script


A recursive function is a self-called function that calls itself directly or indirectly in the function body. However, you must set the self-called conditions. If the conditions are met, the function itself is called, if not, terminate the self-call of the function and return the current process's Main Control Right to the previous function for execution. This may be difficult to understand.

Function test ($ n ){
Echo $ n. "";
If ($ n> 0 ){
Test ($ N-1 );
} Else {
Echo "<-> ";
}
Echo $ n .""
}
Test (2)

In this example, the final output result is 2 1 0 <-> 0 1 2

I will explain why the output is like this.

Step 1: Execute test (2), echo 2, and then execute test (1) Because 2> 0. There is still echo 2 that can be executed later.
Step 2: Execute test (1), echo 1, and then execute test (0) because 1> 0. Similarly, echo 1 is not executed in time.
Step 3: Execute test (0), echo 0, execute test (0), echo 0. At this time, the 0> 0 conditions are not met, and the test () function is not executed, but echo "<->", and execute the echo 0

At this time, the function no longer calls itself, and begins to return the main control right of the process to the previous function for execution, that is, to execute the last echo that has just been output by all the test () functions, the first layer of 0 is 1, that is, the first layer of output 1 is 2, that is, the output 2 is not a mountain layer, so the output content is 2 1 0 <-> 0 1 2

Example

, We need to traverse all directories in a folder and list all the files in it. PHP itself has a readdir function, but it can only read the current directory. According to this function, I wrote another function to meet my needs. The principle of a function is very simple. It is mainly called recursively.

Function file_list ($ path ){
If ($ handle = opendir ($ path )){
While (false! ==( $ File = readdir ($ handle ))){
If ($ file! = "." & $ File! = ".."){
If (is_dir ($ path. "/". $ file )){
Echo $ path. ":". $ file ."
"; // Remove this line to display all non-directory files
File_list ($ path. "/". $ file );
} Else {
Echo $ path. ":". $ file ."
";
}
}
}
}
}

Example

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.