Documentation ¶
Overview ¶
goheap is a simple implementation of the heap data structure and sorting algorithm. Its intention is for learning purposes of the Go language.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HeapSort ¶
func HeapSort[T constraints.Ordered](slice []T)
Sorts a slice in increasing order. Its time complexity is O(N log N) where N is the size of the slice.
Types ¶
type Heap ¶
type Heap[T any] struct { // contains filtered or unexported fields }
Generic heap structure
func New ¶
Creates a new heap data structure ordered by the lesser function lessFunc and with optional starting values.
func (*Heap[T]) Insert ¶
func (heap *Heap[T]) Insert(value T)
Inserts a value into the heap. If its internal slice is full then it will append the element which breaks the pointer to the old slice. Its time complexity is O(log N) swaps where N is the heap size.
func (*Heap[T]) Remove ¶
Remove the lesser value from the heap, example:
heap := MakeHeap([]int{8, 9, 4, 2, 7}, func(a, b int) bool { return a < b }) value, _ := heap.Remove() value, _ := heap.Remove()
It should output 2 and 4. If the heap is empty, then returns an error. Its time complexity is O(log N) where N is the heap size.