Skip to content

Commit

Permalink
Merge pull request #1 from okaoka/fix-pop-max-bug
Browse files Browse the repository at this point in the history
Fix a pop-max bug
  • Loading branch information
titsuki committed Mar 13, 2016
2 parents 37ee080 + 46f8dfd commit 924ee00
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/Algorithm/MinMaxHeap.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ method pop-max() {
elsif (@!nodes.elems == 2) {
return @!nodes.pop;
}
elsif (@!nodes[1] > @!nodes[2]) {
elsif (@!nodes[1] >= @!nodes[2]) {
my $max-value = @!nodes[1];
@!nodes[1] = @!nodes.pop;
self!trickle-down(1);
Expand Down
19 changes: 19 additions & 0 deletions t/03-pop-max.t
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,23 @@ use Algorithm::MinMaxHeap;
is @actual, [8,7,6,5,4,3,2,1,0];
}

{
my $heap = Algorithm::MinMaxHeap.new();
$heap.insert(0);
$heap.insert(1);
$heap.insert(1);
$heap.insert(3);
$heap.insert(4);
$heap.insert(5);
$heap.insert(6);
$heap.insert(7);
$heap.insert(8);

my @actual;
while (not $heap.is-empty()) {
@actual.push($heap.pop-max);
}
is @actual, [8,7,6,5,4,3,1,1,0];
}

done-testing;

0 comments on commit 924ee00

Please sign in to comment.