diff --git a/php/24_tree/Tree.php b/php/24_tree/Tree.php new file mode 100644 index 0000000..97fd14b --- /dev/null +++ b/php/24_tree/Tree.php @@ -0,0 +1,152 @@ +head = new TreeNode($headData); + } + } + + /** + * 查找数据 + * @param [type] $data [数据] + * @return [type] [description] + */ + public function find($data) + { + if ($this->head == null) { + return null; + } + + $node = $this->head; + + while ($node != null) { + if ($node->data == $data) { + return $node; + } elseif ($data > $node->data) { + $node = $node->right; + } else { + $node = $node->left; + } + } + + return null; + } + + /** + * 插入数据 + * @param [type] $data [数据] + * @return [type] [description] + */ + public function insert($data) + { + if ($this->head == null) { + $this->head = new TreeNode($data); + return true; + } + + $node = $this->head; + + while ($node != null) { + if ($data > $node->data) { + if ($node->right == null) { + $node->right = new TreeNode($data); + return true; + } + $node = $node->right; + } else { + if ($node->left == null) { + $node->left = new TreeNode($data); + return true; + } + $node = $node->left; + } + } + } + + /** + * 删除节点 + * @param [type] $data [节点] + * @return [type] [description] + */ + public function delete($data) + { + // 找到需要删除节点 + $node = $this->head; + $pnode = null; + while ($node != null) { + if ($node->data == $data) { + break; + } elseif ($data > $node->data) { + $pnode = $node; + $node = $node->right; + } else { + $pnode = $node; + $node = $node->left; + } + } + if ($node == null) { + return false; + } + // 要删除的节点有两个子节点 + // 查找右子树中最小节点 + if ($node->left != null && $node->right != null) { + $minPP = $node; + $minP = $node->right; + while ($minP->left != null) { + $minPP = $minP; + $minP = $minP->left; + } + $node->data = $minP->data; + $node = $minP; + // 删除掉右子树中的最小节点 + $minPP->left = null; + } + + if ($node->left != null) { + $child = $node->left; + } elseif ($node->right != null) { + $child = $node->right; + } else { + $child = null; + } + + if ($pnode == null) { + // 删除的是根节点 + $node = $child; + } elseif ($pnode->left == $node) { + $pnode->left = $child; + } else { + $pnode->right = $child; + } + } + + /** + * 前序遍历 + * @return [type] [description] + */ + public function preOrder($node) + { + if ($node == null) { + return ; + } + echo $node->data . '->'; + $this->preOrder($node->left); + $this->preOrder($node->right); + } +} \ No newline at end of file diff --git a/php/24_tree/TreeNode.php b/php/24_tree/TreeNode.php new file mode 100644 index 0000000..21c3e1a --- /dev/null +++ b/php/24_tree/TreeNode.php @@ -0,0 +1,36 @@ +data = $data; + $this->left = null; + $this->right = null; + } +} \ No newline at end of file diff --git a/php/24_tree/main.php b/php/24_tree/main.php new file mode 100644 index 0000000..f68ba9f --- /dev/null +++ b/php/24_tree/main.php @@ -0,0 +1,25 @@ +insert(20); +$tree->insert(30); +$tree->insert(10); +$tree->insert(21); +$tree->insert(22); + +$tree->preOrder($tree->head); +echo PHP_EOL; + +var_dump($tree->find(30)); +echo PHP_EOL; + + +$tree->delete(30); +$tree->preOrder($tree->head); +echo PHP_EOL; \ No newline at end of file diff --git a/php/composer.json b/php/composer.json index eef8f49..ee6e4b1 100644 --- a/php/composer.json +++ b/php/composer.json @@ -8,7 +8,8 @@ "Algo_06\\": "06_linkedlist/", "Algo_07\\": "07_linkedlist/", "Algo_08\\": "08_stack/", - "Algo_09\\": "09_queue/" + "Algo_09\\": "09_queue/", + "Algo_24\\": "24_tree/" } } }