PHP three common techniques for traversing trees

Source: Internet
Author: User
This article mainly introduces PHP commonly used three kinds of traversal tree skills, interested in the friend's reference, I hope to be helpful to everyone.

This article describes the common methods of the PHP traversal tree, as follows:

First, recursive depth-first algorithm:

<?phpdefine (' DS ', directory_separator), function rec_list_files ($from = '. ')  {if (!is_dir ($from)) {return array ();  } $files = Array (); if ($dh = Opendir ($from)) {while (false!== ($file = Readdir ($DH))) {if ($file = = '. ' | | $file = = ' ... ')      {continue; } $path = $from. Ds.             $file;      if (Is_file ($path)) {$files [] = $path;    } $files = Array_merge ($files, Rec_list_files ($path));  } closedir ($DH); } return $files;}  function profile ($func, $trydir) {$mem 1 = memory_get_usage (); Echo ' <pre>-----------------------Test run for '. $func. '  () ';  Flush ();  $time _start = Microtime (true);  $list = $func ($trydir);  Print_r ($list);  $time = Microtime (True)-$time _start;  Echo ' finished: '. Count ($list). ' Files</pre> ';  $mem 2 = Memory_get_peak_usage (); printf (' <pre>max memory for '. $func. ' ():%0.2f Kbytes Running time for '. $func. '  ():%0.f s</pre> ', ($mem 2-$mem 1)/1024.0, $time); return $list;} Profile (' ReC_list_files ', "D:\www\server");? >

Second, recursive depth-first algorithm (implemented with a stack)

<?phpdefine (' DS ', directory_separator), function deep_first_list_files ($from = '. ')  {if (!is_dir ($from)) {return false;  } $files = Array ();  $dirs = Array ($from);        while (NULL!== ($dir = Array_pop ($dirs))) {if ($dh = Opendir ($dir)) {while (false!== ($file = Readdir ($DH))) { if ($file = = '. ' | | $file = = ' ... ')        {continue; } $path = $dir. Ds.        $file;        if (Is_dir ($path)) {$dirs [] = $path;        } else {$files [] = $path;    }} closedir ($DH); }} return $files;}  function profile ($func, $trydir) {$mem 1 = memory_get_usage (); Echo ' <pre>-----------------------Test run for '. $func. '  () ';  Flush ();  $time _start = Microtime (true);  $list = $func ($trydir);  Print_r ($list);  $time = Microtime (True)-$time _start;  Echo ' finished: '. Count ($list). ' Files</pre> ';  $mem 2 = Memory_get_peak_usage (); printf (' <pre>max memory for '. $func. ' ():%0.2f Kbytes Running time for '. $func. ' ():%0.F S</pre> ', ($mem 2-$mem 1)/1024.0, $time); return $list;} Profile (' deep_first_list_files ', ' D:\www\server ');? >

Three, a non-recursive breadth-first algorithm (implemented with a queue)

<?phpdefine (' DS ', directory_separator), function breadth_first_files ($from = '. ') {$queue = Array (RTrim ($from, DS).  DS);//normalize all paths $files = Array (); while ($base = Array_shift ($queue)) {if ($handle = Opendir ($base))} {while (($child = Readdir ($handle))!== FAL SE) {if ($child = = '. ' | | $child = = ' ... ')        {continue; } if (Is_dir ($base. $child)) {$combined _path = $base. $child.          DS;        Array_push ($queue, $combined _path);        } else {$files [] = $base. $child;    }} closedir ($handle); }//Else unable to open directory = NEXT child} return $files;  End of tree, file not found}function profile ($func, $trydir) {$mem 1 = memory_get_usage (); Echo ' <pre>-----------------------Test run for '. $func. '  () ';  Flush ();  $time _start = Microtime (true);  $list = $func ($trydir);  Print_r ($list);  $time = Microtime (True)-$time _start; Echo ' finished: '. Count ($list). ' Files</pre> '; $mem 2 = Memory_get_peak_usage (); printf (' <pre>max memory for '. $func. ' ():%0.2f Kbytes Running time for '. $func. '  ():%0.f s</pre> ', ($mem 2-$mem 1)/1024.0, $time); return $list;} Profile (' breadth_first_files ', ' D:\www\server ');? >

Summary: The above is the entire content of this article, I hope to be able to help you learn.

Related recommendations:

PHP implementation of integrated Discuz user approach

PHP Processing session Function Summary sharing

PHP four basic sorting algorithms and two search algorithms

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.