Two ways to implement tree structure _php tutorial

Source: Internet
Author: User
Two ways to implement tree structure 1. Recursive method
Recursion refers to the invocation of itself explicitly in a function.
The use of recursive method to achieve the characteristics of the tree structure is to write data faster, the display speed is slow (especially in the case of branches/layers of the tree is more obvious). Suitable for cases where the amount of data written is large and the structure of the tree is complex.
Data structure (MySQL for 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

Sequential tree:

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

/* Database connection */
Mysql_connect ();
mysql_select_db (' tree ');

/* Recursive function for tree display */
function tree ($parentid = 0) {
/* Execute SQL query to 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 in */
Echo ("
      ");
      while ($ra = Mysql_fetch_row ($rs)) {
      /* Show record title */
      Echo ('
    • '. $ra [0]. '
    • ');
      /* Recursive call */
      Tree ($ra [1]);
      }
      Echo ("
");
}
Tree ();
?>

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


Reverse Tree:

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

/* Database connection */
Mysql_connect ();
mysql_select_db (' tree ');

/* Recursive function for tree display */
function tree ($parentid = 0) {
/* Execute SQL query to 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 in */
Echo ("
      ");
      while ($ra = Mysql_fetch_row ($rs)) {
      /* Show record title */
      Echo ('
    • '. $ra [0]. '
    • ');
      /* Recursive call */
      Tree ($ra [1]);
      }
      Echo ("
");
}
Tree ();
?>

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


Inserting a 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 data structure that marks the order of the records throughout the tree. It is characterized by high speed and efficient display. However, in the case of the complex structure of single tree, the data writing efficiency is not enough. Moreover, the algorithm for inserting and deleting records is too complex, so it is usually arranged in reverse order.

Data structure (MySQL for 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 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 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 ("
          ", $ra 1[1]-$lay));
          }elseif ($ra 1[1]< $lay) {
          Echo (Str_repeat ("
        ", $lay-$ra 1[1]));
        }
        /* Record display */
        Echo ("$ra 1[1]> $lay");
        Echo ("
      • $ra 1[0]
      • ");
        $lay = $ra 1[1];
        }
        Echo ("
      ");
      }
      Echo ("
");
?>

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


Inserting a 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 insert location 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);? >

http://www.bkjia.com/PHPjc/314792.html www.bkjia.com true http://www.bkjia.com/PHPjc/314792.html techarticle two ways to implement tree structure 1. Recursive recursion refers to the invocation of itself explicitly in a function. The use of recursive method to achieve the characteristics of the tree structure is to write data faster, display speed ...

  • 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.