PHP Class&Object -- PHP 自排序二叉樹的深入解析
更新時(shí)間:2013年06月25日 09:00:21 作者:
本篇文章是對PHP中的自排序二叉樹進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
在節(jié)點(diǎn)之間再應(yīng)用一些排序邏輯,二叉樹就能提供出色的組織方式。對于每個(gè)節(jié)點(diǎn),都讓滿足所有特定條件的元素都位于左節(jié)點(diǎn)及其子節(jié)點(diǎn)。在插入新元素時(shí),我們需要從樹的第一個(gè)節(jié) 點(diǎn)(根節(jié)點(diǎn))開始,判斷它屬于哪一側(cè)的節(jié)點(diǎn),然后沿著這一側(cè)找到恰當(dāng)?shù)奈恢?,類似地,在讀取數(shù)據(jù)時(shí),只需要使用按序遍歷方法來遍歷二叉樹。
<?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管理依賴(dependency)關(guān)系工具 Composer 安裝與使用
Composer 是PHP中用來管理依賴(dependency)關(guān)系的工具。你可以在自己的項(xiàng)目中聲明所依賴的外部工具庫(libraries),Composer會幫你安裝這些依賴的庫文件。2014-08-08
PHP中數(shù)組轉(zhuǎn)換為SimpleXML教程
在本篇文章中我們給大家總結(jié)了一篇關(guān)于PHP中數(shù)組轉(zhuǎn)換為SimpleXML教程內(nèi)容,有需要的朋友們跟著學(xué)習(xí)參考下。2019-01-01
PHP實(shí)現(xiàn)json_decode不轉(zhuǎn)義中文的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)json_decode不轉(zhuǎn)義中文的方法,結(jié)合實(shí)例形式具體分析了php5.4+及5.3版本針對json_decode實(shí)現(xiàn)不轉(zhuǎn)義中文的具體操作技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-05-05
PHP判斷文件是否被引入的方法get_included_files用法示例
這篇文章主要介紹了PHP判斷文件是否被引入的方法get_included_files用法,結(jié)合實(shí)例形式分析了get_included_files函數(shù)獲取引入文件及遍歷輸出的操作技巧,需要的朋友可以參考下2016-11-11
thinkphp框架實(shí)現(xiàn)數(shù)據(jù)添加和顯示功能
這篇文章主要為大家詳細(xì)介紹了thinkphp框架實(shí)現(xiàn)數(shù)據(jù)添加和顯示功能的相關(guān)資料,需要的朋友可以參考下2016-06-06
php7.3報(bào)preg_match()?JIT?compilation?failed?no?more?mem
PHP?JIT編譯失敗,內(nèi)存不足的解決方法!你是否遇到過這個(gè)問題?不用擔(dān)心,我們將為你提供簡單易懂的解決方案,讓你擺脫這一困擾,立即閱讀我們的指南,輕松解決PHP?JIT編譯失敗的煩惱!2023-12-12

