完成樹(shù)狀結(jié)構(gòu)的2種方法
發(fā)表時(shí)間:2024-06-04 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]實(shí)現(xiàn)樹(shù)狀結(jié)構(gòu)的兩種方法1。遞歸法遞歸是指在函數(shù)中顯式的調(diào)用它自身。利用遞歸法實(shí)現(xiàn)樹(shù)狀結(jié)構(gòu)的特點(diǎn)是寫(xiě)入數(shù)據(jù)速度較快,顯示速度較慢(在樹(shù)的分支/層次較多的情況下尤其明顯)。適用與寫(xiě)入數(shù)據(jù)量大,樹(shù)的結(jié)構(gòu)復(fù)雜的情況下。數(shù)據(jù)結(jié)構(gòu)(以mysql為例)代碼:---------------------------...
實(shí)現(xiàn)樹(shù)狀結(jié)構(gòu)的兩種方法1。遞歸法
遞歸是指在函數(shù)中顯式的調(diào)用它自身。
利用遞歸法實(shí)現(xiàn)樹(shù)狀結(jié)構(gòu)的特點(diǎn)是寫(xiě)入數(shù)據(jù)速度較快,顯示速度較慢(在樹(shù)的分支/層次較多的情況下尤其明顯)。適用與寫(xiě)入數(shù)據(jù)量大,樹(shù)的結(jié)構(gòu)復(fù)雜的情況下。
數(shù)據(jù)結(jié)構(gòu)(以mysql為例)
代碼:--------------------------------------------------------------------------------
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,'樹(shù)1'),
(2,0,'樹(shù)2'),
(3,0,'樹(shù)3'),
(4,2,'樹(shù)2-1'),
(5,4,'樹(shù)2-1-1'),
(6,2,'樹(shù)2-2'),
(7,1,'樹(shù)1-1'),
(8,1,'樹(shù)1-2'),
(9,1,'樹(shù)1-3'),
(10,8,'樹(shù)1-2-1'),
(11,7,'樹(shù)1-1-1'),
(12,11,'樹(shù)1-1-1-1');
--------------------------------------------------------------------------------
字段說(shuō)明
id,記錄的id號(hào)
parentid,記錄的父記錄id(為0則為根記錄)
topic,記錄的顯示標(biāo)題
顯示程序
順序樹(shù):
PHP代碼:--------------------------------------------------------------------------------
<?
/* 數(shù)據(jù)庫(kù)連接 */
mysql_connect();
mysql_select_db('tree');
/* 樹(shù)狀顯示的遞歸函數(shù) */
function tree($parentid = 0) {
/*執(zhí)行sql查詢(xún),獲取記錄的標(biāo)題和id*/
$sql = "select topic,id from tree1 where parentid = $parentid order by id asc";
$rs = mysql_query($sql);
/* 縮進(jìn)*/
echo("<ul>");
while($ra = mysql_fetch_row($rs)) {
/* 顯示記錄標(biāo)題 */
echo('<li>'.$ra[0].'</li>');
/* 遞歸調(diào)用 */
tree($ra[1]);
}
echo("</ul>");
}
tree();
?>
--------------------------------------------------------------------------------
逆序樹(shù):
PHP代碼:--------------------------------------------------------------------------------
<?
/* 數(shù)據(jù)庫(kù)連接 */
mysql_connect();
mysql_select_db('tree');
/* 樹(shù)狀顯示的遞歸函數(shù) */
function tree($parentid = 0) {
/*執(zhí)行sql查詢(xún),獲取記錄的標(biāo)題和id*/
$sql = "select topic,id from tree1 where parentid = $parentid order by id desc";
$rs = mysql_query($sql);
/* 縮進(jìn)*/
echo("<ul>");
while($ra = mysql_fetch_row($rs)) {
/* 顯示記錄標(biāo)題 */
echo('<li>'.$ra[0].'</li>');
/* 遞歸調(diào)用 */
tree($ra[1]);
}
echo("</ul>");
}
tree();
?>
--------------------------------------------------------------------------------
插入數(shù)據(jù)程序
PHP代碼:--------------------------------------------------------------------------------
<?
/* 數(shù)據(jù)庫(kù)連接 */
mysql_connect();
mysql_select_db('tree');
$sql = "insert into tree (topic,parentid) values('樹(shù)3-1',3);";
mysql_query($sql);
?>
--------------------------------------------------------------------------------
2。排序字段法
此方法是通過(guò)在數(shù)據(jù)結(jié)構(gòu)中增加一個(gè)標(biāo)志記錄在整個(gè)樹(shù)中的順序位置的字段來(lái)實(shí)現(xiàn)的。特點(diǎn)是顯示速度和效率高。但在單個(gè)樹(shù)的結(jié)構(gòu)復(fù)雜的情況下,數(shù)據(jù)寫(xiě)入效率有所不足。而且順序排列時(shí)候,插入,刪除記錄的算法過(guò)于復(fù)雜,故通常用逆序排列。
數(shù)據(jù)結(jié)構(gòu)(以mysql為例)
代碼:--------------------------------------------------------------------------------
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` (`id`, `parentid`, `rootid`, `layer`, `orders`, `topic`) VALUES
(1,0,1,0,0,'樹(shù)1'),
(2,0,2,0,0,'樹(shù)2'),
(3,0,3,0,0,'樹(shù)3'),
(4,2,2,1,2,'樹(shù)2-1'),
(5,4,2,2,3,'樹(shù)2-1-1'),
(6,2,2,1,1,'樹(shù)2-2'),
(7,1,1,1,4,'樹(shù)1-1'),
(8,1,1,1,2,'樹(shù)1-2'),
(9,1,1,1,1,'樹(shù)1-3'),
(10,8,1,2,3,'樹(shù)1-2-1'),
(11,7,1,2,5,'樹(shù)1-1-1'),
(12,11,1,3,6,'樹(shù)1-1-1-1');
--------------------------------------------------------------------------------
顯示程序
PHP代碼:--------------------------------------------------------------------------------
<?
/* 數(shù)據(jù)庫(kù)連接 */
mysql_connect();
mysql_select_db('tree');
/* 選出所有根記錄id */
$sql = "select id from tree2 where parentid = 0 order by id desc";
$rs = mysql_query($sql);
echo("<ul>");
$lay = 0;
while($ra = mysql_fetch_row($rs)) {
echo("<ul>");
/* 選出此樹(shù)所有記錄,并按orders字段排序 */
$sql = "select topic,layer from tree2 where rootid = $ra[0] order by orders";
$rs1 = mysql_query($sql);
while($ra1 = mysql_fetch_row($rs1)) {
/* 縮進(jìn)顯示 */
if($ra1[1]>$lay) {
echo(str_repeat("<ul>",$ra1[1]-$lay));
}elseif($ra1[1]<$lay) {
echo(str_repeat("</ul>",$lay-$ra1[1]));
}
/* 記錄顯示 */
//echo("$ra1[1]>$lay");
echo("<li>$ra1[0]</li>");
$lay = $ra1[1];
}
echo("</ul>");
}
echo("</ul>");
?>
--------------------------------------------------------------------------------
插入數(shù)據(jù)程序
PHP代碼:--------------------------------------------------------------------------------
<?
/* 數(shù)據(jù)庫(kù)連接 */
mysql_connect();
mysql_select_db('tree');
/* 插入根記錄 */
$sql = "insert into tree2 (topic) values ('樹(shù)5')";
mysql_query($sql);
$sql = "update tree2 set rootid = id where id = ".mysql_insert_id();
mysql_query($sql);
/* 插入子記錄 */
$parentid = 5;//父記錄id
/* 取出 根記錄id,父記錄縮進(jìn)層次,父記錄順序位置 */
$sql = "select rootid,layer,orders from tree2 where id = $parentid";
list($rootid,$layer,$orders) = mysql_fetch_row(mysql_query($sql));
/* 更新插入位置后記錄的orders值 */
$sql = "update tree2 set orders = orders + 1 where orders > $orders";
mysql_query($sql);
/* 插入記錄 */
$sql = "insert into tree2 (rootid,parentid,orders,layer,topic) values ($rootid,$parentid,".($orders+1).",".($layer+1).",'樹(shù)2-1-1-2')";
mysql_query($sql);?>