Share Recursive presentation of a PHP implementation tree
Original Blog Address: http://blog.csdn.net/lgg201/article/details/7973971
Usage:
PHP Code
usage:php tree-display.php
Test output:
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-0000 0018[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-0000002 7[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-00000 036[36]┠name-00000037[37]┠name-00000038[38]┗name-00000039[39]resource usage[level:3, node number:39]: C Lock 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 * Constructs a tree (the number of child nodes per node in the tree is determined by max_nodes) * @param mixed $datas The tree reference to be returned * @param mixed $id start ID * @param Mixed $level Tree Hierarchy * @access public * @return void */function tree_build (& $datas, & $id, $level) {if ($leve L < 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 * Outputs the information of a node itself * @param mixed $string The string that returns the result (reference pass value) * @param mixed $data node data * @access public * @re turn void */function node_str (& $string, $data) {$string. = sprintf ('%s[%d] ', $data [K_name], $data [k_id]);} /** * node_sign * Outputs a node glyph * @param mixed $string The string that returns the result (reference pass value) * @param mixed $level Current Depth * @param mixed $i The index of the current node in the parent node (subscript) * @access public * @return void */function node_sign (& $string, $lev El, $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 * Outputs a node prefix * @param mixed $string The string that returns the result (reference pass value) * @param mixed $level Current Depth * @param mix Ed $is _last the current node (including) whether all ancestor nodes are marked with a tail node * @access public * @return void */function node_prefix (& $string, $level, $is _last {if ($level > 0) {$i = 0; /* Prefix format: "Parent lines" ["Wide Whitespace" "Parent line" ...] "Wide whitespace" */$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 * outputs a node * @param mixed $string The string that returns the result (reference value) * @param mixed $data node data to process * @param mixed $level Node Depth * @param the index of the mixed $i node in the parent node (subscript) * @param mixed $is _last the current node (including) whether all ancestor nodes are marked with a tail node * @acces s public * @return void */function node_out (& $string, $data, $level, $i, $is _last) {/* processing prefix string: Ancestor's connector and blank */No De_prefix ($string, $level, $is _last); /* Handle the identity symbol for this node */node_sign ($string, $level, $i); /* Processing this node data information */NODE_STR ($string, $data); /* Append newline */$string. = "\ n";} /** * tree_parse * Output a tree * 1. Because the $is_last of an integral type is used as a marker of whether the ancestor is a tail node, it supports a maximum of php_int_max depth * 2. If you need to extend, fix $is_last data type and check method * @param mixed $string Return the result of the string (reference value) * @param mixed $datas The tree data to be processed * @param int $level Depth of current processing * @param int $is _last Current Depth all ancestors whether tail node tag * @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; /* Handles all nodes of this layer */foreach ($datas as $i + = $data) {/* current node and all ancestors whether tail node tag */$tmp _is_last = $is _last << 1 | 1 & $i = = $max _index; /* Output Current node */node_out ($string, $data, $level, $i, $tmp _is_last); /* If there are child nodes, the recursive child node */if (Is_array ($data [k_child]) &&!empty ($data [K_child])) Tree_parse ($string , $data [K_child], $level + 1, $tmp _is_last); }}/* compute the actual number of nodes */function N_node ($n, $s) {$sum = 0; while ($n > 0) $sum + = Pow ($s, $n-); return $sum;} /* Calculate ruage time */function ru_time ($info, $type) {return $info [$type. '. Tv_sec '] + $info [$type. '. Tv_usec ']/1000000;} /* Output Resource Usage */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 ($r B, ' Ru_utime '), ' Memory usage: ', $me-$MB);} /* Usage */function usage ($cmd) {printf ("Usage: \n%s
\ n ", $cmd); Exit;} /* Test entry function */function run () {global $ARGC, $argv; if ($ARGC! = 2 | | intval ($ARGV [1]) < 1) usage ($argv [0]); $datas = Array (); $id = 1; $string = "; $level = Intval ($argv [1]); /* Initial Construction Test tree */Tree_build ($datas, $id, $level); $clock _begin = Microtime (TRUE); $memory _begin = Memory_get_usage (); $rusage _begin = Getrusage (); /* Parse tree */Tree_parse ($string, $datas); $rusage _end = Getrusage (); $memory _end = Memory_get_usage (); $clock _end = Microtime (TRUE); /* Output result */echo $string. "\ n"; Resource_usage ($level, N_node ($level, max_nodes), $clock _begin, $clock _end, $memory _begin, $memory _end, $rusage _begin, $rusage _end);} /* Execute entry function */run ();/* Local variables: * tab-width:4 * c-basic-offset:4 * indent-tabs-mode:t * End: */