
在实现二叉树之前,我们需要定义构成树的基本单元——节点(node),以及管理这些节点的二叉树(binarytree)类。
节点(Node)类 一个节点通常包含一个值(value)以及指向其左子节点(left)和右子节点(right)的引用。
<?php
class Node {
public $value;
public $right = null;
public $left = null;
/**
* 构造函数,初始化节点的值。
* 注意:PHP的构造函数是__construct(),不是__constructor()。
*/
public function __construct($value) {
$this->value = $value;
}
}二叉树(BinaryTree)类 二叉树类主要负责管理树的根节点(root),并提供插入、遍历等操作。
class BinaryTree {
public $root;
/**
* 构造函数,初始化二叉树的根节点为null。
*/
public function __construct() {
$this->root = null;
}
// 其他方法将在此处添加
}注意事项:
create方法用于向二叉树中插入新节点。这里我们实现的是二叉搜索树(Binary Search Tree, BST),其特性是左子树的所有节点值小于根节点,右子树的所有节点值大于根节点。
class BinaryTree {
// ... (Node类和BinaryTree的构造函数省略)
/**
* 插入新节点到二叉搜索树。
*
* @param mixed $value 要插入的值。
* @return Node 新插入的节点。
* @throws \Exception 如果节点值已存在。
*/
public function create($value) {
$newNode = new Node($value);
if ($this->root === null) {
$this->root = $newNode;
return $newNode;
}
$current = $this->root;
while($current !== null){
if($current->value > $value){ // 新值小于当前节点值,向左子树移动
if($current->left === null){
$current->left = $newNode;
break; // 插入成功,跳出循环
}else{
$current = $current->left; // 继续向左遍历
}
}else if($current->value < $value){ // 新值大于当前节点值,向右子树移动
if($current->right === null){
$current->right = $newNode;
break; // 插入成功,跳出循环
}else{
$current = $current->right; // 继续向右遍历
}
}else{
// 如果值已存在,抛出异常或选择其他处理方式
throw new \Exception("Node with $value already exists.");
}
}
return $newNode;
}
// ... (其他方法)
}常见陷阱与解决方案:
二叉树的遍历是访问树中所有节点的一种方式。主要有三种深度优先遍历方式:前序遍历(Pre-order)、中序遍历(In-order)和后序遍历(Post-order)。这些遍历通常通过递归实现。
立即学习“PHP免费学习笔记(深入)”;
为了避免前述的“在方法内部定义函数”问题,我们将遍历的递归逻辑封装为私有辅助方法。
class BinaryTree {
// ... (create方法省略)
/**
* 前序遍历(根-左-右)。
*
* @return array 遍历结果。
*/
public function preOrder() {
$visited = [];
if ($this->root !== null) {
$this->traversePreOrder($this->root, $visited);
}
return $visited;
}
/**
* 辅助方法:递归进行前序遍历。
*
* @param Node $node 当前节点。
* @param array $visited 存储已访问节点的数组,通过引用传递。
*/
private function traversePreOrder($node, array &$visited) {
array_push($visited, $node->value);
if ($node->left !== null) {
$this->traversePreOrder($node->left, $visited);
}
if ($node->right !== null) {
$this->traversePreOrder($node->right, $visited);
}
}
/**
* 中序遍历(左-根-右)。
*
* @return array 遍历结果。
*/
public function inOrder() {
$visited = [];
if ($this->root !== null) {
$this->traverseInOrder($this->root, $visited);
}
return $visited;
}
/**
* 辅助方法:递归进行中序遍历。
*
* @param Node $node 当前节点。
* @param array $visited 存储已访问节点的数组,通过引用传递。
*/
private function traverseInOrder($node, array &$visited) {
if ($node->left !== null) {
$this->traverseInOrder($node->left, $visited);
}
array_push($visited, $node->value);
if ($node->right !== null) {
$this->traverseInOrder($node->right, $visited);
}
}
/**
* 后序遍历(左-右-根)。
*
* @return array 遍历结果。
*/
public function postOrder() {
$visited = [];
if ($this->root !== null) {
$this->traversePostOrder($this->root, $visited);
}
return $visited;
}
/**
* 辅助方法:递归进行后序遍历。
*
* @param Node $node 当前节点。
* @param array $visited 存储已访问节点的数组,通过引用传递。
*/
private function traversePostOrder($node, array &$visited) {
if ($node->left !== null) {
$this->traversePostOrder($node->left, $visited);
}
if ($node->right !== null) {
$this->traversePostOrder($node->right, $visited);
}
array_push($visited, $node->value);
}
}常见陷阱与解决方案:
将上述所有修正后的代码整合,并进行测试。
<?php
class Node {
public $value;
public $right = null;
public $left = null;
public function __construct($value) {
$this->value = $value;
}
}
class BinaryTree {
public $root;
public function __construct() {
$this->root = null;
}
public function create($value) {
$newNode = new Node($value);
if ($this->root === null) {
$this->root = $newNode;
return $newNode;
}
$current = $this->root;
while($current !== null){
if($current->value > $value){
if($current->left === null){
$current->left = $newNode;
break;
}else{
$current = $current->left;
}
}else if($current->value < $value){
if($current->right === null){
$current->right = $newNode;
break;
}else{
$current = $current->right;
}
}else{
throw new \Exception("Node with $value already exists.");
}
}
return $newNode;
}
public function preOrder() {
$visited = [];
if ($this->root !== null) {
$this->traversePreOrder($this->root, $visited);
}
return $visited;
}
private function traversePreOrder($node, array &$visited) {
array_push($visited, $node->value);
if ($node->left !== null) {
$this->traversePreOrder($node->left, $visited);
}
if ($node->right !== null) {
$this->traversePreOrder($node->right, $visited);
}
}
public function postOrder() {
$visited = [];
if ($this->root !== null) {
$this->traversePostOrder($this->root, $visited);
}
return $visited;
}
private function traversePostOrder($node, array &$visited) {
if ($node->left !== null) {
$this->traversePostOrder($node->left, $visited);
}
if ($node->right !== null) {
$this->traversePostOrder($node->right, $visited);
}
array_push($visited, $node->value);
}
public function inOrder() {
$visited = [];
if ($this->root !== null) {
$this->traverseInOrder($this->root, $visited);
}
return $visited;
}
private function traverseInOrder($node, array &$visited) {
if ($node->left !== null) {
$this->traverseInOrder($node->left, $visited);
}
array_push($visited, $node->value);
if ($node->right !== null) {
$this->traverseInOrder($node->right, $visited);
}
}
}
$tree = new BinaryTree();
try {
$tree->create(50);
$tree->create(30);
$tree->create(45);
$tree->create(12);
$tree->create(29);
$tree->create(70);
$tree->create(60);
$tree->create(80);
$tree->create(75);
echo "inOrder: ". implode(",", $tree->inOrder()), PHP_EOL;
echo "preOrder: ". implode(",", $tree->preOrder()), PHP_EOL;
echo "postOrder: ". implode(",", $tree->postOrder()), PHP_EOL;
} catch (\Exception $e) {
echo "Error: " . $e->getMessage() . PHP_EOL;
}输出结果:
inOrder: 12,29,30,45,50,60,70,75,80 preOrder: 50,30,12,29,45,70,60,80,75 postOrder: 29,12,45,30,60,75,80,70,50
其他注意事项:
通过本教程,我们不仅实现了一个功能完善的PHP二叉搜索树,还深入探讨了在PHP中实现此类数据结构时可能遇到的常见陷阱。关键点包括:
遵循这些最佳实践,可以帮助开发者编写出更健壮、可维护且高效的PHP代码。
以上就是PHP二叉树实现与常见陷阱:构建、遍历及优化实践的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号