Skip to content

Commit 924ee00

Browse files
committed
Merge pull request #1 from okaoka/fix-pop-max-bug
Fix a pop-max bug
2 parents 37ee080 + 46f8dfd commit 924ee00

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

lib/Algorithm/MinMaxHeap.pm6

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ method pop-max() {
5858
elsif (@!nodes.elems == 2) {
5959
return @!nodes.pop;
6060
}
61-
elsif (@!nodes[1] > @!nodes[2]) {
61+
elsif (@!nodes[1] >= @!nodes[2]) {
6262
my $max-value = @!nodes[1];
6363
@!nodes[1] = @!nodes.pop;
6464
self!trickle-down(1);

t/03-pop-max.t

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,23 @@ use Algorithm::MinMaxHeap;
2121
is @actual, [8,7,6,5,4,3,2,1,0];
2222
}
2323

24+
{
25+
my $heap = Algorithm::MinMaxHeap.new();
26+
$heap.insert(0);
27+
$heap.insert(1);
28+
$heap.insert(1);
29+
$heap.insert(3);
30+
$heap.insert(4);
31+
$heap.insert(5);
32+
$heap.insert(6);
33+
$heap.insert(7);
34+
$heap.insert(8);
35+
36+
my @actual;
37+
while (not $heap.is-empty()) {
38+
@actual.push($heap.pop-max);
39+
}
40+
is @actual, [8,7,6,5,4,3,1,1,0];
41+
}
42+
2443
done-testing;

0 commit comments

Comments
 (0)