Skip to content

Commit f3f7d60

Browse files
Merge pull request wangzheng0822#270 from qqlcbb/master
完善24二叉树基本算法
2 parents 2984168 + 058c7fa commit f3f7d60

File tree

4 files changed

+215
-1
lines changed

4 files changed

+215
-1
lines changed

php/24_tree/Tree.php

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
3+
namespace Algo_24;
4+
5+
class Tree
6+
{
7+
8+
/**
9+
* 树的根节点
10+
* @var [type]
11+
*/
12+
public $head = null;
13+
14+
/**
15+
* [__construct description]
16+
* @param TreeNode|null $head [description]
17+
*/
18+
public function __construct($headData = null)
19+
{
20+
if ($headData != null) {
21+
$this->head = new TreeNode($headData);
22+
}
23+
}
24+
25+
/**
26+
* 查找数据
27+
* @param [type] $data [数据]
28+
* @return [type] [description]
29+
*/
30+
public function find($data)
31+
{
32+
if ($this->head == null) {
33+
return null;
34+
}
35+
36+
$node = $this->head;
37+
38+
while ($node != null) {
39+
if ($node->data == $data) {
40+
return $node;
41+
} elseif ($data > $node->data) {
42+
$node = $node->right;
43+
} else {
44+
$node = $node->left;
45+
}
46+
}
47+
48+
return null;
49+
}
50+
51+
/**
52+
* 插入数据
53+
* @param [type] $data [数据]
54+
* @return [type] [description]
55+
*/
56+
public function insert($data)
57+
{
58+
if ($this->head == null) {
59+
$this->head = new TreeNode($data);
60+
return true;
61+
}
62+
63+
$node = $this->head;
64+
65+
while ($node != null) {
66+
if ($data > $node->data) {
67+
if ($node->right == null) {
68+
$node->right = new TreeNode($data);
69+
return true;
70+
}
71+
$node = $node->right;
72+
} else {
73+
if ($node->left == null) {
74+
$node->left = new TreeNode($data);
75+
return true;
76+
}
77+
$node = $node->left;
78+
}
79+
}
80+
}
81+
82+
/**
83+
* 删除节点
84+
* @param [type] $data [节点]
85+
* @return [type] [description]
86+
*/
87+
public function delete($data)
88+
{
89+
// 找到需要删除节点
90+
$node = $this->head;
91+
$pnode = null;
92+
while ($node != null) {
93+
if ($node->data == $data) {
94+
break;
95+
} elseif ($data > $node->data) {
96+
$pnode = $node;
97+
$node = $node->right;
98+
} else {
99+
$pnode = $node;
100+
$node = $node->left;
101+
}
102+
}
103+
if ($node == null) {
104+
return false;
105+
}
106+
// 要删除的节点有两个子节点
107+
// 查找右子树中最小节点
108+
if ($node->left != null && $node->right != null) {
109+
$minPP = $node;
110+
$minP = $node->right;
111+
while ($minP->left != null) {
112+
$minPP = $minP;
113+
$minP = $minP->left;
114+
}
115+
$node->data = $minP->data;
116+
$node = $minP;
117+
// 删除掉右子树中的最小节点
118+
$minPP->left = null;
119+
}
120+
121+
if ($node->left != null) {
122+
$child = $node->left;
123+
} elseif ($node->right != null) {
124+
$child = $node->right;
125+
} else {
126+
$child = null;
127+
}
128+
129+
if ($pnode == null) {
130+
// 删除的是根节点
131+
$node = $child;
132+
} elseif ($pnode->left == $node) {
133+
$pnode->left = $child;
134+
} else {
135+
$pnode->right = $child;
136+
}
137+
}
138+
139+
/**
140+
* 前序遍历
141+
* @return [type] [description]
142+
*/
143+
public function preOrder($node)
144+
{
145+
if ($node == null) {
146+
return ;
147+
}
148+
echo $node->data . '->';
149+
$this->preOrder($node->left);
150+
$this->preOrder($node->right);
151+
}
152+
}

php/24_tree/TreeNode.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Algo_24;
4+
5+
class TreeNode
6+
{
7+
8+
/**
9+
* 节点中的数据
10+
* @var [type]
11+
*/
12+
public $data;
13+
14+
/**
15+
* 左节点
16+
* @var [type]
17+
*/
18+
public $left;
19+
20+
/**
21+
* 右节点
22+
* @var [type]
23+
*/
24+
public $right;
25+
26+
/**
27+
* [__construct description]
28+
* @param [type] $data [description]
29+
*/
30+
public function __construct($data = null)
31+
{
32+
$this->data = $data;
33+
$this->left = null;
34+
$this->right = null;
35+
}
36+
}

php/24_tree/main.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Algo_24;
4+
5+
require_once '../vendor/autoload.php';
6+
7+
8+
$tree = new Tree();
9+
10+
$tree->insert(20);
11+
$tree->insert(30);
12+
$tree->insert(10);
13+
$tree->insert(21);
14+
$tree->insert(22);
15+
16+
$tree->preOrder($tree->head);
17+
echo PHP_EOL;
18+
19+
var_dump($tree->find(30));
20+
echo PHP_EOL;
21+
22+
23+
$tree->delete(30);
24+
$tree->preOrder($tree->head);
25+
echo PHP_EOL;

php/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"Algo_06\\": "06_linkedlist/",
99
"Algo_07\\": "07_linkedlist/",
1010
"Algo_08\\": "08_stack/",
11-
"Algo_09\\": "09_queue/"
11+
"Algo_09\\": "09_queue/",
12+
"Algo_24\\": "24_tree/"
1213
}
1314
}
1415
}

0 commit comments

Comments
 (0)