PHP實現(xiàn)二叉樹深度優(yōu)先遍歷(前序、中序、后序)和廣度優(yōu)先遍歷(層次)實例詳解
本文實例講述了PHP實現(xiàn)二叉樹深度優(yōu)先遍歷(前序、中序、后序)和廣度優(yōu)先遍歷(層次)。分享給大家供大家參考,具體如下:
前言:
深度優(yōu)先遍歷:對每一個可能的分支路徑深入到不能再深入為止,而且每個結(jié)點只能訪問一次。要特別注意的是,二叉樹的深度優(yōu)先遍歷比較特殊,可以細(xì)分為先序遍歷、中序遍歷、后序遍歷。具體說明如下:
前序遍歷:根節(jié)點->左子樹->右子樹
中序遍歷:左子樹->根節(jié)點->右子樹
后序遍歷:左子樹->右子樹->根節(jié)點
廣度優(yōu)先遍歷:又叫層次遍歷,從上往下對每一層依次訪問,在每一層中,從左往右(也可以從右往左)訪問結(jié)點,訪問完一層就進入下一層,直到?jīng)]有結(jié)點可以訪問為止。
例如對于一下這棵樹:
深度優(yōu)先遍歷:
前序遍歷:10 8 7 9 12 11 13
中序遍歷:7 8 9 10 11 12 13
后序遍歷:7 9 8 11 13 12 10
廣度優(yōu)先遍歷:
層次遍歷:10 8 12 7 9 11 13
二叉樹的深度優(yōu)先遍歷的非遞歸的通用做法是采用棧,廣度優(yōu)先遍歷的非遞歸的通用做法是采用隊列。
深度優(yōu)先遍歷:
1、前序遍歷:
/** * 前序遍歷(遞歸方法) */ private function pre_order1($root) { if (!is_null($root)) { //這里用到常量__FUNCTION__,獲取當(dāng)前函數(shù)名,好處是假如修改函數(shù)名的時候,里面的實現(xiàn)不用修改 $function = __FUNCTION__; echo $root->key . " "; $this->$function($root->left); $this->$function($root->right); } } /** * 前序遍歷(非遞歸方法) * 因為當(dāng)遍歷過根節(jié)點之后還要回來,所以必須將其存起來??紤]到后進先出的特點,選用棧存儲。 */ private function pre_order2($root) { // $stack = new splstack(); // $stack->push($root); // while(!$stack->isEmpty()){ // $node = $stack->pop(); // echo $node->key.' '; // if(!is_null($node->right)){ // $stack->push($node->right); // } // if(!is_null($node->left)){ // $stack->push($node->left); // } // } if (is_null($root)) { return; } $stack = new splstack(); $node = $root; while (!is_null($node) || !$stack->isEmpty()) { while (!is_null($node)) { //只要結(jié)點不為空就應(yīng)該入棧保存,與其左右結(jié)點無關(guān) $stack->push($node); echo $node->key . ' '; $node = $node->left; } $node = $stack->pop(); $node = $node->right; } } //前序遍歷 public function PreOrder() { // 所在對象中的tree屬性保存了一個樹的引用 // $this->pre_order1($this->tree->root); $this->pre_order2($this->tree->root); }
說明:1、我將所有的遍歷方法都封裝在一個類 traverse 里面了。2、pre_order2方法中,在使用棧的過程中,我使用的是PHP標(biāo)準(zhǔn)庫SPL提供的splstack,如果你們習(xí)慣使用數(shù)組的話,可以使用 array_push()
和array_pop()
模擬實現(xiàn)。
2、中序遍歷:
/** * 中序遍歷(遞歸方法) */ private function mid_order1($root) { if (!is_null($root)) { $function = __FUNCTION__; $this->$function($root->left); echo $root->key . " "; $this->$function($root->right); } } /** * 中序遍歷(非遞歸方法) * 因為當(dāng)遍歷過根節(jié)點之后還要回來,所以必須將其存起來。考慮到后進先出的特點,選用棧存儲。 */ private function mid_order2($root) { if (is_null($root)) { return; } $stack = new splstack(); $node = $root; while (!is_null($node) || !$stack->isEmpty()) { while (!is_null($node)) { $stack->push($node); $node = $node->left; } $node = $stack->pop(); echo $node->key . ' '; $node = $node->right; } } //中序遍歷 public function MidOrder() { // $this->mid_order1($this->tree->root); $this->mid_order2($this->tree->root); }
3、后序遍歷:
/** * 后序遍歷(遞歸方法) */ private function post_order1($root) { if (!is_null($root)) { $function = __FUNCTION__; $this->$function($root->left); $this->$function($root->right); echo $root->key . " "; } } /** * 后序遍歷(非遞歸方法) * 因為當(dāng)遍歷過根節(jié)點之后還要回來,所以必須將其存起來。考慮到后進先出的特點,選用棧存儲。 * 由于在訪問了左子節(jié)點后怎么跳到右子節(jié)點是難點,這里使用一個標(biāo)識lastVisited來標(biāo)識上一次訪問的結(jié)點 */ private function post_order2($root) { if (is_null($root)) { return; } $node = $root; $stack = new splstack(); //保存上一次訪問的結(jié)點引用 $lastVisited = NULL; $stack->push($node); while(!$stack->isEmpty()){ $node = $stack->top();//獲取棧頂元素但不彈出 if(($node->left == NULL && $node->right == NULL) || ($node->right == NULL && $lastVisited == $node->left) || ($lastVisited == $node->right)){ echo $node->key.' '; $lastVisited = $node; $stack->pop(); }else{ if($node->right){ $stack->push($node->right); } if($node->left){ $stack->push($node->left); } } } } //后序遍歷 public function PostOrder() { // $this->post_order1($this->tree->root); $this->post_order2($this->tree->root); }
廣度優(yōu)先遍歷:
1、層次遍歷:
/** * 層次遍歷(遞歸方法) * 由于是按層逐層遍歷,因此傳遞樹的層數(shù) */ private function level_order1($root,$level){ if($root == NULL || $level < 1){ return; } if($level == 1){ echo $root->key.' '; return; } if(!is_null($root->left)){ $this->level_order1($root->left,$level - 1); } if(!is_null($root->right)){ $this->level_order1($root->right,$level - 1); } } /** * 層次遍歷(非遞歸方法) * 每一層從左向右輸出 元素需要儲存有先進先出的特性,所以選用隊列存儲。 */ private function level_order2($root){ if(is_null($root)){ return; } $node = $root; //利用隊列實現(xiàn) // $queue = array(); // array_push($queue,$node); // // while(!is_null($node = array_shift($queue))){ // echo $node->key.' '; // if(!is_null($node->left)){ // array_push($queue,$node->left); // } // if(!is_null($node->right)){ // array_push($queue,$node->right); // } // } $queue = new splqueue(); $queue->enqueue($node); while(!$queue->isEmpty()){ $node = $queue->dequeue(); echo $node->key.' '; if (!is_null($node->left)) { $queue->enqueue($node->left); } if (!is_null($node->right)) { $queue->enqueue($node->right); } } } //層次遍歷 public function LevelOrder(){ // $level = $this->getdepth($this->tree->root); // for($i = 1;$i <= $level;$i ++){ // $this->level_order1($this->tree->root,$i); // } $this->level_order2($this->tree->root); } //獲取樹的層數(shù) private function getdepth($root){ if(is_null($root)){ return 0; } $left = getdepth($root -> left); $right = getdepth($root -> right); $depth = ($left > $right ? $left : $right) + 1; return $depth; }
說明:level_order2方法中,在使用隊列的過程中,我使用的是PHP標(biāo)準(zhǔn)庫SPL提供的splqueue,如果你們習(xí)慣使用數(shù)組的話,可以使用 array_push()
和array_shift()
模擬實現(xiàn)。
使用:
現(xiàn)在我們來看看客戶端代碼:
class Client { public static function Main() { try { //實現(xiàn)文件的自動加載 function autoload($class) { include strtolower($class) . '.php'; } spl_autoload_register('autoload'); $arr = array(10, 8, 12, 7, 9, 11, 13); $tree = new Bst(); // $tree = new Avl(); // $tree = new Rbt(); $tree->init($arr); $traverse = new traverse($tree); $traverse->PreOrder(); // $traverse->MidOrder(); // $traverse->PostOrder(); // $traverse->LevelOrder(); } catch (Exception $e) { echo $e->getMessage(); } } } CLient::Main();
補充:
1. 在客戶端中所使用的三個類 Bst、Avl、Rbt 大家可以參考前面一篇:《PHP實現(xiàn)繪制二叉樹圖形顯示功能詳解》
2. 為什么我推薦大家使用SPL標(biāo)準(zhǔn)庫中提供的splstack
和splqueue
呢?這是我在某一篇文章中看到的:雖然我們可以使用傳統(tǒng)的變量類型來描述數(shù)據(jù)結(jié)構(gòu),例如用數(shù)組來描述堆棧(Strack)– 然后使用對應(yīng)的方式 pop 和 push(array_pop()
、array_push()
),但你得時刻小心,因為畢竟它們不是專門用于描述數(shù)據(jù)結(jié)構(gòu)的 – 一次誤操作就有可能破壞該堆棧。而 SPL 的 SplStack 對象則嚴(yán)格以堆棧的形式描述數(shù)據(jù),并提供對應(yīng)的方法。同時,這樣的代碼應(yīng)該也能理解它在操作堆棧而非某個數(shù)組,從而能讓你的同伴更好的理解相應(yīng)的代碼,并且它更快。原文地址:PHP SPL,遺落的寶石
3. 本文相關(guān)參考文章: 《C語言二叉樹常見操作詳解【前序,中序,后序,層次遍歷及非遞歸查找,統(tǒng)計個數(shù),比較,求深度】》、《Java實現(xiàn)二叉樹的深度優(yōu)先遍歷和廣度優(yōu)先遍歷算法示例》
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計算法總結(jié)》、《php字符串(string)用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP常用遍歷算法與技巧總結(jié)》及《PHP數(shù)學(xué)運算技巧總結(jié)》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
php通過COM類調(diào)用組件的實現(xiàn)代碼
COM(Component Object Model)組件對象模型,是一種跨應(yīng)用和語言共享二進制代碼的方法。COM可以作為DLL被本機程序載入也可以通過DCOM被遠(yuǎn)程進程調(diào)用2012-01-01PHP PDOStatement:bindParam插入數(shù)據(jù)錯誤問題分析
PHP PDOStatement:bindParam插入數(shù)據(jù)錯誤問題分析,開發(fā)中一定要注意2013-11-11PHP數(shù)據(jù)庫操作二:memcache用法分析
這篇文章主要介紹了PHP數(shù)據(jù)庫操作memcache用法,結(jié)合實例形式詳細(xì)分析了memcache的下載、安裝、配置及相關(guān)使用技巧,需要的朋友可以參考下2017-08-08PHP Imagick完美實現(xiàn)圖片裁切、生成縮略圖、添加水印
這篇文章主要介紹了PHP Imagick完美實現(xiàn)圖片裁切、生成縮略圖、添加水印的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-02-02