diff --git a/lib/Algorithm/MinMaxHeap.pm6 b/lib/Algorithm/MinMaxHeap.pm6 index 268e5aa..fefcabc 100644 --- a/lib/Algorithm/MinMaxHeap.pm6 +++ b/lib/Algorithm/MinMaxHeap.pm6 @@ -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); diff --git a/t/03-pop-max.t b/t/03-pop-max.t index 5859e74..b4a4e2f 100644 --- a/t/03-pop-max.t +++ b/t/03-pop-max.t @@ -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;