heap

package
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 17, 2025 License: MIT Imports: 0 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fix

func Fix[T any](h Heap[T], i int)

Fix re-establishes the heap ordering after the element at index i has changed its value. Changing the value of the element at index i and then calling Fix is equivalent to, but less expensive than, calling Remove(h, i) followed by a Push of the new value. The complexity is O(log n) where n = h.Len().

func Init

func Init[T any](h Heap[T])

Init establishes the heap invariants required by the other routines in this package. Init is idempotent with respect to the heap invariants and may be called whenever the heap invariants may have been invalidated. The complexity is O(n) where n = h.Len().

func Pop

func Pop[T any](h Heap[T]) any

Pop removes and returns the minimum element (according to Less) from the heap. The complexity is O(log n) where n = h.Len(). Pop is equivalent to Remove(h, 0).

func Push

func Push[T any](h Heap[T], x any)

Push pushes the element x onto the heap. The complexity is O(log n) where n = h.Len().

func Remove

func Remove[T any](h Heap[T], i int) any

Remove removes and returns the element at index i from the heap. The complexity is O(log n) where n = h.Len().

Types

type Heap

type Heap[T any] interface {
	Push(x any)
	Pop() any
	Len() int
	Less(i, j int) bool
	Swap(i, j int)
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL