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