-
Notifications
You must be signed in to change notification settings - Fork 50
/
MaxHeap.php
83 lines (67 loc) · 1.76 KB
/
MaxHeap.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
/**
* code-segment
*
* @author liu hao<liu546hao@163.com>
* @copyright liu hao<liu546hao@163.com>
*
* 最大堆
*/
abstract class Heap
{
protected $heap = [0];
protected $size = 0;
public function __construct(array $dataList = [])
{
foreach ($dataList as $value) {
$this->size++;
$this->insert($value);
}
}
abstract public function insert($value);
abstract public function delete();
public function sort()
{
$result = [];
$size = $this->size;
for ($i = 1; $i <= $size; $i++) {
$result[] = $this->delete();
}
return $result;
}
}
class MaxHeap extends Heap
{
public function insert($value)
{
for ($i = $this->size; (floor($i/2) > 0) && ($this->heap[floor($i/2)] < $value); $i = floor($i/2)) {
$this->heap[$i] = $this->heap[floor($i/2)];
}
$this->heap[$i] = $value;
}
public function delete()
{
$maxValue = $this->heap[1];
$lastValue = $this->heap[$this->size--];
$child = 1;
for ($i = 1; $i * 2 <= $this->size; $i = $child) {
$child = $i * 2;
$leftIndex = $child;
$rightIndex = $child + 1;
if (($child != $this->size) && ($this->heap[$leftIndex] < $this->heap[$rightIndex])) {
$child++;
}
if ($this->heap[$child] > $lastValue) {
$this->heap[$i] = $this->heap[$child];
} else {
break;
}
}
$this->heap[$i] = $lastValue;
return $maxValue;
}
}
$testData = [33, 4, 3, 2, 5, 6, 9, 10, 32, 23, 45, 11, 25];
$maxHeap = new MaxHeap($testData);
echo '<pre>';
var_dump($maxHeap->sort());