Two methods of implementing the tree-like structure
1. Recursive method
Recursion refers to an explicit invocation of itself in a function.
The feature of the tree structure by recursion is that it is faster to write data and slower to display (especially in the case of branches/levels of trees). Applies to the case that the data volume is large and the structure of the tree is complex.
Data structure (take MySQL as an example)
/* Tree Display recursive function * *
function tree ($parentid = 0) {
/* Execute SQL query, get the title and id*/of the record
$sql = "Select Topic,id from tree1 where parentid = $parentid ORDER by ID ASC";
$rs = mysql_query ($sql);
* * Indent/
Echo ("<ul>");
while ($ra = Mysql_fetch_row ($rs)) {
/* Display Record title * *
Echo (' <li> '. $ra [0]. </li> ');
/* Recursive call * *
Tree ($ra [1]);
}
Echo ("</ul>");
}
Tree ();
?>
/* Tree Display recursive function * *
function tree ($parentid = 0) {
/* Execute SQL query, get the title and id*/of the record
$sql = "Select Topic,id from tree1 where parentid = $parentid order BY id DESC";
$rs = mysql_query ($sql);
* * Indent/
Echo ("<ul>");
while ($ra = Mysql_fetch_row ($rs)) {
/* Display Record title * *
Echo (' <li> '. $ra [0]. </li> ');
/* Recursive call * *
Tree ($ra [1]);
}
Echo ("</ul>");
}
Tree ();
?>
2. Sort Field Method
This method is achieved by adding a field in the data structure that marks the ordinal position of the entire tree. The characteristic is the display speed and the efficiency is high. However, in the case of the complex structure of a single tree, the data write efficiency is insufficient. Moreover, the algorithm of inserting and deleting records is too complex, so it is usually arranged in reverse order.
/* Insert Root record * *
$sql = "INSERT into TREE2 (topic) VALUES (' Tree 5 ')";
mysql_query ($sql);
$sql = "Update tree2 set Rootid = id where id =". mysql_insert_id ();
mysql_query ($sql);
/* Insert Child record * *
$parentid = 5;//Parent Record ID
/* Remove root record ID, parent record indent level, parent record order position
$sql = "Select Rootid,layer,orders from tree2 where id = $parentid";
List ($rootid, $layer, $orders) = Mysql_fetch_row (mysql_query ($sql));
/* The orders value recorded after the insertion position is updated * *
$sql = "Update tree2 Set orders = orders + 1 where orders > $orders";
mysql_query ($sql);
/* Insert Record * *
$sql = "INSERT into Tree2 (rootid,parentid,orders,layer,topic) VALUES ($rootid, $parentid,". $orders + 1). ",". ($layer + 1). ", ' Tree 2-1-1-2 ')";
mysql_query ($sql);? >
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.