php 二叉树
时间: 2023-07-26 17:10:58 浏览: 54
以下是 PHP 实现二叉树的基本代码:
```
class TreeNode {
public $value;
public $left;
public $right;
public function __construct($val) {
$this->value = $val;
$this->left = null;
$this->right = null;
}
}
class BinaryTree {
public $root;
public function __construct() {
$this->root = null;
}
public function insert($val) {
$new_node = new TreeNode($val);
if ($this->root == null) {
$this->root = $new_node;
return;
}
$current = $this->root;
while (true) {
if ($val < $current->value) {
if ($current->left == null) {
$current->left = $new_node;
return;
} else {
$current = $current->left;
}
} else {
if ($current->right == null) {
$current->right = $new_node;
return;
} else {
$current = $current->right;
}
}
}
}
public function search($val) {
$current = $this->root;
while ($current != null) {
if ($val == $current->value) {
return $current;
} elseif ($val < $current->value) {
$current = $current->left;
} else {
$current = $current->right;
}
}
return null;
}
public function delete($val) {
$parent = null;
$current = $this->root;
$is_left_child = false;
while ($current != null) {
if ($val == $current->value) {
if ($current->left == null && $current->right == null) {
if ($current == $this->root) {
$this->root = null;
} elseif ($is_left_child) {
$parent->left = null;
} else {
$parent->right = null;
}
} elseif ($current->left == null) {
if ($current == $this->root) {
$this->root = $current->right;
} elseif ($is_left_child) {
$parent->left = $current->right;
} else {
$parent->right = $current->right;
}
} elseif ($current->right == null) {
if ($current == $this->root) {
$this->root = $current->left;
} elseif ($is_left_child) {
$parent->left = $current->left;
} else {
$parent->right = $current->left;
}
} else {
$successor = $this->get_successor($current);
if ($current == $this->root) {
$this->root = $successor;
} elseif ($is_left_child) {
$parent->left = $successor;
} else {
$parent->right = $successor;
}
$successor->left = $current->left;
}
return;
} elseif ($val < $current->value) {
$parent = $current;
$current = $current->left;
$is_left_child = true;
} else {
$parent = $current;
$current = $current->right;
$is_left_child = false;
}
}
}
public function get_successor($del_node) {
$successor_parent = $del_node;
$successor = $del_node;
$current = $del_node->right;
while ($current != null) {
$successor_parent = $successor;
$successor = $current;
$current = $current->left;
}
if ($successor != $del_node->right) {
$successor_parent->left = $successor->right;
$successor->right = $del_node->right;
}
return $successor;
}
}
// 示例用法
$tree = new BinaryTree();
$tree->insert(5);
$tree->insert(3);
$tree->insert(7);
$tree->insert(1);
$tree->insert(4);
$tree->insert(6);
$tree->insert(8);
print_r($tree->search(5)); // 输出: TreeNode Object ( [value] => 5 [left] => TreeNode Object ( [value] => 3 [left] => TreeNode Object ( [value] => 1 [left] => [right] => ) [right] => TreeNode Object ( [value] => 4 [left] => [right] => ) ) [right] => TreeNode Object ( [value] => 7 [left] => TreeNode Object ( [value] => 6 [left] => [right] => ) [right] => TreeNode Object ( [value] => 8 [left] => [right] => ) ) )
$tree->delete(4);
print_r($tree->search(4)); // 输出: null
```
上面的代码实现了二叉树的基本功能,包括插入节点、查找节点和删除节点。在实际使用时,可根据需要进行修改和扩展。
阅读全文