[分享]PHP兌現樹的遞迴展示

來源:互聯網
上載者:User
[分享]PHP實現樹的遞迴展示
原文部落格地址: http://blog.csdn.net/lgg201/article/details/7973971

用法:
PHP code
usage: php tree-display.php 


測試輸出:
PHP code
$ php tree-display.php 3 name-00000001[1]┇    ┠ name-00000002[2]┇    ┇    ┠ name-00000003[3]┇    ┇    ┠ name-00000004[4]┇    ┇    ┗ name-00000005[5]┇    ┠ name-00000006[6]┇    ┇    ┠ name-00000007[7]┇    ┇    ┠ name-00000008[8]┇    ┇    ┗ name-00000009[9]┇    ┗ name-00000010[10]┇         ┠ name-00000011[11]┇         ┠ name-00000012[12]┇         ┗ name-00000013[13]┠ name-00000014[14]┇    ┠ name-00000015[15]┇    ┇    ┠ name-00000016[16]┇    ┇    ┠ name-00000017[17]┇    ┇    ┗ name-00000018[18]┇    ┠ name-00000019[19]┇    ┇    ┠ name-00000020[20]┇    ┇    ┠ name-00000021[21]┇    ┇    ┗ name-00000022[22]┇    ┗ name-00000023[23]┇         ┠ name-00000024[24]┇         ┠ name-00000025[25]┇         ┗ name-00000026[26]┗ name-00000027[27]     ┠ name-00000028[28]     ┇    ┠ name-00000029[29]     ┇    ┠ name-00000030[30]     ┇    ┗ name-00000031[31]     ┠ name-00000032[32]     ┇    ┠ name-00000033[33]     ┇    ┠ name-00000034[34]     ┇    ┗ name-00000035[35]     ┗ name-00000036[36]          ┠ name-00000037[37]          ┠ name-00000038[38]          ┗ name-00000039[39]resource usage[level: 3, node number: 39]:         clock time: 0.001967s        system cpu: 0.000169s          user cpu: 0.001013s      memory usage: 7208 byte


PHP code
 $id,         K_NAME    => sprintf(NAME_FMT, $id),         K_CHILD    => $is_leaf ? NULL : array(),     );}/** * tree_build  * 構造一棵樹(樹中每個節點的子節點數由MAX_NODES確定) * @param mixed $datas     要返回的樹引用 * @param mixed $id     起始ID * @param mixed $level     樹的層級 * @access public * @return void */function tree_build(&$datas, &$id, $level) {    if ( $level < 1 ) return ;    $is_leaf    = $level == 1;    $i            = -1;    $next_level    = $level - 1;    while ( ++ $i < MAX_NODES ) {        $data    = node_build($id ++, $is_leaf);        if ( !$is_leaf )             tree_build($data[K_CHILD], $id, $next_level);        array_push($datas, $data);    }}/** * node_str  * 輸出一個節點自身的資訊 * @param mixed $string 返回結果的字串(引用傳值) * @param mixed $data     節點資料 * @access public * @return void */function node_str(&$string, $data) {    $string    .= sprintf(' %s[%d]', $data[K_NAME], $data[K_ID]);}/** * node_sign  * 輸出一個節點的標誌符號 * @param mixed $string    返回結果的字串(引用傳值) * @param mixed $level     當前深度 * @param mixed $i         當前節點在父節點中的索引(下標) * @access public * @return void */function node_sign(&$string, $level, $i) {    switch ( $i ) {        case 0:            $string    .= $level == 0 ? PREFIX_TOP : PREFIX_MIDDLE;            break;        case MAX_NODE_INDEX:            $string    .= PREFIX_BOTTOM;            break;        default:            $string    .= PREFIX_MIDDLE;            break;    }}/** * node_prefix  * 輸出一個節點的首碼 * @param mixed $string     返回結果的字串(引用傳值) * @param mixed $level         當前深度 * @param mixed $is_last     當前節點(含)所有祖先節點是否尾節點標記 * @access public * @return void */function node_prefix(&$string, $level, $is_last) {    if ( $level > 0 ) {        $i    = 0;        /* 首碼格式: "父級連線" ["寬空白符" "父級連線" ...] "寬空白符" */        $string    .= ($is_last & 1 << ($level - $i) ? SPACE : PREFIX_LINE);        while ( ++ $i < $level )             $string    .= WIDE_SPACE . ($is_last & 1 << ($level - $i) ? SPACE : PREFIX_LINE);        $string    .= WIDE_SPACE;    }}/** * node_out  * 輸出一個節點 * @param mixed $string     返回結果的字串(引用傳值) * @param mixed $data         要處理的節點資料 * @param mixed $level         節點深度 * @param mixed $i             節點在父節點中的索引(下標) * @param mixed $is_last     當前節點(含)所有祖先節點是否尾節點標記 * @access public * @return void */function node_out(&$string, $data, $level, $i, $is_last) {    /* 處理前置詞字元串: 祖先的串連符及空白 */    node_prefix($string, $level, $is_last);    /* 處理本節點的標識符號 */    node_sign($string, $level, $i);    /* 處理本節點資料資訊 */    node_str($string, $data);    /* 追加換行 */    $string        .= "\n";}/** * tree_parse  * 輸出一棵樹 *     1. 由於使用了整型的$is_last作為祖先是否尾節點的標記, 所以最多支援PHP_INT_MAX的深度 *     2. 如果需要擴充, 修正$is_last的資料類型及校正方法即可 * @param mixed $string 返回結果的字串(引用傳值) * @param mixed $datas     要處理的樹資料 * @param int $level     當前處理的深度 * @param int $is_last     當前深度所有祖先是否尾節點標記 * @access public * @return void */function tree_parse(&$string, $datas, $level = 0, $is_last = 0) {    if ( !is_array($datas) || count($datas) < 1 ) return ;    $max_index    = count($datas) - 1;    /* 處理本層的所有節點 */    foreach ( $datas as $i => $data ) {        /* 當前節點及所有祖先是否尾節點標記 */        $tmp_is_last    = $is_last << 1 | 1 & $i == $max_index;        /* 輸出當前節點 */        node_out($string, $data, $level, $i, $tmp_is_last);        /* 如果有子節點, 遞迴子節點 */        if ( is_array($data[K_CHILD]) && !empty($data[K_CHILD]) )            tree_parse($string, $data[K_CHILD], $level + 1, $tmp_is_last);    }}/* 計算實際節點數 */function n_node($n, $s) {    $sum    = 0;    while ( $n > 0 )         $sum    += pow($s, $n --);    return $sum;}/* 計算ruage時間 */function ru_time($info, $type) {    return $info[$type . '.tv_sec'] + $info[$type . '.tv_usec'] / 1000000;}/* 輸出資源使用方式 */function resource_usage($lv, $nodes, $cb, $ce, $mb, $me, $rb, $re) {    printf("\nresource usage[level: %d, node number: %d]: \n%20s%0.6fs\n%20s%0.6fs\n%20s%0.6fs\n%20s%d byte\n",         $lv, $nodes,        'clock time: ',        $ce - $cb,         'system cpu: ',        ru_time($re, 'ru_stime') - ru_time($rb, 'ru_stime'),         'user cpu: ',        ru_time($re, 'ru_utime') - ru_time($rb, 'ru_utime'),         'memory usage: ',    $me - $mb);}/* 用法 */function usage($cmd) {    printf("usage: \n%s \n", $cmd);    exit;}/* 測試入口函數 */function run() {    global    $argc, $argv;    if ( $argc != 2 || intval($argv[1]) < 1 )        usage($argv[0]);    $datas    = array();    $id        = 1;    $string    = '';    $level    = intval($argv[1]);    /* 初始構造測試樹 */    tree_build($datas, $id, $level);    $clock_begin    = microtime(TRUE);    $memory_begin    = memory_get_usage();    $rusage_begin    = getrusage();    /* 解析樹 */    tree_parse($string, $datas);    $rusage_end    = getrusage();    $memory_end    = memory_get_usage();    $clock_end    = microtime(TRUE);    /* 輸出結果 */    echo $string . "\n";    resource_usage($level, n_node($level, MAX_NODES),        $clock_begin, $clock_end,         $memory_begin, $memory_end,         $rusage_begin, $rusage_end);}/* 執行入口函數 */run();/* * Local variables: * tab-width: 4 * c-basic-offset: 4 * indent-tabs-mode: t * End: */
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.