PHP实现二叉树深度优先遍历(前序、中序、后序)和广度优先遍历(层
副问题[/!--empirenews.page--]
本篇章节讲授PHP实现二叉树深度优先遍历(前序、中序、后序)和广度优先遍历(条理)。分享给各人供各人参考,详细如下: 媒介: 深度优先遍历:对每一个也许的分支路径深入到不能再深入为止,并且每个结点只能会见一次。要出格留意的是,二叉树的深度优先遍历较量非凡,可以细分为先序遍历、中序遍历、后序遍历。详细声名如下:前序遍历:根节点->左子树->右子树 中序遍历:左子树->根节点->右子树 后序遍历:左子树->右子树->根节点 广度优先遍历:又叫条理遍历,从上往下对每一层依次会见,在每一层中,从左往右(也可以从右往左)会见结点,会见完一层就进入下一层,直到没有结点可以会见为止。譬喻对付一下这棵树:
前序遍历:10 8 7 9 12 11 13 中序遍历:7 8 9 10 11 12 13 后序遍历:7 9 8 11 13 12 10
条理遍历:10 8 12 7 9 11 13 二叉树的深度优先遍历的非递归的通用做法是回收栈,广度优先遍历的非递归的通用做法是回收行列。 深度优先遍历: 1、前序遍历: key . " "; $this->$function($root->left); $this->$function($root->right); } } /** * 前序遍历(非递归要领) * 由于当遍历过根节点之后还要返来,以是必需将其存起来。思量到后进先出的特点,选用栈存储。 */ 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)) { //只要结点不为空就应该入栈生涯,与其阁下结点无关 $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尺度库SPL提供的splstack,假如你们风俗行使数组的话,可以行使 2、中序遍历: $function($root->left); echo $root->key . " "; $this->$function($root->right); } } /** * 中序遍历(非递归要领) * 由于当遍历过根节点之后还要返来,以是必需将其存起来。思量到后进先出的特点,选用栈存储。 */ 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、后序遍历: $function($root->left); $this->$function($root->right); echo $root->key . " "; } } /** * 后序遍历(非递归要领) * 由于当遍历过根节点之后还要返来,以是必需将其存起来。思量到后进先出的特点,选用栈存储。 * 因为在会见了左子节点后怎么跳到右子节点是难点,这里行使一个标识lastVisited来标识上一次会见的结点 */ private function post_order2($root) { if (is_null($root)) { return; } $node = $root; $stack = new splstack(); //生涯上一次会见的结点引用 $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); }广度优先遍历: 1、条理遍历: 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; //操作行列实现 // $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); } //获取树的层数 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尺度库SPL提供的splqueue,假如你们风俗行使数组的话,可以行使 行使: (编辑:湖南网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |