實現樹狀結構的兩種方法
1。遞歸法
遞歸是指在函數中明確的呼叫它本身。
利用遞歸法實現樹狀結構的特徵是寫入資料速度較快,顯示速度較慢(在樹的分支/層次較多的情況下尤其明顯)。適用與寫入資料量大,樹的結構複雜的情況下。
資料結構(以mysql為例)
程式碼:------------------------------- -------------------------------------------------
CREATE TABLE `tree1` (
`id` tinyint(3) unsigned NOT NULL auto_increment,
`parentid` tinyint(3) unsigned NOTULL Ndefault's
KEY `parentid` (`parentid`)
) TYPE=MyISAM;
(1,0,'樹1'),
(2,0,'樹2'),
(3,0,'樹3' ),
(4,2,'樹2-1'),
(5,4,'樹2-1-1'),
(6,2,'樹2-2' ),
(7,1,'樹1-1'),
(8,1,'樹1-2'),
(9,1,'樹1-3'),
(10,8,'樹1-2-1'),
(11,7,'樹1-1-1'),
(12,11,'樹1-1- 1-1');
----------------------------------------- ---------------------------------------
欄位說明
id,記錄的id號
parentid,記錄的父記錄id(為0則為根記錄)
topic,記錄的顯示標題
顯示程式
順序樹:
PHP代碼:-------------------------------------- ------------------------------------------
/* 資料庫連線*/
mysql_connect();
mysql_select_db('tree');
/* 樹狀顯示的遞歸函數*/
function tree($parentid = 0) {
/*執行sql查詢,取得記錄的標題和id*/
$sql = "select topic,id from tree1 where parentid = $parentid order by id asc";
where parentid = $parentid order by id asc";
= mysql_query($sql);
/* 縮排*/
echo("");
");
while($ra = mysql_fetch_row($rs)) { echo('
/* 遞歸給*/
/* 遞歸給*/
🎜> }
echo("
}
tree();
?>
------------ -------------------------------------------------- ------------------
逆序樹:
PHP代碼:----------- -------------------------------------------------- -------------------
/* 資料庫連線*/
mysql_connect();
mysql_select_db('tree ');
/* 樹狀顯示的遞歸函數*/
function tree($parentid = 0) {
/*執行sql查詢,取得記錄的標題和id*/
$sql = "select topic,id from tree1 where parentid = $parentid order by id desc";
$rs = mysql_query($sql);
/* 縮排*/");
while($ra = mysql_fetch_row($rs)) {
/* 顯示記錄標題*/
/* 顯示記錄標題*/
/* 顯示記錄標題*/
li>');
/* 遞歸呼叫*/
tree($ra[1]);
}
🎜> tree();
?>
----------------------------------- ---------------------------------------------
插入資料程式
PHP代碼:---------------------------------- ----------------------------------------------
/* 資料庫連線*/
mysql_connect();
mysql_select_db('tree');
$sql = "insert into tree (topic,parentid) values('33 -1',3);";
mysql_query($sql);
?>
--------------------- -------------------------------------------------- ---------
2。排序字段法
此方法是透過在資料結構中增加一個標誌記錄在整個樹中的順序位置的字段來實現的。特點是顯示速度和效率高。但在單一樹的結構複雜的情況下,資料寫入效率不足。而且順序排列時候,插入,刪除記錄的演算法過於複雜,故通常用逆序排列。
資料結構(以mysql為例)
程式碼:---------------------------- -------------------------------------------------- --
CREATE TABLE `tree2` (
`id` tinyint(3) unsigned NOT NULL auto_increment,
`parentid` tinyint(3) unsigned NOT N N NM NLDL如何tinyint(3) unsigned NOT NULL default '0',
`layer` tinyint(3) unsigned NOT NULL default '0',
`orders` tinyint(3) unsigned NOT NULL ault ' > `topic` varchar(50) default NULL,
PRIMARY KEY (`id`),
KEY `parentid` (`parentid`),
KEY `parentid` (`parentid`),
KEY `parentid` (`parentid`),
KEY `parentid` (`parentid`),
KEY ``id``(`rootEY)(`YyKEY? ) TYPE=MyISAM
INSERT INTO `tree2` (`id`, `parentid`, `rootid`, `layer`, `orders`, `topic`) VALUES
,0,0,'樹1'),
(2,0,2,0,0,'樹2'),
(3,0,3,0,0,'樹3') ,
(4,2,2,1,2,'樹2-1'),
(5,4,2,2,3,'樹2-1-1'),
(6,2,2,1,1,'樹2-2'),
(7,1,1,1,4,'樹1-1'),
(8,1,1 ,1,2,'樹1-2'),
(9,1,1,1,1,'樹1-3'),
(10,8,1,2,3,'樹1-2-1'),
(11,7,1,2,5,'樹1-1-1'),
(12,11,1,3,6,'樹1 -1-1-1');
-------------------------------------- ------------------------------------------
顯示程式
PHP代碼:-------------------------------------- ------------------------------------------
/* 資料庫連線*/
mysql_connect();
mysql_select_db('tree');
/* 選取所有根記錄id */
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("");
");
/* 選取此樹所有記錄,並依orders欄位排序*/
$ ,layer from tree2 where rootid = $ra[0] order by orders";
$rs1 = mysql_query($sql);
while($ra1 = mysql_fetch_row($rs1)) { if($ra1[1]>$lay) {
echo(str_repeat("",$ra1[1]-$layse); ra1[1] echo(str_repeat("
",$lay-$ra1[1]));
//echo("$ra1[1]>$lay");
echo("
$lay = $ra1[🎜> $lay = $ra1[1];
}
echo("
}
echo("
?>
------ -------------------------------------------------- ------------------------
插入資料程式
PHP代碼:----- -------------------------------------------------- -------------------------
/* 資料庫連線*/
mysql_connect();
mysql_select_db('tree');
/* 插入根記錄*/
$sql = "insert into tree2 (topic) values ('樹5')";
mysql_query($ sql);
$sql = "update tree2 set rootid = id where id = ".mysql_insert_id();
mysql_query($sql);
/* 插入子記錄*/
$parentid = 5;//父記錄id
/* 取出根記錄id,父記錄縮排層次,父記錄順序位置*/
$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,layerid,orders,layerid,orders,layerid,orders,layerid,orders,layerid,orders. ,topic) values ($rootid,$parentid,".($orders 1).",".($layer 1).",'樹2-1-1-2')";
mysql_query($ sql);?>
http://www.bkjia.com/PHPjc/314792.html
www.bkjia.com

TheSecretTokeEpingAphp-PowerEdwebSiterUnningSmoothlyShyunderHeavyLoadInVolvOLVOLVOLDEVERSALKEYSTRATICES:1)emplactopCodeCachingWithOpcachingWithOpCacheToreCescriptexecution Time,2)使用atabasequercachingCachingCachingWithRedataBasEndataBaseLeSendataBaseLoad,3)

你應該關心DependencyInjection(DI),因為它能讓你的代碼更清晰、更易維護。 1)DI通過解耦類,使其更模塊化,2)提高了測試的便捷性和代碼的靈活性,3)使用DI容器可以管理複雜的依賴關係,但要注意性能影響和循環依賴問題,4)最佳實踐是依賴於抽象接口,實現鬆散耦合。

是的,優化papplicationispossibleandessential.1)empartcachingingcachingusedapcutorediucedsatabaseload.2)優化的atabaseswithexing,高效Quereteries,and ConconnectionPooling.3)EnhanceCodeWithBuilt-unctions,避免使用,避免使用ingglobalalairaiables,並避免使用

theKeyStrategiestosigantificallyBoostPhpaPplicationPerformenCeare:1)UseOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)優化AtabaseInteractionswithPreparedStateTementStatementStatementAndProperIndexing,3)配置

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增強codemodocultion,可驗證性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

選擇DependencyInjection(DI)用於大型應用,ServiceLocator適合小型項目或原型。 1)DI通過構造函數注入依賴,提高代碼的測試性和模塊化。 2)ServiceLocator通過中心註冊獲取服務,方便但可能導致代碼耦合度增加。

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)啟用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替換loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

phpemailvalidation invoLvesthreesteps:1)格式化進行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

SublimeText3漢化版
中文版,非常好用

Dreamweaver CS6
視覺化網頁開發工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)