search
HomeBackend DevelopmentPHP TutorialInfinite recursion tree display_PHP tutorial

Infinite recursion tree display_PHP tutorial

Jul 13, 2016 pm 05:53 PM
parsephptreeexhibitunlimitedTreeCommentalgorithmnodemenurecursionlimit

[php]
/**
* Infinite level (limited by the tail node description algorithm, see tree_parse comment for details) recursive menu
* author: selfimpr
* blog: http://blog.csdn.net/lgg201
* mail: lgg860911@yahoo.com.cn
​*/

define('MAX_NODES', 3); /* Maximum number of child nodes */
define('MAX_NODE_INDEX', MAX_NODES - 1); /* Maximum index value of child nodes */
define('NAME_FMT', 'name-%08d'); /* Node content output format string */

/* Tree node data structure */
define('K_ID', 'id');
define('K_NAME', 'name');
define('K_CHILD', 'children');

/* Output the assembly characters used in construction */
define('PREFIX_TOP',                                                                                                                                                                                                                                                                                       through define('PREFIX_BOTTOM', '┗'); /* The identifier of the last child node of each parent node */
define('PREFIX_MIDDLE', '┠'); /* Identifiers of all nodes that are not in the above two cases */
define('PREFIX_LINE', '┇'); /* The ligature of the ancestor node */
define('SPACE',             ' ');                                                                                                                                                                                                                         define('WIDE_SPACE', str_repeat(SPACE, 4)); /* Wide blank placeholder, in order to make the hierarchy of the tree clear */


/**
* data_build
* Construct a node
* @param mixed $id Node id
* @param mixed $is_leaf Whether it is a leaf
* @access public
* @return void
​*/
function node_build($id, $is_leaf = FALSE) {
Return array(
K_ID => $id,
K_NAME => sprintf(NAME_FMT, $id),
K_CHILD => $is_leaf ? NULL : array(),
);
}
/**
* tree_build
* Construct a tree (the number of child nodes of each node in the tree is determined by MAX_NODES)
* @param mixed $datas The tree reference to be returned
* @param mixed $id Starting ID
* @param mixed $level The level of the tree
* @access public
* @return void
​*/
function tree_build(&$datas, &$id, $level) {
If ( $level $is_leaf = $level == 1;
$i = -1;
$next_level = $level - 1;
while ( ++ $i $data = node_build($id++, $is_leaf);
If ( !$is_leaf )
Tree_build($data[K_CHILD], $id, $next_level);
         array_push($datas, $data);
}  
}

/**
* node_str
* Output a node’s own information
 * @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
* Output the glyph of a node
* @param mixed $string Return the string of the result (pass by value)
* @param mixed $level Current depth
* @param mixed $i The index (subscript) of the current node in the parent node
* @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
* Output the prefix of a node
* @param mixed $string Return the string of the result (pass by value)
* @param mixed $level Current depth
* @param mixed $is_last Whether all ancestor nodes of the current node (including) have tail node tags
* @access public
* @return void
​*/ 
function node_prefix(&$string, $level, $is_last) { 
    if ( $level > 0 ) { 
        $i  = 0; 
        /* 前缀格式: "父级连线" ["宽空白符" "父级连线" ...] "宽空白符" */ 
        $string .= ($is_last & 1         while ( ++ $i             $string .= WIDE_SPACE . ($is_last & 1         $string .= WIDE_SPACE; 
    } 

/**
* node_out
* Output a node
* @param mixed $string Return the string of the result (pass by value)
* @param mixed $data The node data to be processed
* @param mixed $level Node depth
* @param mixed $i The index (subscript) of the node in the parent node
* @param mixed $is_last Whether all ancestor nodes of the current node (including) have tail node tags
* @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) $max_index = count($datas) - 1;
/* Process all nodes in this layer */
foreach ( $datas as $i => $data ) {
             /* Whether the current node and all ancestors are marked as tail nodes */
$tmp_is_last = $is_last /* Output the current node */
Node_out($string, $data, $level, $i, $tmp_is_last);
           /* If there are child nodes, recurse the child nodes */
If ( is_array($data[K_CHILD]) && !emptyempty($data[K_CHILD]) )
Tree_parse($string, $data[K_CHILD], $level + 1, $tmp_is_last);
}  
}

/* Calculate 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 floatval(sprintf('%d.%d', $info[$type . '.tv_sec'], $info[$type . '.tv_usec']));
}
/* 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.6fsn%20s%0.6fsn%20s%0.6fsn%20s%d byten",
         $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);
}
/* 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]) usage($argv[0]);


$datas = array();
$id = 1;
$string = '';
$level = intval($argv[1]);

/* Initial construction of 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 results */
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:
*/

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477981.htmlTechArticle[php] ?php /** * Infinite level (limited by the tail node description algorithm, see tree_parse comment for details) Recursive menu * author: selfimpr * blog: http://blog.csdn.net/lgg201 * mail: lgg860911@yahoo.com.cn...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain the concept of session locking.Explain the concept of session locking.Apr 29, 2025 am 12:39 AM

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

Are there any alternatives to PHP sessions?Are there any alternatives to PHP sessions?Apr 29, 2025 am 12:36 AM

Alternatives to PHP sessions include Cookies, Token-based Authentication, Database-based Sessions, and Redis/Memcached. 1.Cookies manage sessions by storing data on the client, which is simple but low in security. 2.Token-based Authentication uses tokens to verify users, which is highly secure but requires additional logic. 3.Database-basedSessions stores data in the database, which has good scalability but may affect performance. 4. Redis/Memcached uses distributed cache to improve performance and scalability, but requires additional matching

Define the term 'session hijacking' in the context of PHP.Define the term 'session hijacking' in the context of PHP.Apr 29, 2025 am 12:33 AM

Sessionhijacking refers to an attacker impersonating a user by obtaining the user's sessionID. Prevention methods include: 1) encrypting communication using HTTPS; 2) verifying the source of the sessionID; 3) using a secure sessionID generation algorithm; 4) regularly updating the sessionID.

What is the full form of PHP?What is the full form of PHP?Apr 28, 2025 pm 04:58 PM

The article discusses PHP, detailing its full form, main uses in web development, comparison with Python and Java, and its ease of learning for beginners.

How does PHP handle form data?How does PHP handle form data?Apr 28, 2025 pm 04:57 PM

PHP handles form data using $\_POST and $\_GET superglobals, with security ensured through validation, sanitization, and secure database interactions.

What is the difference between PHP and ASP.NET?What is the difference between PHP and ASP.NET?Apr 28, 2025 pm 04:56 PM

The article compares PHP and ASP.NET, focusing on their suitability for large-scale web applications, performance differences, and security features. Both are viable for large projects, but PHP is open-source and platform-independent, while ASP.NET,

Is PHP a case-sensitive language?Is PHP a case-sensitive language?Apr 28, 2025 pm 04:55 PM

PHP's case sensitivity varies: functions are insensitive, while variables and classes are sensitive. Best practices include consistent naming and using case-insensitive functions for comparisons.

How do you redirect a page in PHP?How do you redirect a page in PHP?Apr 28, 2025 pm 04:54 PM

The article discusses various methods for page redirection in PHP, focusing on the header() function and addressing common issues like "headers already sent" errors.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software