PHP实现二叉树的深度优先遍历(前序、中序、后序)和广度优先遍历(层次)
2017-07-26 23:30
756 查看
前言:
深度优先遍历:对每一个可能的分支路径深入到不能再深入为止,而且每个结点只能访问一次。要特别注意的是,二叉树的深度优先遍历比较特殊,可以细分为先序遍历、中序遍历、后序遍历。具体说明如下:前序遍历:根节点->左子树->右子树
中序遍历:左子树->根节点->右子树
后序遍历:左子树->右子树->根节点
广度优先遍历:又叫层次遍历,从上往下对每一层依次访问,在每一层中,从左往右(也可以从右往左)访问结点,访问完一层就进入下一层,直到没有结点可以访问为止。
例如对于一下这棵树:
1 <?php 2 header("Content-type: text/html; charset=utf-8"); 3 class Node 4 { 5 public $value; 6 public $left; 7 public $right; 8 9 public function __construct($value) 10 { 11 $this->value = $value; 12 } 13 } 14 15 class Tree 16 { 17 /** 18 * 先序遍历(递归方法) 19 */ 20 public function recursion_preorder($root) 21 { 22 static $res = array(); 23 if (!is_null($root)) 24 { 25 $function = __FUNCTION__; 26 $res[] = $root->value; 27 $this->$function($root->left); 28 $this->$function($root->right); 29 } 30 return $res; 31 } 32 33 /** 34 * 中序遍历(递归方法) 35 */ 36 public function recursion_midorder($root) 37 { 38 static $res = array(); 39 if(!is_null($root)) 40 { 41 $function = __FUNCTION__; 42 $this->$function($root->left); 43 $res[] = $root->value; 44 $this->$function($root->right); 45 } 46 return $res; 47 } 48 49 /** 50 * 后序遍历(递归方法) 51 */ 52 public function recursion_postorder($root) 53 { 54 static $res = array(); 55 if (!is_null($root)) 56 { 57 $function = __FUNCTION__; 58 $this->$function($root->left); 59 $this->$function($root->right); 60 $res[] = $root->value; 61 } 62 return $res; 63 } 64 65 /** 66 * 先序遍历(非递归) 67 */ 68 public function preorder($node) 69 { 70 $res = array(); 71 $stack = new splstack(); 72 while(!is_null($node) || !$stack->isEmpty()) 73 { 74 while(!is_null($node))//节点不为空就入栈 75 { 76 $stack->push($node); 77 $res[] = $node->value; 78 $node = $node->left; 79 } 80 $node = $stack->pop(); 81 $node = $node->right; 82 } 83 return $res; 84 } 85 86 /** 87 * 中序遍历(非递归) 88 */ 89 public function midorder($node) 90 { 91 $res = array(); 92 $stack = new splstack(); 93 while(!is_null($node) || !$stack->isEmpty()) 94 { 95 while(!is_null($node)) 96 { 97 $stack->push($node); 98 $node = $node->left; 99 } 100 $node = $stack->pop(); 101 $res[] = $node->value; 102 $node = $node->right; 103 } 104 return $res; 105 } 106 107 /** 108 * 后序遍历(非递归) 109 */ 110 public function postorder($node) 111 { 112 $stack = new splstack(); 113 $outstack = new splstack(); 114 115 $stack->push($node); 116 while(!$stack->isEmpty()) 117 { 118 $center_node = $stack->pop(); 119 $outstack->push($center_node);//最先压入根节点,最后输出 120 if(!is_null($center_node->left)) 121 { 122 $stack->push($center_node->left); 123 } 124 if(!is_null($center_node->right)) 125 { 126 $stack->push($center_node->right); 127 } 128 } 129 130 $res = array(); 131 while(!$outstack->isEmpty()) 132 { 133 $node = $outstack->pop(); 134 $res[] = $node->value; 135 } 136 return $res; 137 } 138 139 /** 140 * 广度优先遍历(层次遍历、非递归) 141 */ 142 public function level_order($node) 143 { 144 $res = array(); 145 $queue = new splqueue(); 146 $queue->enqueue($node); 147 while(!$queue->isEmpty()) 148 { 149 $node = $queue->dequeue(); 150 $res[] = $node->value; 151 if(!is_null($node->left)) 152 { 153 $queue->enqueue($node->left); 154 } 155 if(!is_null($node->right)) 156 { 157 $queue->enqueue($node->right); 158 } 159 } 160 return $res; 161 } 162 } 163 164 $a = new Node(10); 165 $b = new Node(8); 166 $c = new Node(12); 167 $d = new Node(7); 168 $e = new Node(9); 169 $f = new Node(11); 170 $g = new Node(13); 171 172 $a->left = $b; 173 $a->right = $c; 174 $b->left = $d; 175 $b->right = $e; 176 $c->left = $f; 177 $c->right = $g; 178 179 $tree = new Tree(); 180 $res = $tree->recursion_preorder($a); 181 echo "先序遍历结果(递归):" . implode('-', $res) . "<br/>"; 182 183 $res = $tree->preorder($a); 184 echo "先序遍历结果(非递归):" . implode('-', $res) . "<br/>"; 185 186 $res = $tree->recursion_midorder($a); 187 echo "中序遍历结果(递归):" . implode('-', $res) . "<br/>"; 188 189 $res = $tree->midorder($a); 190 echo "中序遍历结果(非递归):" . implode('-', $res) . "<br/>"; 191 192 $res = $tree->recursion_postorder($a); 193 echo "后序遍历结果(递归):" . implode('-', $res) . "<br/>"; 194 195 $res = $tree->postorder($a); 196 echo "后序遍历结果(非递归):" . implode('-', $res) . "<br/>"; 197 198 $res = $tree->level_order($a); 199 echo "层次遍历结果(非递归):" . implode('-', $res) . "<br/>";
View Code
相关文章推荐
- PHP实现二叉树的深度优先遍历(前序、中序、后序)和广度优先遍历(层次)
- PHP实现二叉树的深度优先遍历(前序、中序、后序)和广度优先遍历(层次)
- PHP实现二叉树深度优先遍历(前序、中序、后序)和广度优先遍历(层次)实例详解
- 二叉树的深度优先遍历与广度优先遍历 [ C++ 实现 ]
- [二叉树专题]:广度优先:按层次遍历二叉树的非递归实现||使用队列实现层次遍历二叉树
- 二叉树的深度优先遍历与广度优先遍历 [ C++ 实现 ]
- 二叉树的深度优先遍历与广度优先遍历 [ C++ 实现 ]
- 二叉树构建,先序,中序,后序遍历(以及非递归实现),广度优先遍历
- 二叉树的深度优先遍历与广度优先遍历 [C++实现]
- 二叉树的深度优先遍历与广度优先遍历 [ C++ 实现 ]
- python 用栈和队列实现二叉树的深度优先遍历(三种)和广度优先遍历
- 二叉树的深度优先遍历与广度优先遍历 [ C++ 实现 ]
- 二叉树基本操作的递归实现(二叉树建立,先序,中序,后序,深度的递归遍历。广度优先,高度优先的非递归遍历)
- 二叉树的深度优先遍历与广度优先遍历 [ C++ 实现 ]
- 二叉树的深度优先遍历与广度优先遍历 [ C++ 实现 ]
- 二叉树的深度优先遍历与广度优先遍历 [ C++ 实现 ]
- 二叉树的深度优先遍历与广度优先遍历 [ C++ 实现 ]
- 二叉树的深度优先遍历与广度优先遍历 [ C++ 实现 ]
- java实现二叉树的深度优先遍历和广度优先遍历
- 二叉树构建,先序,中序,后序遍历(以及非递归实现),广度优先遍历