1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
| <?php
* Class Node 节点 */ class Node { public $value; public $left; public $right;
public function __construct($value = null) { $this->value = $value; } }
* Class BinaryTree 二叉树 */ class BinaryTree {
public $data;
public function __construct($data = []) { $this->data = $data; }
* 前序遍历: * 根节点 -> 左子树 -> 右子树 * 利用栈先进后出的特性,先访问根节点,再把右子树压入,再压入左子树。 * 这样取出的时候是先取出左子树,最后取出右子树。 * * @param $tree * * @return array */ public function pre_order($tree) { $stack = []; $output = []; array_push($stack, $tree); while (!empty($stack)) { $center_node = array_pop($stack); $output[] = $center_node->value; if ($center_node->right) { array_push($stack, $center_node->right); } if ($center_node->left) { array_push($stack, $center_node->left); } } return $output; }
* 前序遍历(递归) * * @param $tree * @param $output * * @return array|void */ public function pre_order_recursive($tree, &$output) { if (is_null($tree)) return; else { $output[] = $tree->value; $this->pre_order_recursive($tree->left, $output); $this->pre_order_recursive($tree->right, $output); } return $output; }
* 中序遍历 * 左子树 -> 根节点 -> 右子树 * 需要从下向上遍历,先把左子树压入栈,然后逐个访问根节点和右子树。 * * @param $tree * * @return array */ public function in_order($tree) { $stack = []; $output = []; $center_node = $tree; while (!empty($stack) || $center_node != null) { while ($center_node) { array_push($stack, $center_node); $center_node = $center_node->left; }
$center_node = array_pop($stack); if ($center_node->value) { $output[] = $center_node->value; } $center_node = $center_node->right; } return $output; }
* 中序遍历(递归) * * @param $tree * @param $output * * @return array|void */ public function in_order_recursive($tree, &$output) { if (is_null($tree)) return; else { $this->in_order_recursive($tree->left, $output); $output[] = $tree->value; $this->in_order_recursive($tree->right, $output); } return $output; }
* 后序遍历 * 左子树 -> 右子树 -> 根节点 * 先把根节点存起来,然后依次储存左子树和右子树。然后输出。 * * @param $tree * * @return array */ public function tail_order($tree) { $stack = []; $output = []; array_push($stack, $tree); while (!empty($stack)) { $center_node = array_pop($stack); $output[] = $center_node->value; if ($center_node->left) { array_push($stack, $center_node->left); } if ($center_node->right) { array_push($stack, $center_node->right); } } return array_reverse($output); }
* 后序遍历(递归) * * @param $tree * @param $output * * @return array|void */ public function tail_order_recursive($tree, &$output) { if (is_null($tree)) return; else { $this->tail_order_recursive($tree->left, $output); $this->tail_order_recursive($tree->right, $output); $output[] = $tree->value; } return $output; }
* 前序生成二叉树 * * @param $root * * @return Node|string|void */ public function pre_order_create(&$root) { $data = array_shift($this->data); if (is_null($data)) return; elseif ($data == '#') return $root = null; else { $root = new Node($data); $this->pre_order_create($root->left); $this->pre_order_create($root->right); } return $root; } }
$data = array(0 => 'A', 1 => 'B', 2 => '#', 3 => 'D', 4 => '#', 5 => '#', 6 => 'C', 7 => '#', 8 => '#',);
$bt = new BinaryTree($data);
$tree = $bt->pre_order_create($root);
print_r($tree);
|