Help language development. Donate to The Perl Foundation

Algorithm-Heap-Binary-0.0.1/

Algorithm::Heap::Binary - Implementation of a BinaryHeap

```
use Algorithm::Heap::Binary;
my Algorithm::Heap::Binary $heap .= new(
comparator => * <=> *,
3 => 'c',
2 => 'b',
1 => 'a'
);
$heap.size.say; # 3
# heap-sort example
$heap.delete-min.value.say; # a
$heap.delete-min.value.say; # b
$heap.delete-min.value.say; # c
```

Algorithm::Heap::Binary provides to you BinaryHeap data structure with basic heap operations defined in Algorithm::Heap role:

find a maximum item of a max-heap, or a minimum item of a min-heap, respectively

returns the node of maximum value from a max heap [or minimum value from a min heap] after removing it from the heap

removing the root node of a max heap [or min heap]

pop root and push a new key. More efficient than pop followed by push, since only need to balance once, not twice, and appropriate for fixed-size heaps

return true if the heap is empty, false otherwise

return the number of items in the heap

joining two heaps to form a valid new heap containing all the elements of both, preserving the original heaps

BinaryHeap contains `Pair`

objects and define order between `Pair.key`

by the comparator. Comparator - is a `Code`

which defines how to order elements internally. With help of the comparator you can create Min-heap or Max-heap.

empty constructor

`my $heap = Algorithm::Heap::Binary.new;`

Default comparator is: `* <=> *`

named constructor

`my $heap = Algorithm::Heap::Binary.new(comparator => -> $a, $b {$b cmp $a});`

constructor with heapify

`my @numbers = 1 .. *; my @letters = 'a' .. *; my @data = @numbers Z=> @letters; my $heap = Algorithm::Heap::Binary.new(comparator => * <=> *, |@data[^5]);`

This will automatically heapify data for you.

Clones heap object for you with all internal data.

Returns `Bool`

result as to empty Heap or not.

Returns `Int`

which corresponds to amount elements in the Heap data structure.

Adds new Pair to the heap and resort it.

Alias for push method.

Returns `Pair`

from the top of the Heap.

Just an syntatic alias for peek method.

Just an syntatic alias for peek method.

Returns `Piar`

from the top of the Heap and also removes it from the Heap.

Just an syntatic alias for pop method.

Just an syntatic alias for pop method.

Replace top element with another Pair. Returns replaced element as a result.

Construct a new Heap merging current one and passed to as an argument.

Returns `Seq`

of Heap elements. This will clone the data for you, so initial data structure going to be untouched.

Prints internal representation of the Heap (as an `Array`

).

Method wich provides iterator (`role Iterable`

). Will clone current Heap for you.

Internal method to make sift-up operation.

Internal method to make sift-down operation.

Copyright 2018 cono

This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.