Algorithm::SkewHeap - a mergable min heap
0.0.1
use Algorithm::SkewHeap;
my $heap = Algorithm::SkewHeap.new;
for (1 .. 1000).pick(1000) -> $n {
$heap.put($n);
}
until $heap.is-empty {
my $n = $heap.take;
}
$heap.merge($other-heap);
A skew heap is a type of heap based on a binary tree in which all operations are based on merging subtrees, making it possible to quickly combine multiple heaps, while still retaining speed and efficiency. Ammortized performance is O(log n) or better (see https://en.wikipedia.org/wiki/Skew_heap).
Items in the heap are returned with the lowest first. Comparisons are done with the greater than operator, which may be overloaded as needed for types intended to be used in the heap.
SkewHeap class
method size() returns IntReturns the number of items in the heap
method is-empty() returns BoolReturns true when the heap is empty
method top() returns AnyReturns the top item in the heap without removing it.
method take() returns AnyRemoves and returns the top item in the heap.
method put(
$value
) returns IntAdds a new item to the heap. Returns the new size of the heap.
method merge(
Algorithm::SkewHeap $other
) returns IntDestructively merges with another heap. The other heap should be considered unusable afterward. Returns the new size of the heap.
method explain() returns NilPrints the structure of the heap for debugging purposes.