Two methods for implementing tree structure using PHP program

Source: Internet
Author: User
1. 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 and 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: CREATETABLE 'tree1 '('id' tinyi 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

Display program

Sequence tree:

PHP:

   /* 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 ("

    ");
    While ($ ra = mysql_fetch_row ($ rs )){
    /* Display record title */
    Echo ('
  • '. $ Ra [0].'
  • ');
    /* Recursive call */
    Tree ($ ra [1]);
    }
    Echo ("
");
}
Tree ();
?>

Reverse sequence tree:

PHP:

   /* 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 ("

    ");
    While ($ ra = mysql_fetch_row ($ rs )){
    /* Display record title */
    Echo ('
  • '. $ Ra [0].'
  • ');
    /* Recursive call */
    Tree ($ ra [1]);
    }
    Echo ("
");
}
Tree ();
?>

Insert data program

PHP:

   /* Database connection */
Mysql_connect ();
Mysql_select_db ('tree ');
$ SQL = "insert into tree (topic, parentid) values ('Tree 3-1 ', 3 );";
Mysql_query ($ SQL );
?>

  2. sort fields
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. In addition, the algorithm for inserting and deleting records is too complex in order, so records are 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 ',

'Player', '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:

   /* 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 ("

    ");
    $ Lay = 0;
    While ($ ra = mysql_fetch_row ($ rs )){
    Echo ("
      ");
      /* 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 ("

        ", $ Ra1 [1]-$ lay ));
        } Elseif ($ ra1 [1] <$ lay ){
        Echo (str_repeat ("
      ", $ Lay-$ ra1 [1]);
      }
      /* Record Display */
      // Echo ("$ ra1 [1]> $ lay ");
      Echo ("
    • $ Ra1 [0]
    • ");
      $ Lay = $ ra1 [1];
      }
      Echo ("
    ");
    }
    Echo ("
");
?>

Insert data program

PHP:

   /* 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.