三种遍历树的方法

三种遍历树的方法

    树的概念在开发里面是很重要的一部分,xml的文档对象模型(DOM)就是一棵树,文件夹文件的结构也是一棵树。遍历树是开发中经常要遇到的一个问题,比如,要找出DOM里面的img 标签的个数,就要遍历一棵DOM树。要在一个目录里面查找是否有一个文件也要用到遍历这个目录。在这里我们以遍历文件为例,说明遍历树的三种基本的方法:
递归深度优先算法,非递归深度优先算法,非递归广度优先算法。
    这些算法是我们在项目中经常重复的一些算法,我感觉我写程序以来,我做的大多数算法都用了大二学的那本数据结构,有些时候,只是改装一些一些算法,有些时候也只是把几种算法合并一下,也许这是为什么数据结构这本书这样重要的原因。
先看代码:
<? 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  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  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(
' rec_list_files ' ,   " D:\www\server " );
profile(
' deep_first_list_files ' ,   " D:\www\server " );
profile(
' breadth_first_files ' ,   " D:\www\server " );
?>
rec_list_files 是递归的深度优先的算法,这个是用一个简单的函数递归来实现,用array_merge 来合并数组
deep_first_list_files 是非递归的深度优先的算法,用了一个栈来实现。
breadth_first_files 是非递归的广度优先算法,用了一个队列来实现。
顺便说一句,php中的数组,可以做为hashtable,queue,stack,普通数组,甚至做树也是可以的。运行的结果:
-----------------------
Test run for rec_list_files() ...Finished : 1868 files
Max memory for rec_list_files() : 496.93 kbytes Running time for rec_list_files() : 9.231678 s
----------------------- 
Test run for deep_first_list_files() ...Finished : 1868 files
Max memory for deep_first_list_files() : 432.41 kbytes Running time for deep_first_list_files() : 3.940216 s
----------------------- 
Test run for breadth_first_files() ...Finished : 1868 files
Max memory for breadth_first_files() : 432.55 kbytes Running time for breadth_first_files() : 3.749125 s
第二种和第三种方法的效率和内存消耗差别不大,但是第一种递归调用消耗的内存和时间都要大很多,有时候为了效率,可能采用非递归的实现方式比较的好。
posted @ 2008-06-28 00:47 暮夏 阅读( ...) 评论( ...) 编辑 收藏
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值