PHP Class&Object -- PHP 自排序二叉樹的深入解析
更新時間:2013年06月25日 09:00:21 作者:
本篇文章是對PHP中的自排序二叉樹進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
在節(jié)點(diǎn)之間再應(yīng)用一些排序邏輯,二叉樹就能提供出色的組織方式。對于每個節(jié)點(diǎn),都讓滿足所有特定條件的元素都位于左節(jié)點(diǎn)及其子節(jié)點(diǎn)。在插入新元素時,我們需要從樹的第一個節(jié) 點(diǎn)(根節(jié)點(diǎn))開始,判斷它屬于哪一側(cè)的節(jié)點(diǎn),然后沿著這一側(cè)找到恰當(dāng)?shù)奈恢?,類似地,在讀取數(shù)據(jù)時,只需要使用按序遍歷方法來遍歷二叉樹。
<?php
ob_start();
// Here we need to include the binary tree class
Class Binary_Tree_Node() {
// You can find the details at
}
ob_end_clean();
// Define a class to implement self sorting binary tree
class Sorting_Tree {
// Define the variable to hold our tree:
public $tree;
// We need a method that will allow for inserts that automatically place
// themselves in the proper order in the tree
public function insert($val) {
// Handle the first case:
if (!(isset($this->tree))) {
$this->tree = new Binary_Tree_Node($val);
} else {
// In all other cases:
// Start a pointer that looks at the current tree top:
$pointer = $this->tree;
// Iteratively search the tree for the right place:
for(;;) {
// If this value is less than, or equal to the current data:
if ($val <= $pointer->data) {
// We are looking to the left ... If the child exists:
if ($pointer->left) {
// Traverse deeper:
$pointer = $pointer->left;
} else {
// Found the new spot: insert the new element here:
$pointer->left = new Binary_Tree_Node($val);
break;
}
} else {
// We are looking to the right ... If the child exists:
if ($pointer->right) {
// Traverse deeper:
$pointer = $pointer->right;
} else {
// Found the new spot: insert the new element here:
$pointer->right = new Binary_Tree_Node($val);
break;
}
}
}
}
}
// Now create a method to return the sorted values of this tree.
// All it entails is using the in-order traversal, which will now
// give us the proper sorted order.
public function returnSorted() {
return $this->tree->traverseInorder();
}
}
// Declare a new sorting tree:
$sort_as_you_go = new Sorting_Tree();
// Let's randomly create 20 numbers, inserting them as we go:
for ($i = 0; $i < 20; $i++) {
$sort_as_you_go->insert(rand(1,100));
}
// Now echo the tree out, using in-order traversal, and it will be sorted:
// Example: 1, 2, 11, 18, 22, 26, 32, 32, 34, 43, 46, 47, 47, 53, 60, 71,
// 75, 84, 86, 90
echo '<p>', implode(', ', $sort_as_you_go->returnSorted()), '</p>';
?>
復(fù)制代碼 代碼如下:
<?php
ob_start();
// Here we need to include the binary tree class
Class Binary_Tree_Node() {
// You can find the details at
}
ob_end_clean();
// Define a class to implement self sorting binary tree
class Sorting_Tree {
// Define the variable to hold our tree:
public $tree;
// We need a method that will allow for inserts that automatically place
// themselves in the proper order in the tree
public function insert($val) {
// Handle the first case:
if (!(isset($this->tree))) {
$this->tree = new Binary_Tree_Node($val);
} else {
// In all other cases:
// Start a pointer that looks at the current tree top:
$pointer = $this->tree;
// Iteratively search the tree for the right place:
for(;;) {
// If this value is less than, or equal to the current data:
if ($val <= $pointer->data) {
// We are looking to the left ... If the child exists:
if ($pointer->left) {
// Traverse deeper:
$pointer = $pointer->left;
} else {
// Found the new spot: insert the new element here:
$pointer->left = new Binary_Tree_Node($val);
break;
}
} else {
// We are looking to the right ... If the child exists:
if ($pointer->right) {
// Traverse deeper:
$pointer = $pointer->right;
} else {
// Found the new spot: insert the new element here:
$pointer->right = new Binary_Tree_Node($val);
break;
}
}
}
}
}
// Now create a method to return the sorted values of this tree.
// All it entails is using the in-order traversal, which will now
// give us the proper sorted order.
public function returnSorted() {
return $this->tree->traverseInorder();
}
}
// Declare a new sorting tree:
$sort_as_you_go = new Sorting_Tree();
// Let's randomly create 20 numbers, inserting them as we go:
for ($i = 0; $i < 20; $i++) {
$sort_as_you_go->insert(rand(1,100));
}
// Now echo the tree out, using in-order traversal, and it will be sorted:
// Example: 1, 2, 11, 18, 22, 26, 32, 32, 34, 43, 46, 47, 47, 53, 60, 71,
// 75, 84, 86, 90
echo '<p>', implode(', ', $sort_as_you_go->returnSorted()), '</p>';
?>
您可能感興趣的文章:
- PHP實(shí)現(xiàn)二叉樹的深度優(yōu)先與廣度優(yōu)先遍歷方法
- PHP實(shí)現(xiàn)的線索二叉樹及二叉樹遍歷方法詳解
- php實(shí)現(xiàn)的二叉樹遍歷算法示例
- PHP構(gòu)造二叉樹算法示例
- PHP實(shí)現(xiàn)繪制二叉樹圖形顯示功能詳解【包括二叉搜索樹、平衡樹及紅黑樹】
- PHP實(shí)現(xiàn)從上往下打印二叉樹的方法
- PHP基于非遞歸算法實(shí)現(xiàn)先序、中序及后序遍歷二叉樹操作示例
- PHP獲取二叉樹鏡像的方法
- PHP實(shí)現(xiàn)判斷二叉樹是否對稱的方法
- PHP實(shí)現(xiàn)二叉樹深度優(yōu)先遍歷(前序、中序、后序)和廣度優(yōu)先遍歷(層次)實(shí)例詳解
- PHP排序二叉樹基本功能實(shí)現(xiàn)方法示例
相關(guān)文章
PHP+Redis開發(fā)的書簽案例實(shí)戰(zhàn)詳解
這篇文章主要介紹了PHP+Redis開發(fā)的書簽案例,結(jié)合實(shí)例形式詳細(xì)分析了php結(jié)合redis開發(fā)書簽功能的具體步驟及相關(guān)操作技巧,需要的朋友可以參考下2019-07-07PHP+MySQL高并發(fā)加鎖事務(wù)處理問題解決方法
這篇文章主要介紹了PHP+MySQL高并發(fā)加鎖事務(wù)處理問題解決方法,結(jié)合實(shí)例形式分析了PHP+MySQL事務(wù)處理相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-04-04微信公眾平臺開發(fā)教程①獲取用戶Openid及個人信息圖文詳解
這篇文章主要介紹了微信公眾平臺開發(fā)獲取用戶Openid及個人信息,結(jié)合圖文形式詳細(xì)分析了微信公眾平臺獲取用戶Openid及個人信息的步驟、操作技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2019-04-04PHP排序之二維數(shù)組的按照字母排序?qū)崿F(xiàn)代碼
PHP排序之二維數(shù)組的按照字母排序方法,在實(shí)際開發(fā)還是非常有用的,有需要的拿去2011-08-08PHP將HTML轉(zhuǎn)換成文本的實(shí)現(xiàn)代碼
這篇文章主要介紹了PHP將HTML轉(zhuǎn)換成文本的實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-01-01