Skip to content

Commit

Permalink
Merge pull request wangzheng0822#270 from qqlcbb/master
Browse files Browse the repository at this point in the history
完善24二叉树基本算法
  • Loading branch information
wangzheng0822 authored Mar 14, 2019
2 parents 2984168 + 058c7fa commit f3f7d60
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 1 deletion.
152 changes: 152 additions & 0 deletions php/24_tree/Tree.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php

namespace Algo_24;

class Tree
{

/**
* 树的根节点
* @var [type]
*/
public $head = null;

/**
* [__construct description]
* @param TreeNode|null $head [description]
*/
public function __construct($headData = null)
{
if ($headData != null) {
$this->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);
}
}
36 changes: 36 additions & 0 deletions php/24_tree/TreeNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Algo_24;

class TreeNode
{

/**
* 节点中的数据
* @var [type]
*/
public $data;

/**
* 左节点
* @var [type]
*/
public $left;

/**
* 右节点
* @var [type]
*/
public $right;

/**
* [__construct description]
* @param [type] $data [description]
*/
public function __construct($data = null)
{
$this->data = $data;
$this->left = null;
$this->right = null;
}
}
25 changes: 25 additions & 0 deletions php/24_tree/main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Algo_24;

require_once '../vendor/autoload.php';


$tree = new Tree();

$tree->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;
3 changes: 2 additions & 1 deletion php/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
}
}
}

0 comments on commit f3f7d60

Please sign in to comment.