当前位置:Gxlcms > PHP教程 > PHP实现二叉树深度优先遍历(前序、中序、后序)和广度优先遍历(层次)

PHP实现二叉树深度优先遍历(前序、中序、后序)和广度优先遍历(层次)

时间:2021-07-01 10:21:17 帮助过:7人阅读

这篇文章主要介绍了PHP实现二叉树深度优先遍历(前序、中序、后序)和广度优先遍历(层次),结合实例形式详细分析了php针对二叉树的深度优先遍历与广度优先遍历相关操作技巧与注意事项,需要的朋友可以参考下

本文实例讲述了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、前序遍历:


  1. /**
  2. * 前序遍历(递归方法)
  3. */
  4. private function pre_order1($root)
  5. {
  6. if (!is_null($root)) {
  7. //这里用到常量__FUNCTION__,获取当前函数名,好处是假如修改函数名的时候,里面的实现不用修改
  8. $function = __FUNCTION__;
  9. echo $root->key . " ";
  10. $this->$function($root->left);
  11. $this->$function($root->right);
  12. }
  13. }
  14. /**
  15. * 前序遍历(非递归方法)
  16. * 因为当遍历过根节点之后还要回来,所以必须将其存起来。考虑到后进先出的特点,选用栈存储。
  17. */
  18. private function pre_order2($root)
  19. {
  20. // $stack = new splstack();
  21. // $stack->push($root);
  22. // while(!$stack->isEmpty()){
  23. // $node = $stack->pop();
  24. // echo $node->key.' ';
  25. // if(!is_null($node->right)){
  26. // $stack->push($node->right);
  27. // }
  28. // if(!is_null($node->left)){
  29. // $stack->push($node->left);
  30. // }
  31. // }
  32. if (is_null($root)) {
  33. return;
  34. }
  35. $stack = new splstack();
  36. $node = $root;
  37. while (!is_null($node) || !$stack->isEmpty()) {
  38. while (!is_null($node)) {
  39. //只要结点不为空就应该入栈保存,与其左右结点无关
  40. $stack->push($node);
  41. echo $node->key . ' ';
  42. $node = $node->left;
  43. }
  44. $node = $stack->pop();
  45. $node = $node->right;
  46. }
  47. }
  48. //前序遍历
  49. public function PreOrder()
  50. {
  51. // 所在对象中的tree属性保存了一个树的引用
  52. // $this->pre_order1($this->tree->root);
  53. $this->pre_order2($this->tree->root);
  54. }


说明:1、我将所有的遍历方法都封装在一个类 traverse 里面了。2、pre_order2方法中,在使用栈的过程中,我使用的是PHP标准库SPL提供的splstack,如果你们习惯使用数组的话,可以使用 array_push() array_pop() 模拟实现。

2、中序遍历:


  1. /**
  2. * 中序遍历(递归方法)
  3. */
  4. private function mid_order1($root)
  5. {
  6. if (!is_null($root)) {
  7. $function = __FUNCTION__;
  8. $this->$function($root->left);
  9. echo $root->key . " ";
  10. $this->$function($root->right);
  11. }
  12. }
  13. /**
  14. * 中序遍历(非递归方法)
  15. * 因为当遍历过根节点之后还要回来,所以必须将其存起来。考虑到后进先出的特点,选用栈存储。
  16. */
  17. private function mid_order2($root)
  18. {
  19. if (is_null($root)) {
  20. return;
  21. }
  22. $stack = new splstack();
  23. $node = $root;
  24. while (!is_null($node) || !$stack->isEmpty()) {
  25. while (!is_null($node)) {
  26. $stack->push($node);
  27. $node = $node->left;
  28. }
  29. $node = $stack->pop();
  30. echo $node->key . ' ';
  31. $node = $node->right;
  32. }
  33. }
  34. //中序遍历
  35. public function MidOrder()
  36. {
  37. // $this->mid_order1($this->tree->root);
  38. $this->mid_order2($this->tree->root);
  39. }


3、后序遍历:


  1. /**
  2. * 后序遍历(递归方法)
  3. */
  4. private function post_order1($root)
  5. {
  6. if (!is_null($root)) {
  7. $function = __FUNCTION__;
  8. $this->$function($root->left);
  9. $this->$function($root->right);
  10. echo $root->key . " ";
  11. }
  12. }
  13. /**
  14. * 后序遍历(非递归方法)
  15. * 因为当遍历过根节点之后还要回来,所以必须将其存起来。考虑到后进先出的特点,选用栈存储。
  16. * 由于在访问了左子节点后怎么跳到右子节点是难点,这里使用一个标识lastVisited来标识上一次访问的结点
  17. */
  18. private function post_order2($root)
  19. {
  20. if (is_null($root)) {
  21. return;
  22. }
  23. $node = $root;
  24. $stack = new splstack();
  25. //保存上一次访问的结点引用
  26. $lastVisited = NULL;
  27. $stack->push($node);
  28. while(!$stack->isEmpty()){
  29. $node = $stack->top();//获取栈顶元素但不弹出
  30. if(($node->left == NULL && $node->right == NULL) || ($node->right == NULL && $lastVisited == $node->left) || ($lastVisited == $node->right)){
  31. echo $node->key.' ';
  32. $lastVisited = $node;
  33. $stack->pop();
  34. }else{
  35. if($node->right){
  36. $stack->push($node->right);
  37. }
  38. if($node->left){
  39. $stack->push($node->left);
  40. }
  41. }
  42. }
  43. }
  44. //后序遍历
  45. public function PostOrder()
  46. {
  47. // $this->post_order1($this->tree->root);
  48. $this->post_order2($this->tree->root);
  49. }


广度优先遍历:

1、层次遍历:


  1. /**
  2. * 层次遍历(递归方法)
  3. * 由于是按层逐层遍历,因此传递树的层数
  4. */
  5. private function level_order1($root,$level){
  6. if($root == NULL || $level < 1){
  7. return;
  8. }
  9. if($level == 1){
  10. echo $root->key.' ';
  11. return;
  12. }
  13. if(!is_null($root->left)){
  14. $this->level_order1($root->left,$level - 1);
  15. }
  16. if(!is_null($root->right)){
  17. $this->level_order1($root->right,$level - 1);
  18. }
  19. }
  20. /**
  21. * 层次遍历(非递归方法)
  22. * 每一层从左向右
输出 元素需要储存有先进先出的特性,所以选用队列存储。 */ 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,如果你们习惯使用数组的话,可以使用 array_push() array_shift() 模拟实现。

使用:

现在我们来看看客户端代码:


  1. class Client
  2. {
  3. public static function Main()
  4. {
  5. try {
  6. //实现文件的自动加载
  7. function autoload($class)
  8. {
  9. include strtolower($class) . '.php';
  10. }
  11. spl_autoload_register('autoload');
  12. $arr = array(10, 8, 12, 7, 9, 11, 13);
  13. $tree = new Bst();
  14. // $tree = new Avl();
  15. // $tree = new Rbt();
  16. $tree->init($arr);
  17. $traverse = new traverse($tree);
  18. $traverse->PreOrder();
  19. // $traverse->MidOrder();
  20. // $traverse->PostOrder();
  21. // $traverse->LevelOrder();
  22. } catch (Exception $e) {
  23. echo $e->getMessage();
  24. }
  25. }
  26. }
  27. CLient::Main();


补充:

1. 在客户端中所使用的三个类 Bst、Avl、Rbt 大家可以参考前面一篇:《PHP实现绘制二叉树图形显示功能详解》

2. 为什么我推荐大家使用SPL标准库中提供的splstacksplqueue呢?这是我在某一篇文章中看到的:虽然我们可以使用传统的变量类型来描述数据结构,例如用数组来描述堆栈(Strack)– 然后使用对应的方式 pop 和 push(array_pop()array_push()),但你得时刻小心,因为毕竟它们不是专门用于描述数据结构的 – 一次误操作就有可能破坏该堆栈。而 SPL 的 SplStack 对象则严格以堆栈的形式描述数据,并提供对应的方法。同时,这样的代码应该也能理解它在操作堆栈而非某个数组,从而能让你的同伴更好的理解相应的代码,并且它更快。原文地址:PHP SPL,遗落的宝石

3. 本文相关参考文章: 《C语言二叉树常见操作详解【前序,中序,后序,层次遍历及非递归查找,统计个数,比较,求深度】》、《Java实现二叉树的深度优先遍历和广度优先遍历算法示例》

相关推荐:

PHP排序算法之冒泡排序(Bubble Sort)



以上就是PHP实现二叉树深度优先遍历(前序、中序、后序)和广度优先遍历(层次)的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行