Skip to content

Commit d8ce80a

Browse files
Merge pull request wangzheng0822#403 from hkui/master
添加php 树的层级遍历,中序后续遍历,以及修改之前堆的提交author信息
2 parents d7aa998 + 0ce7459 commit d8ce80a

File tree

8 files changed

+119
-2
lines changed

8 files changed

+119
-2
lines changed

php/10_heap/Heap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Date: 2019/9/5
66
* Time: 9:01
77
* 堆的基本操作(大顶堆,小顶堆 几种堆化方式,堆排序,动态数据流查top k ,查中位数)
8+
*
89
*/
910
namespace Algo_10;
1011

php/10_heap/findmiddle.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
<?php
2+
/**
3+
* Created by PhpStorm.
4+
5+
* 动态数据流实时获取中位数
6+
*/
27
namespace Algo_10;
38

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

php/10_heap/main.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
<?php
2+
/**
3+
* Created by PhpStorm.
4+
5+
* 堆的基本操作
6+
*/
7+
28
namespace Algo_10;
39

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

php/10_heap/topn.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php
22
/**
3-
*2.动态数据集合求top n
3+
* Created by PhpStorm.
4+
5+
6+
*动态数据集合求top n
47
*/
58
namespace Algo_10;
69

php/24_tree/Tree.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,61 @@ public function preOrder($node)
149149
$this->preOrder($node->left);
150150
$this->preOrder($node->right);
151151
}
152+
153+
/**中序遍历
154+
* @param $node
155+
*
156+
*/
157+
public function inOrder($node){
158+
if(empty($node)){
159+
return;
160+
}
161+
$this->inOrder($node->left);
162+
echo $node->data . ' ';
163+
$this->inOrder($node->right);
164+
165+
}
166+
167+
/**
168+
* @param $node
169+
* 后续遍历
170+
*/
171+
public function postOrder($node){
172+
if(empty($node)){
173+
return;
174+
}
175+
$this->postOrder($node->left);
176+
$this->postOrder($node->right);
177+
echo $node->data . ' ';
178+
179+
}
180+
/**
181+
* @param $queue
182+
* @param int $index 从队列(数组)的那个位置开始处理
183+
* 层级遍历
184+
* 首先把节点放入数组,记录放入数组的根节点个数index,把节点的左右子放入数组
185+
* 开始遍历数组queue(从index开始,子节点已经入队列的节点元素不再处理),把左右子节点放入queue,index++
186+
* 持续上述过程,当节点没有子节点时,入队列过程结束,queue里节点的顺序即为层级遍历元素节点的顺序
187+
*
188+
* 完全二叉树
189+
*/
190+
public function levelOrder($queue, $index = 0)
191+
{
192+
for ($i = $index; $i < count($queue); $i++) {
193+
$node = $queue[$i];
194+
if ($node->left) {
195+
$queue[] = $node->left;
196+
} else {
197+
return $queue;
198+
}
199+
if ($node->right) {
200+
$queue[] = $node->right;
201+
} else {
202+
return $queue;
203+
}
204+
$index++;
205+
}
206+
return $queue;
207+
208+
}
152209
}

php/24_tree/levelOrder.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
5+
* Date: 2019/9/22
6+
* Time: 23:30
7+
*
8+
*二叉树的层级遍历
9+
*/
10+
11+
12+
namespace Algo_24;
13+
14+
require_once '../vendor/autoload.php';
15+
16+
$tree=new Tree(20);
17+
$tree->insert(16);
18+
$tree->insert(30);
19+
$tree->insert(12);
20+
$tree->insert(19);
21+
22+
$tree->insert(10);
23+
$tree->insert(15);
24+
$tree->insert(18);
25+
$tree->insert(21);
26+
$tree->insert(38);
27+
28+
29+
$q=$tree->levelOrder([$tree->head]);
30+
31+
foreach ($q as $n){
32+
echo $n->data." ";
33+
}
34+
echo PHP_EOL;

php/24_tree/main.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,22 @@
99

1010
$tree->insert(20);
1111
$tree->insert(30);
12+
$tree->insert(40);
1213
$tree->insert(10);
1314
$tree->insert(21);
1415
$tree->insert(22);
1516

1617
$tree->preOrder($tree->head);
1718
echo PHP_EOL;
1819

19-
var_dump($tree->find(30));
20+
$tree->inOrder($tree->head);
21+
echo PHP_EOL;
22+
23+
$tree->postOrder($tree->head);
24+
echo PHP_EOL;
25+
26+
27+
print_r($tree->find(30));
2028
echo PHP_EOL;
2129

2230

php/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,6 @@
2525
* main 堆的基本操作,堆排序
2626
* findmiddle 动态数据流求中位数
2727
* topn 动态数据流求top k
28+
#### 24_tree
29+
* main 二叉树的基本操作 前中后序遍历
30+
* levelOrder 二叉树的层级遍历

0 commit comments

Comments
 (0)