Documentation
¶
Overview ¶
Package binaryheap provides a generic min-heap implementation.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Heap ¶
type Heap[T comparable] struct { Comparator utils.Comparator[T] // contains filtered or unexported fields }
Heap represents a min-heap data structure. It is implemented using a slice, where for any element at index i:
- Left child is at index 2*i + 1
- Right child is at index 2*i + 2
- Parent is at index (i-1)/2
func NewWith ¶
func NewWith[T comparable](comparator utils.Comparator[T]) *Heap[T]
NewWith creates a new heap with a custom comparator. The comparator determines the heap ordering (min or max).
func (*Heap[T]) DeleteMin ¶
DeleteMin removes and returns the minimum element from the heap. Returns (zero value, false) if the heap is empty.
func (*Heap[T]) FindMin ¶
FindMin returns the minimum element without removing it. Returns (zero value, false) if the heap is empty.
func (*Heap[T]) Insert ¶
func (heap *Heap[T]) Insert(values ...T)
Insert adds one or more values to the heap. For a single value, it appends and percolates up. For multiple values, it uses Floyd's algorithm for O(n) heap construction.