Two methods for implementing tree structure

Source: Internet
Author: User

Two methods for implementing tree structure

1. Recursion
Recursion refers to explicitly calling itself in a function.
Recursive tree structure is characterized by fast data writing speed and slow display speed (especially when there are many branches/layers of the tree ). Applicable to scenarios where a large amount of data is written and the tree structure 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 (50) 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 '),
(, 'Tree 1-2-1 '),
(, 'Tree 1-1-1 '),
(, 'Tree 1-1-1-1 ');
--------------------------------------------------------------------------------

Field description
ID, ID of the record
Parentid, the record's parent Record ID (0 is the root record)
Topic, the title of the record

DisplayProgram

Sequence tree:

PHP code :--------------------------------------------------------------------------------

<?
/* Database connection */
Mysql_connect ();
Mysql_select_db ('tree ');

/* Recursive function displayed in the tree */
Function tree ($ parentid = 0 ){
/* Execute an SQL query to obtain 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 Sequence tree:

PHP code :--------------------------------------------------------------------------------

<?
/* Database connection */
Mysql_connect ();
Mysql_select_db ('tree ');

/* Recursive function displayed in the tree */
Function tree ($ parentid = 0 ){
/* Execute an SQL query to obtain 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 tree (topic, parentid) values ('tree 3-1 ', 3 );";
Mysql_query ($ SQL );
?>

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

2. Sort Field Method
This method is implemented by adding a field in the sequence position of the Flag record in the entire tree in the data structure. It features high display speed and efficiency. However, when the structure of a single tree is complex, the Data Writing efficiency is insufficient. And insert or delete records in sequence.AlgorithmIt 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 ',
'Lay' tinyint (3) unsigned not null default '0 ',
'Orders 'tinyint (3) unsigned not null default '0 ',
'Topic 'varchar (50) default null,
Primary Key ('id '),
Key 'parentid' ('parentid '),
Key 'rootid' ('rootid ')
) Type = MyISAM

Insert into 'tree2' ('id', 'parentid', 'rootid', 'layer', 'Orders ', 'topic') Values
(, 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 '),
(, 4, 'tree 1-1 '),
(, 2, 'tree 1-2 '),
(, 1, 'tree 1-3 '),
(, 3, 'tree 1-2-1 '),
(, 5, 'tree 1-1-1 '),
(, 6, 'tree 1-1-1-1 ');
--------------------------------------------------------------------------------

Display program

PHP code :--------------------------------------------------------------------------------

<?
/* Database connection */
Mysql_connect ();
Mysql_select_db ('tree ');

/* Select all root record IDs */
$ 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 in the tree and sort them by the orders field */
$ SQL = "select topic, layer from tree2 where rootid = $ RA [0] Order by orders ";
$ RS1 = mysql_query ($ SQL );
While ($ ra1 = mysql_fetch_row ($ RS1 )){
/* Indent display */
If ($ ra1 [1]> $ lay ){
Echo (str_repeat ("<ul>", $ ra1 [1]-$ lay ));
} Elseif ($ ra1 [1] <$ lay ){
Echo (str_repeat ("</ul>", $ lay-$ ra1 [1]);
}
/* Record display */
// Echo ("$ ra1 [1]> $ lay ");
Echo ("<li> $ ra1 [0] </LI> ");
$ Lay = $ ra1 [1];
}
Echo ("</ul> ");
}
Echo ("</ul> ");
?>

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

Insert Data Program

PHP code :--------------------------------------------------------------------------------

<?
/* Database connection */
Mysql_connect ();
Mysql_select_db ('tree ');

/* Insert the 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 sub-records */
$ Parentid = 5; // parent Record ID
/* Retrieve the root Record ID, parent record indent level, and parent record sequence position */
$ SQL = "select rootid, layer, orders from tree2 where id = $ parentid ";
List ($ rootid, $ layer, $ orders) = mysql_fetch_row (mysql_query ($ SQL ));
/* Update the orders value recorded after the insert position */
$ 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-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.