-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMinHeap.php
114 lines (92 loc) Β· 2.45 KB
/
MinHeap.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
namespace leetcode\util;
class MinHeap
{
private array $heap;
public function __construct()
{
$this->heap = [];
}
public function push(int $val): void
{
$this->heap[] = $val;
$this->siftUp(count($this->heap) - 1);
}
public function pop()
{
if (empty($this->heap)) {
throw new \InvalidArgumentException('Min heap is empty');
}
$leaf = array_pop($this->heap);
if (empty($this->heap)) {
return $leaf;
}
$value = $this->getRoot();
$this->setRoot($leaf);
return $value;
}
public function count(): int
{
return count($this->heap);
}
public function top()
{
return current($this->heap);
}
public function compare(int $a, int $b): int
{
return $a <=> $b;
}
public function getRoot(): int
{
return $this->heap[0];
}
public function setRoot(int $leaf): void
{
$this->heap[0] = $leaf;
$this->siftDown(0);
}
private function siftUp(int $leaf): void
{
while ($leaf > 0) {
$parent = $this->parent($leaf);
if ($this->compare($this->heap[$leaf], $this->heap[$parent]) > 0) {
break;
}
[$this->heap[$leaf], $this->heap[$parent]] = [$this->heap[$parent], $this->heap[$leaf]];
$leaf = $parent;
}
}
private function siftDown(int $node): void
{
$last = (int) floor(count($this->heap) / 2);
for ($parent = $node; $parent < $last; $parent = $leaf) {
$leaf = $this->getSmallestLeaf($parent);
if ($this->compare($this->heap[$parent], $this->heap[$leaf]) < 0) {
break;
}
[$this->heap[$leaf], $this->heap[$parent]] = [$this->heap[$parent], $this->heap[$leaf]];
}
}
private function getSmallestLeaf(int $parent): int
{
$left = $this->left($parent);
$right = $this->right($parent);
if ($right < count($this->heap) && $this->compare($this->heap[$left], $this->heap[$right]) > 0) {
return $right;
}
return $left;
}
private function left(int $index): int
{
return ($index * 2) + 1;
}
private function right(int $index): int
{
return ($index * 2) + 2;
}
private function parent(int $index): int
{
return ($index - 1) / 2;
}
}