1. the method of delivery refers to explicitly calling itself in a function. The implementation of the tree structure using the progressive method is characterized by fast data writing speed and slow display speed (especially when there are many branches/layers of the tree ). Large amount of practical and written data
1. return method
A progressive callback is an explicit call to itself in a function.
The implementation of the tree structure using the progressive method is characterized by fast data writing speed and slow display speed (especially when there are many branches/layers of the tree ). It is useful when 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 clarification
Id, id of the record
Parentid, the record's parent record id (0 is the root record)
Topic, the title of the record
Display program
Sequence tree:
PHP:
/* Database connection */
Mysql_connect ();
Mysql_select_db ('tree ');
/* Tree-based return function */
Function tree ($ parentid = 0 ){
/* Perform 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 ('
');
While ($ ra = mysql_fetch_row ($ rs )){
/* Display record title */
Echo ('
- '. $ Ra [0].'
');
/* Progressive call */
Tree ($ ra [1]);
}
Echo ('
');
}
Tree ();
?>
Reverse sequence tree:
PHP:
/* Database connection */
Mysql_connect ();
Mysql_select_db ('tree ');
/* Tree-based return function */
Function tree ($ parentid = 0 ){
/* Perform 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 ('
');
While ($ ra = mysql_fetch_row ($ rs )){
/* Display record title */
Echo ('
- '. $ Ra [0].'
');
/* Progressive call */