Documentation
¶
Overview ¶
Package heap implements heap data structures.
Heaps are also known as priority queues.
Index ¶
- type Heap
- type IndexedHeap
- func NewIndexedBinary[K, V any](cap int, cmpKey generic.CompareFunc[K], eqVal generic.EqualFunc[V]) IndexedHeap[K, V]
- func NewIndexedBinomial[K, V any](cap int, cmpKey generic.CompareFunc[K], eqVal generic.EqualFunc[V]) IndexedHeap[K, V]
- func NewIndexedFibonacci[K, V any](cap int, cmpKey generic.CompareFunc[K], eqVal generic.EqualFunc[V]) IndexedHeap[K, V]
- type MergeableHeap
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Heap ¶
type Heap[K, V any] interface { Size() int IsEmpty() bool Insert(K, V) Delete() (K, V, bool) DeleteAll() Peek() (K, V, bool) ContainsKey(K) bool ContainsValue(V) bool DOT() string // contains filtered or unexported methods }
Heap represents a heap (priority queue) abstract data type.
func NewBinary ¶ added in v0.3.2
func NewBinary[K, V any](size int, cmpKey generic.CompareFunc[K], eqVal generic.EqualFunc[V]) Heap[K, V]
NewBinary creates a new binary heap that can be used as a priority queue. The heap size will be automatically increased or decreased as needed.
Parameters:
- size is the initial size of the heap (priority queue).
- cmpKey is a function for comparing two keys.
- eqVal is a function for checking the equality of two values.
type IndexedHeap ¶ added in v0.3.2
type IndexedHeap[K, V any] interface { Size() int IsEmpty() bool Insert(int, K, V) bool ChangeKey(int, K) bool Delete() (int, K, V, bool) DeleteIndex(int) (K, V, bool) DeleteAll() Peek() (int, K, V, bool) PeekIndex(int) (K, V, bool) ContainsIndex(int) bool ContainsKey(K) bool ContainsValue(V) bool DOT() string // contains filtered or unexported methods }
IndexedHeap represents an indexed heap (priority queue) abstract data type.
func NewIndexedBinary ¶ added in v0.3.2
func NewIndexedBinary[K, V any](cap int, cmpKey generic.CompareFunc[K], eqVal generic.EqualFunc[V]) IndexedHeap[K, V]
NewIndexedBinary creates a new indexed binary heap that can be used as a priority queue.
An indexed heap (priority queue) associates an index with each key-value pair. It allows changing the key (priority) of an index, deleting by index, and looking up by index. The size of an indexed binary heap is fixed.
Parameters:
- cap is the maximum number of items on the heap.
- cmpKey is a function for comparing two keys.
- eqVal is a function for checking the equality of two values.
func NewIndexedBinomial ¶ added in v0.10.0
func NewIndexedBinomial[K, V any](cap int, cmpKey generic.CompareFunc[K], eqVal generic.EqualFunc[V]) IndexedHeap[K, V]
NewIndexedBinomial creates a new indexed binomial heap that can be used as a priority queue.
An indexed heap (priority queue) associates an index with each key-value pair. It allows changing the key (priority) of an index, deleting by index, and looking up by index. The size of an indexed binary heap is fixed.
The indexed binomial heap does not support the merge operation, as doing so would cause conflicts between indices.
Parameters:
- cap is the maximum number of items on the heap.
- cmpKey is a function for comparing two keys.
- eqVal is a function for checking the equality of two values.
func NewIndexedFibonacci ¶ added in v0.10.0
func NewIndexedFibonacci[K, V any](cap int, cmpKey generic.CompareFunc[K], eqVal generic.EqualFunc[V]) IndexedHeap[K, V]
NewIndexedFibonacci creates a new indexed Fibonacci heap that can be used as a priority queue.
An indexed heap (priority queue) associates an index with each key-value pair. It allows changing the key (priority) of an index, deleting by index, and looking up by index. The size of an indexed binary heap is fixed.
The indexed Fibonacci heap does not support the merge operation, as doing so would cause conflicts between indices.
Parameters:
- cap is the maximum number of items on the heap.
- cmpKey is a function for comparing two keys.
- eqVal is a function for checking the equality of two values.
type MergeableHeap ¶ added in v0.10.0
type MergeableHeap[K, V any] interface { Heap[K, V] Merge(MergeableHeap[K, V]) }
MergeableHeap represents a mergeable heap (priority queue) abstract data type.
func NewBinomial ¶ added in v0.10.0
func NewBinomial[K, V any](cmpKey generic.CompareFunc[K], eqVal generic.EqualFunc[V]) MergeableHeap[K, V]
NewBinomial creates a new binomial heap that can be used as a mergeable priority queue.
Binomial heap is an implementation of the mergeable heap ADT, a priority queue supporting merge operation. A binomial heap is implemented as a set of binomial trees that satisfy the binomial heap properties:
- Heap Property: Every binomial tree in a binomial heap satisfies the min-heap or max-heap property.
- Structural Property: The heap contains at most one binomial tree of any given order.
A binomial tree Bₖ of order k is defined recursively:
- A binomial tree B₀ of order 0 is a single node.
- A binomial tree Bₖ of order k is formed by linking two binomial trees of orders k-1 together, making the root of one tree a child of the root of the other tree. Equivalently, a binomial tree Bₖ has a root node whose children are roots of binomial trees of orders k-1, k-2, ..., 1, 0.
The number inside each node denotes the order of the binomial sub-tree rooted at that node.
[ 0 ] [ 1 ] [ 2 ] [ 3 ]
│ ┌────┴────┐ ┌───────────┼───────────┐
[ 0 ] [ 1 ] [ 0 ] [ 2 ] [ 1 ] [ 0 ]
│ ┌────┴────┐ │
[ 0 ] [ 1 ] [ 0 ] [ 0 ]
│
[ 0 ]
Examples of minimum binomial heaps:
[ 9 ]────[ 1 ]────────[ 3 ]
│ ┌────┴────┐
[ 10 ] [ 5 ] [ 4 ]
│
[ 12 ]
[ 4 ]───────────────────[ 2 ]
┌────────────┼────────────┐
[ 3 ] [ 5 ] [ 8 ]
┌────┴────┐ │
[ 6 ] [ 12 ] [ 11 ]
│
[ 14 ]
Here are some properties of binomial trees:
- The height of a Bₖ tree is k.
- The number of nodes in a Bₖ tree is 2ᵏ.
- The root of a Bₖ tree has k children.
- The children of the root of a Bₖ tree are the roots of B₀, B₁, ..., Bₖ₋₁ trees.
- A binomial tree Bₖ of order k has C(k, d) nodes at depth d, a binomial coefficient.
Parameters:
- cmpKey is a function for comparing two keys.
- eqVal is a function for checking the equality of two values.
func NewFibonacci ¶ added in v0.10.0
func NewFibonacci[K, V any](cmpKey generic.CompareFunc[K], eqVal generic.EqualFunc[V]) MergeableHeap[K, V]
NewFibonacci creates a new Fibonacci heap that can be used as a priority queue.
Fibonacci heap is an implementation of the mergeable heap ADT, a priority queue supporting merge operation. A Fibonacci heap is implemented as a collection of heap-ordered trees. It has a better amortized running time than binary and binomial heaps.
Fibonacci heaps are more flexible than binomial heaps, as their trees do not have a predetermined shape. In the extreme case, a Fibonacci heap can have every item in a separate tree. This flexibility allows some operations to be executed in a lazy manner, postponing the work for later operations.
Parameters:
- cmpKey is a function for comparing two keys.
- eqVal is a function for checking the equality of two values.