Two methods of realizing a tree structure

xiaoxiao2021-03-06  40

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 "),

(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, 'Trees 1-1-1-1');

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

Field description

ID, record ID number

ParentID, recorded parent record ID (0 is root record)

Topic, record title

Display program

Sequence tree:

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

/* Database Connectivity */

mysql_connect ();

MySQL_SELECT_DB ('Tree');

/ * Tree display recursive function * /

Function Tree ($ PARENTID = 0) {

/ * Execute SQL query, get the title and ID * /

$ SQL = "SELECT TOPIC, ID from trentid = $ pentid Order by id ASC";

$ r = mysql_query ($ SQL);

/ * Indent * /

ECHO ("

    ");

    While ($ ra = mysql_fetch_row ($ r)) {

    / * Display record title * /

    ECHO ('

  • '. $ ra [0]. '');

    / * Recursive call * /

    Tree ($ RA [1]);

    }

    echo ("");

    }

    Tree ();

    ?>

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

    Reverse tree:

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

    /* Database Connectivity */

    mysql_connect ();

    MySQL_SELECT_DB ('Tree'); / * Tree display recursive function * /

    Function Tree ($ PARENTID = 0) {

    / * Execute SQL query, get the title and ID * /

    $ SQL = "SELECT TOPIC, ID from Tree1 Where Parentid = $ ParentId Order By ID DESC";

    $ r = mysql_query ($ SQL);

    / * Indent * /

    ECHO ("

      ");

      While ($ ra = mysql_fetch_row ($ r)) {

      / * Display record title * /

      ECHO ('

    • '. $ ra [0]. '');

      / * Recursive call * /

      Tree ($ RA [1]);

      }

      echo ("");

      }

      Tree ();

      ?>

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

      Insert a data program

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

      /* Database Connectivity */

      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 flag recorded in the data structure in a field of sequential position in the entire tree. Features are high display speed and efficiency. However, in the case of the structure of a single tree, the data write efficiency is insufficient. And the algorithm inserted, inserted, deleted records is too complex, so it is usually arranged in reverse sequence.

      Data structure (as an example of mysql)

      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 (50) Default Null,

      Primary key (`ID`),

      Key `Parentid` (` Parentid`),

      Key `rootid` (` rootid`)

      ) Type = Myisam

      INSERT INTO `Tree2`,` 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, '树 1-1-1-1');

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

      Display program

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

      /* Database Connectivity */

      mysql_connect ();

      MySQL_SELECT_DB ('Tree');

      / * Out of all root record ID * /

      $ SQL = "SELECT ID from Tree2 where ParentId = 0 Order By ID DESC";

      $ r = mysql_query ($ SQL);

      ECHO ("

        ");

        $ lay = 0;

        While ($ ra = mysql_fetch_row ($ r)) {

        ECHO ("

          ");

          / * Select all records of this tree and sort it according to the ORDERS field * /

          $ SQL = "SELECT TOPIC, LAYER from Tree2 Where rootid = $ ra [0] Order by Orders;

          $ r1 = mysql_query ($ SQL);

          While ($ ra1 = mysql_fetch_row ($ r 1)) {

          / * Corridation 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 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 subduction * /

            $ PARENTID = 5; // Father Record ID

            / * Remove the root record ID, the parent record indented level, the parent record sequence location * /

            $ SQL = "SELECT ROOTID, LAYER, ORDERS from Tree2 where id = $ parentid";

            List ($ ROOTID, $ Layer, $ ORDERS) = mysql_fetch_row (MySQL_QUERY ($ SQL));

            / * ORDERS value recorded after updating the insertion position * /

            $ SQL = "Update Tree2 Set Orders = Orders 1 Where Orders> $ Orders";

            mysql_query ($ SQL);

            / * Insert Record * /

            $ SQL = "INSERT INTO TRE2 (Rootid, Parentid, Orders, Layer, Topic) Values ​​($ ROOTID, $ PARENTID,". ",", "($ Layer 1).", ' 2-1-1-2 ') ";

            MySQL_QUERY ($ SQL);?>

转载请注明原文地址:https://www.9cbs.com/read-66782.html

New Post(0)