国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > PHP > 正文

php遍歷樹(shù)的常用方法匯總

2024-05-04 23:36:25
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

這篇文章主要介紹了php遍歷樹(shù)的常用方法,實(shí)例分析了php常用的三種遍歷樹(shù)的技巧,需要的朋友可以參考下

本文實(shí)例講述了php遍歷樹(shù)的常用方法。分享給大家供大家參考。具體如下:

一、遞歸的深度優(yōu)先的算法:

 

 
  1. <?php 
  2. define('DS', DIRECTORY_SEPARATOR); 
  3. function rec_list_files($from = '.'
  4. if(!is_dir($from)) { 
  5. return array(); 
  6. $files = array(); 
  7. if($dh = opendir($from)) 
  8. while(false !== ($file = readdir($dh))) { 
  9. if($file == '.' || $file == '..') { 
  10. continue
  11. $path = $from . DS . $file
  12.  
  13. if (is_file($path)) { 
  14. $files[] = $path
  15. $files = array_merge($files, rec_list_files($path)); 
  16. closedir($dh); 
  17. return $files
  18. function profile($func$trydir
  19. $mem1 = memory_get_usage(); 
  20. echo '<pre>----------------------- Test run for '.$func.'() '
  21. flush(); 
  22. $time_start = microtime(true); 
  23. $list = $func($trydir); 
  24. //print_r($list); 
  25. $time = microtime(true) - $time_start
  26. echo 'Finished : '.count($list).' files</pre>'
  27. $mem2 = memory_get_peak_usage(); 
  28. printf('<pre>Max memory for '.$func.'() : %0.2f kbytes Running time for '.$func.'() : %0.f s</pre>'
  29. ($mem2-$mem1)/1024.0, $time); 
  30. return $list
  31. profile('rec_list_files'"D:/www/server"); 
  32. ?> 

二、遞歸的深度優(yōu)先的算法(用了一個(gè)棧來(lái)實(shí)現(xiàn))

 

 
  1. <?php 
  2. define('DS', DIRECTORY_SEPARATOR); 
  3. function deep_first_list_files($from = '.'
  4. if(!is_dir($from)) { 
  5. return false; 
  6. $files = array(); 
  7. $dirs = array($from); 
  8. while(NULL !== ($dir = array_pop($dirs))) { 
  9. if$dh = opendir($dir)) { 
  10. while( false !== ($file = readdir($dh))) { 
  11. if($file == '.' || $file == '..') { 
  12. continue
  13. $path = $dir . DS . $file
  14. if(is_dir($path)) { 
  15. $dirs[] = $path
  16. else { 
  17. $files[] = $path
  18. closedir($dh); 
  19. return $files
  20. function profile($func$trydir
  21. $mem1 = memory_get_usage(); 
  22. echo '<pre>----------------------- Test run for '.$func.'() '
  23. flush(); 
  24. $time_start = microtime(true); 
  25. $list = $func($trydir); 
  26. //print_r($list); 
  27. $time = microtime(true) - $time_start
  28. echo 'Finished : '.count($list).' files</pre>'
  29. $mem2 = memory_get_peak_usage(); 
  30. printf('<pre>Max memory for '.$func.'() : %0.2f kbytes Running time for '.$func.'() : %0.f s</pre>'
  31. ($mem2-$mem1)/1024.0, $time); 
  32. return $list
  33. profile('deep_first_list_files'"D:/www/server"); 
  34. ?> 

三、非遞歸的廣度優(yōu)先算法(用了一個(gè)隊(duì)列來(lái)實(shí)現(xiàn))

 

 
  1. <?php 
  2. define('DS', DIRECTORY_SEPARATOR); 
  3. function breadth_first_files($from = '.') { 
  4. $queue = array(rtrim($from, DS).DS);// normalize all paths 
  5. $files = array(); 
  6. while($base = array_shift($queue )) { 
  7. if (($handle = opendir($base))) { 
  8. while (($child = readdir($handle)) !== false) { 
  9. if$child == '.' || $child == '..') { 
  10. continue
  11. if (is_dir($base.$child)) { 
  12. $combined_path = $base.$child.DS; 
  13. array_push($queue$combined_path); 
  14. else { 
  15. $files[] = $base.$child
  16. closedir($handle); 
  17. // else unable to open directory => NEXT CHILD 
  18. return $files// end of tree, file not found 
  19. function profile($func$trydir
  20. $mem1 = memory_get_usage(); 
  21. echo '<pre>----------------------- Test run for '.$func.'() '
  22. flush(); 
  23. $time_start = microtime(true); 
  24. $list = $func($trydir); 
  25. //print_r($list); 
  26. $time = microtime(true) - $time_start
  27. echo 'Finished : '.count($list).' files</pre>'
  28. $mem2 = memory_get_peak_usage(); 
  29. printf('<pre>Max memory for '.$func.'() : %0.2f kbytes Running time for '.$func.'() : %0.f s</pre>'
  30. ($mem2-$mem1)/1024.0, $time); 
  31. return $list
  32. profile('breadth_first_files'"D:/www/server"); 
  33. ?> 

希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 朝阳区| 荥阳市| 明光市| 封丘县| 平顺县| 屯门区| 宜阳县| 陇川县| 闵行区| 电白县| 万盛区| 平利县| 河北区| 台北市| 安多县| 会理县| 晋宁县| 宝鸡市| 聂荣县| 平陆县| 精河县| 沐川县| 莱芜市| 南宫市| 隆昌县| 阿荣旗| 育儿| 雷波县| 新疆| 海门市| 青龙| 淄博市| 洛隆县| 平罗县| 普陀区| 勃利县| 扎鲁特旗| 松原市| 长海县| 卢湾区| 澎湖县|