[分享]PHP兑现树的递归展示
程序员文章站
2022-05-17 07:59:04
...
[分享]PHP实现树的递归展示
原文博客地址: http://blog.csdn.net/lgg201/article/details/7973971
用法:
测试输出:
原文博客地址: 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 0 ) { $i = 0; /* 前缀格式: "父级连线" ["宽空白符" "父级连线" ...] "宽空白符" */ $string .= ($is_last & 1 $data ) { /* 当前节点及所有祖先是否尾节点标记 */ $tmp_is_last = $is_last 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])
上一篇: 如何写好一个PHP的类?_PHP教程