Files
Acid 35592e1bb8
Go / build (push) Successful in 26s
moved heapSort to its own file
2026-07-24 03:57:15 -04:00

46 lines
938 B
Go

package heap
import (
"cmp"
)
// PriorityQueue
type PriorityQueue[T cmp.Ordered] struct {
heap MinHeap[T]
}
// NewPriorityQueue() -> creates a Priority Queue using min Heap
func NewPriorityQueue[T cmp.Ordered]() *PriorityQueue[T] {
return &PriorityQueue[T]{}
}
// Push() -> adds new value to queue
func (q *PriorityQueue[T]) Push(v T) {
q.heap.Insert(v)
}
// Pop() -> removes and returns the min value
func (q *PriorityQueue[T]) Pop() (T, bool) {
return q.heap.PopMin()
}
// Peek() -> returns the upcoming item without removing it.
// ok is false when the queue is empty.
func (q *PriorityQueue[T]) Peek() (T, bool) {
var zero T
if len(q.heap.Array) == 0 {
return zero, false
}
return q.heap.Array[0], true
}
// Size() -> return the Size of queue
func (q PriorityQueue[T]) Size() int {
return len(q.heap.Array)
}
// Display() -> return the whole queue
func (q PriorityQueue[T]) Display() []T {
return q.heap.Array
}