Two methods of implementing the tree-like structure

Source: Internet
Author: User
Tags insert key sql mysql php code query sort
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)

Code:--------------------------------------------------------------------------------
CREATE TABLE ' Tree1 ' (
' ID ' tinyint (3) unsigned not NULL auto_increment,
' ParentID ' tinyint (3) unsigned not NULL default ' 0 ',
' topic ' varchar default NULL,
PRIMARY KEY (' id '),
KEY ' ParentID ' (' ParentID ')
) Type=myisam;

INSERT into ' tree1 ' (' id ', ' parentid ', ' topic ') VALUES
(1, 0, ' tree 1 '),
(2,0, ' Tree 2 '),
(3,0, ' Tree 3 '),
(4,2, ' Tree 2-1 '),
(5,4, ' Tree 2-1-1 '),
(6,2, ' Tree 2-2 '),
(7,1, ' Tree 1-1 '),
(8,1, ' Tree 1-2 '),
(9,1, ' Tree 1-3 '),
(10,8, ' Tree 1-2-1 '),
(11,7, ' Tree 1-1-1 '),
(12,11, ' tree 1-1-1-1 ');
--------------------------------------------------------------------------------


Field description
ID, ID number of the record
ParentID, the record's parent record ID (0 is the root record)
Topic, the display title of the record

Show programs

Order Tree:

PHP Code:--------------------------------------------------------------------------------

?
/* Database Connection * *
Mysql_connect ();
mysql_select_db (' 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 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 ();
?>

--------------------------------------------------------------------------------


Reverse Tree:

PHP Code:--------------------------------------------------------------------------------

?
/* Database Connection * *
Mysql_connect ();
mysql_select_db (' 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 ();
?>

--------------------------------------------------------------------------------


Insert Data Program

PHP Code:--------------------------------------------------------------------------------

?
/* Database Connection * *
Mysql_connect ();
mysql_select_db (' tree ');
$sql = "INSERT into trees (Topic,parentid) VALUES (' Tree 3-1 ', 3);";
mysql_query ($sql);
?>

--------------------------------------------------------------------------------


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.

Data structure (take MySQL as an example)

Code:--------------------------------------------------------------------------------
CREATE TABLE ' Tree2 ' (
' ID ' tinyint (3) unsigned not NULL auto_increment,
' ParentID ' tinyint (3) unsigned not NULL default ' 0 ',
' Rootid ' tinyint (3) unsigned not NULL default ' 0 ',
' Layer ' tinyint (3) unsigned not NULL default ' 0 ',
' Orders ' tinyint (3) unsigned not NULL default ' 0 ',
' topic ' varchar default NULL,
PRIMARY KEY (' id '),
KEY ' ParentID ' (' ParentID '),
KEY ' Rootid ' (' Rootid ')
) Type=myisam

INSERT into ' tree2 ' (' id ', ' parentid ', ' rootid ', ' layer ', ' orders ', ' topic ') VALUES
(1,0,1,0,0, ' tree 1 '),
(2,0,2,0,0, ' Tree 2 '),
(3,0,3,0,0, ' Tree 3 '),
(4,2,2,1,2, ' Tree 2-1 '),
(5,4,2,2,3, ' Tree 2-1-1 '),
(6,2,2,1,1, ' Tree 2-2 '),
(7,1,1,1,4, ' Tree 1-1 '),
(8,1,1,1,2, ' Tree 1-2 '),
(9,1,1,1,1, ' Tree 1-3 '),
(10,8,1,2,3, ' Tree 1-2-1 '),
(11,7,1,2,5, ' Tree 1-1-1 '),
(12,11,1,3,6, ' tree 1-1-1-1 ');
--------------------------------------------------------------------------------


Show programs

PHP Code:--------------------------------------------------------------------------------

?
/* Database Connection * *
Mysql_connect ();
mysql_select_db (' tree ');

/* Select all root record ID * *
$sql = "SELECT id from tree2 where parentid = 0 ORDER BY id DESC";
$rs = mysql_query ($sql);
Echo ("<ul>");
$lay = 0;
while ($ra = Mysql_fetch_row ($rs)) {
Echo ("<ul>");
/* Select All records for this tree and sort by orders field
$sql = "Select Topic,layer from tree2 where rootid = $ra [0] Order by orders";
$rs 1 = mysql_query ($sql);
while ($ra 1 = mysql_fetch_row ($rs 1)) {
/* Indent Display * *
if ($ra 1[1]> $lay) {
Echo (Str_repeat ("<ul>", $ra 1[1]-$lay));
}elseif ($ra 1[1]< $lay) {
Echo (Str_repeat ("</ul>", $lay-$ra 1[1]);
}
/* Record Display * *
Echo ("$ra 1[1]> $lay");
Echo ("<li> $ra 1[0]</li>");
$lay = $ra 1[1];
}
Echo ("</ul>");
}
Echo ("</ul>");
?>

--------------------------------------------------------------------------------


Insert Data Program

PHP Code:--------------------------------------------------------------------------------

?
/* Database Connection * *
Mysql_connect ();
mysql_select_db (' tree ');

/* 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);? >



Contact Us

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.

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.