php遍历树的常用方法汇总


Posted in PHP onJune 18, 2015

本文实例讲述了php遍历树的常用方法。分享给大家供大家参考。具体如下:

一、递归的深度优先的算法:

<?php
define('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)
{
  $mem1 = 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>';
  $mem2 = memory_get_peak_usage();
  printf('<pre>Max memory for '.$func.'() : %0.2f kbytes Running time for '.$func.'() : %0.f s</pre>',
  ($mem2-$mem1)/1024.0, $time);
  return $list;
}
profile('rec_list_files', "D:\www\server");
?>

二、递归的深度优先的算法(用了一个栈来实现)

<?php
define('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)
{
  $mem1 = 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>';
  $mem2 = memory_get_peak_usage();
  printf('<pre>Max memory for '.$func.'() : %0.2f kbytes Running time for '.$func.'() : %0.f s</pre>',
  ($mem2-$mem1)/1024.0, $time);
  return $list;
}
profile('deep_first_list_files', "D:\www\server");
?>

三、非递归的广度优先算法(用了一个队列来实现)

<?php
define('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)) !== false) {
        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)
{
  $mem1 = 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>';
  $mem2 = memory_get_peak_usage();
  printf('<pre>Max memory for '.$func.'() : %0.2f kbytes Running time for '.$func.'() : %0.f s</pre>',
  ($mem2-$mem1)/1024.0, $time);
  return $list;
}
profile('breadth_first_files', "D:\www\server");
?>

希望本文所述对大家的php程序设计有所帮助。

PHP 相关文章推荐
利用 window_onload 实现select默认选择
Oct 09 PHP
用php或asp创建网页桌面快捷方式的代码
Mar 23 PHP
PHP中如何实现常用邮箱的基本判断
Jan 07 PHP
jquery+php实现导出datatables插件数据到excel的方法
Jul 06 PHP
PHP利用APC模块实现大文件上传进度条的方法
Oct 29 PHP
php阿拉伯数字转中文人民币大写
Dec 21 PHP
Smarty模板常见的简单应用分析
Nov 15 PHP
PHP制作登录异常ip检测功能的实例代码
Nov 16 PHP
php查找字符串中第一个非0的位置截取
Feb 27 PHP
tp框架(thinkPHP)实现三次登陆密码错误之后锁定账号功能示例
May 24 PHP
Smarty模板变量与调节器实例详解
Jul 20 PHP
PHP使用Http Post请求发送Json对象数据代码解析
Jul 16 PHP
php编写简单的文章发布程序
Jun 18 #PHP
试用php中oci8扩展
Jun 18 #PHP
浅谈PDO的rowCount函数
Jun 18 #PHP
基于PHP实现的事件机制实例分析
Jun 18 #PHP
php使用MySQL保存session会话的方法
Jun 18 #PHP
php判断表是否存在的方法
Jun 18 #PHP
PHP实现HTTP断点续传的方法
Jun 17 #PHP
You might like
php的控制语句
2006/10/09 PHP
PHP类的静态(static)方法和静态(static)变量使用介绍
2012/02/19 PHP
浅析Yii中使用RBAC的完全指南(用户角色权限控制)
2013/06/20 PHP
如何在Ubuntu下启动Apache的Rewrite功能
2013/07/05 PHP
php魔术函数__call()用法实例分析
2015/02/13 PHP
Symfony2开发之控制器用法实例分析
2016/02/05 PHP
thinkPHP数据查询常用方法总结【select,find,getField,query】
2017/03/15 PHP
PHP微信PC二维码登陆的实现思路
2017/07/13 PHP
PHPStorm 2020.1 调试 Nodejs的多种方法详解
2020/09/17 NodeJs
dojo学习第一天 Tab选项卡 实现
2011/08/28 Javascript
jQuery使用一个按钮控制图片的伸缩实现思路
2013/04/19 Javascript
jQuery $.extend()用法总结
2014/06/15 Javascript
JavaScript操作select元素和option的实例代码
2016/01/29 Javascript
Node.js项目中调用JavaScript的EJS模板库的方法
2016/03/11 Javascript
Bootstrap 模态框实例插件案例分析
2016/12/28 Javascript
浅谈Angular.js中使用$watch监听模型变化
2017/01/10 Javascript
使用nodejs实现JSON文件自动转Excel的工具(推荐)
2020/06/24 NodeJs
微信小程序轮播图swiper代码详解
2020/12/01 Javascript
Python通过递归遍历出集合中所有元素的方法
2015/02/25 Python
Python求导数的方法
2015/05/09 Python
pytest中文文档之编写断言
2019/09/12 Python
Python+Opencv实现把图片、视频互转的示例
2020/12/17 Python
CSS3实现缺角矩形,折角矩形以及缺角边框
2019/12/20 HTML / CSS
Zavvi美国:英国娱乐之家
2017/03/19 全球购物
欧舒丹俄罗斯官方网站:L’OCCITANE俄罗斯
2019/11/22 全球购物
后勤人员自我评价怎么写
2013/09/19 职场文书
电子商务专业自我鉴定
2013/12/18 职场文书
业务部经理岗位职责
2014/01/04 职场文书
女方婚礼新郎答谢词
2014/01/11 职场文书
思想品德自我评价
2014/02/04 职场文书
开展党的群众路线教育实践活动方案
2014/02/05 职场文书
生育关怀行动实施方案
2014/03/26 职场文书
草房子读书笔记
2015/06/29 职场文书
病房管理制度范本
2015/08/06 职场文书
Vue实现tab导航栏并支持左右滑动功能
2021/06/28 Vue.js
解决vue-router的beforeRouteUpdate不能触发
2022/04/14 Vue.js