• 递归的两种模式——取得文件目录树


    递归是个神奇的东西,有时明白,有时候不明白。

    写的多了便会总结。

    总结两种常用的模式:

    • 队列递归
    • 级联递归

    简单的说,一个是返回平坦的队列,一个是返回有层级关系的Map。以取得文件的目录树为例:

     1 #    取得文档整体目录树    
     2 function tree( $path ,  $cutfix = 0 , $regexp = '/.*/' ,  $flat = 0 , $result = array() ,  $level = 0 ){
     3     $level = $level+1;
     4     $dirs  = scandir( $path );
     5     foreach($dirs as $d) {
     6         $realpath = joinp( $path ,$d );
     7         $current  =  array();
     8         if( !is_dir($realpath) ){ continue ;}
     9         if ($d == '.' || $d == '..') {}
    10         else if( preg_match( $regexp , $d ) ){
    11             if( is_int( $flat ) ){
    12                 $id = $this->gid('tree');
    13                 $result[] = array( 'id'=>$id,  'pid'=> $flat, 'name' => $d , 'path' => substr( $realpath ,$cutfix ),'level'=>$level );
    14                 $result   = $result + $this -> tree( $realpath  ,$cutfix , $regexp, $id , $result ,$level);
    15             }else{
    16                 $result[] =  array( 'name' => $d , 'path' => substr( $realpath ,$cutfix ) , 'child' => $this->tree( $realpath ,$cutfix , $regexp  , $flat ,$result  )  );
    17             }
    18         }
    19     }
    20     return $result;
    21 }

    队列递归返回:

    [
    •  
      {
      • id1,
      • pid0,
      • name"first-1",
      • path"/first-1",
      • level1
      },
    •  
      {
      • id2,
      • pid1,
      • name"sec-do",
      • path"/first-1/sec-do",
      • level2
      },
    •  
      {
      • id3,
      • pid0,
      • name"first-2",
      • path"/first-2",
      • level1
      },
    •  
      {
      • id4,
      • pid3,
      • name"sec-2-1",
      • path"/first-2/sec-2-1",
      • level2
      },
    •  
      {
      • id5,
      • pid3,
      • name"sec-2-2",
      • path"/first-2/sec-2-2",
      • level2
      },
    •  
      {
      • id6,
      • pid5,
      • name"thr",
      • path"/first-2/sec-2-2/thr",
      • level3
      }
    ]

    级联递归返回

    [
    •  
      {
      • name"first-1",
      • path"/first-1",
      • child
         
        [
        •  
          {
          • name"sec-do",
          • path"/first-1/sec-do",
          • child: [ ]
          }
        ]
      },
    •  
      {
      • name"first-2",
      • path"/first-2",
      • child
         
        [
        •  
          {
          • name"first-1",
          • path"/first-1",
          • child
             
            [
            •  
              {
              • name"sec-do",
              • path"/first-1/sec-do",
              • child: [ ]
              }
            ]
          },
        •  
          {
          • name"sec-2-1",
          • path"/first-2/sec-2-1",
          • child
             
            [
            •  
              {
              • name"first-1",
              • path"/first-1",
              • child
                 
                [
                •  
                  {
                  • name"sec-do",
                  • path"/first-1/sec-do",
                  • child: [ ]
                  }
                ]
              }
            ]
          },
        •  
          {
          • name"sec-2-2",
          • path"/first-2/sec-2-2",
          • child
             
            [
            •  
              {
              • name"first-1",
              • path"/first-1",
              • child
                 
                [
                •  
                  {
                  • name"sec-do",
                  • path"/first-1/sec-do",
                  • child: [ ]
                  }
                ]
              },
            •  
              {
              • name"sec-2-1",
              • path"/first-2/sec-2-1",
              • child
                 
                [
                •  
                  {
                  • name"first-1",
                  • path"/first-1",
                  • child
                     
                    [
                    •  
                      {
                      • name"sec-do",
                      • path"/first-1/sec-do",
                      • child: [ ]
                      }
                    ]
                  }
                ]
              },
            •  
              {
              • name"thr",
              • path"/first-2/sec-2-2/thr",
              • child
                 
                [
                •  
                  {
                  • name"first-1",
                  • path"/first-1",
                  • child
                     
                    [
                    •  
                      {
                      • name"sec-do",
                      • path"/first-1/sec-do",
                      • child: [ ]
                      }
                    ]
                  },
                •  
                  {
                  • name"sec-2-1",
                  • path"/first-2/sec-2-1",
                  • child
                     
                    [
                    •  
                      {
                      • name"first-1",
                      • path"/first-1",
                      • child
                         
                        [
                        •  
                          {
                          • name"sec-do",
                          • path"/first-1/sec-do",
                          • child: [ ]
                          }
                        ]
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  • 相关阅读:
    管程|| 同步与互斥的实现策略
    sqlserver 用户定义表类型
    C# Post Get 方式发送请求
    linux centos Supervisor守护.netcore进程安装配置
    linux cenos开放端口
    SqlServer DbHelper
    C# 执行js
    sql 多行、一行 互转
    sql 删除完全表中完全重复的数据保留一条
    sql 行列互转
  • 原文地址:https://www.cnblogs.com/webooxx/p/2777362.html
Copyright © 2020-2023  润新知