Two methods of realizing a tree structure

xiaoxiao2021-03-06  41

Original from: http://blog.9cbs.net/ziyou_jo/archive/2005/01/20/260456.aspx 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, ' 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,' 1-3 '), (10, 8,' Tree 1-2-1 '), (11, 7,' Tree 1-1-1 '), (12, 11,' Tree 1-1-1-1) '); ----------------------------------------------- -------------------------------- Field Description ID, recorded ID number ParentID, recorded parent record ID (for 0 is a root record) Topic, recorded display title display program sequence tree: PHP code: ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ -------------------------------------------------- "); while $ ra = mysql_fetch_row ($ r)) {/ * Display Record Title * / Echo ('

  • '. $ ra [0]. ''); / * Recursive call * / Tree ($ RA [ 1]);} echo ("");} Tree ();?>

    -------------------------------------------------- ------------------------------ Reverse sediment tree: PHP code: -------------- -------------------------------------------------- ---------------- "); while ($ ra = mysql_fetch_row ($ = mysql_fetch_row ($ r)) {/ * Display Record Title * / ECHO ('

  • '. $ Ra [0]. ' '); / * Recursive call * / Tree ($ ra [1]);} echo ("");} Tree ();?> ------------- -------------------------------------------------- ---------------- Insert data program PHP code: ------------------------- -------------------------------------------------- --- -------------------------------------------- --------------- --------------------- 2. Sort fields This method is implemented by adding a flag recorded in the data structure in the sequence location of 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 (`Parenti), Key` Parentid` (`Parentid`), Key` rootid` (`rootid`). Type = myisam insert Into` Tree2` (`ID`,` Parentid`, `rootid`,` topic`) Values ​​(1,0,1,0,0 , 'Tree 1'), (2, 0, 2, 0, 0, '), (3, 0, 3, 0, 0,' Tree 3 '), (4, 2, 2, 1, 2, 'Tree 2-1'), (5, 4, 2, 2, 3, 'Trees 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,' Trees 1-1-1 '), ( 12, 11, 1, 3, 6, 'Tree 1-1-1-1'); ---------------------------- -------------------------------------------------- - Display program PHP code: ----------------------------------- -------------------------------------------- "); $ lay = 0; while ($ RA = mysql_fetch_row ($ r)) {echo ("

      "); / * Select all records of this tree and sequenctions 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 ($ RS1)) {/ * indent display * / if ($ RA1 [1]> $ lay) { Echo (Str_Repeat ("

        ", $ RA1 [1] - $ Lay);} elseif ($ ra1 [1] <$ {echo (Str_Repeat ("", $ Lay- $ RA1 [1]));} / * Record display * / // echo ("$ ra1 [1]> $ lay"); echo ("
      • $ ra1 [0] "); $ lay = $ ra1 [1];} echo ("");} echo ("");?> -------------------- -------------------------------------------------- ---------- Insert data program PHP code: -------------------------------- ----------------------------------------------- $ ORDERS"; mysql_query ($ sql); / * Insert record * / $ sql = "Insert Into Tree2 (Rootid, Parentid, Orders, Layer, Topic) Values ​​($ ROOTID, $ PARENTID,". ",", "($ Layer 1) ", 'Tree 2-1-1-2')"; mysql_query ($ SQL);?>

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

    New Post(0)