Documentation ¶
Overview ¶
Package binaryheap implements a binary heap backed by array list.
Comparator defines this heap as either min or max heap.
Structure is not thread safe.
References: http://en.wikipedia.org/wiki/Binary_heap
Index ¶
- type Heap
- func (heap *Heap[T]) Clear()
- func (heap *Heap[T]) Empty() bool
- func (heap *Heap[T]) FromJSON(data []byte) error
- func (heap *Heap[T]) Iterator() *Iterator[T]
- func (heap *Heap[T]) MarshalJSON() ([]byte, error)
- func (heap *Heap[T]) Peek() (value T, ok bool)
- func (heap *Heap[T]) Pop() (value T, ok bool)
- func (heap *Heap[T]) Push(values ...T)
- func (heap *Heap[T]) Size() int
- func (heap *Heap[T]) String() string
- func (heap *Heap[T]) ToJSON() ([]byte, error)
- func (heap *Heap[T]) UnmarshalJSON(bytes []byte) error
- func (heap *Heap[T]) Values() []T
- type Iterator
- func (iterator *Iterator[T]) Begin()
- func (iterator *Iterator[T]) End()
- func (iterator *Iterator[T]) First() bool
- func (iterator *Iterator[T]) Index() int
- func (iterator *Iterator[T]) Last() bool
- func (iterator *Iterator[T]) Next() bool
- func (iterator *Iterator[T]) NextTo(f func(index int, value T) bool) bool
- func (iterator *Iterator[T]) Prev() bool
- func (iterator *Iterator[T]) PrevTo(f func(index int, value T) bool) bool
- func (iterator *Iterator[T]) Value() T
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 holds elements in an array-list
func NewWith ¶
func NewWith[T comparable](comparator utils.Comparator[T]) *Heap[T]
NewWith instantiates a new empty heap tree with the custom comparator.
func (*Heap[T]) Iterator ¶
Iterator returns a stateful iterator whose values can be fetched by an index.
func (*Heap[T]) MarshalJSON ¶
MarshalJSON @implements json.Marshaler
func (*Heap[T]) Peek ¶
Peek returns top element on the heap without removing it, or nil if heap is empty. Second return parameter is true, unless the heap was empty and there was nothing to peek.
func (*Heap[T]) Pop ¶
Pop removes top element on heap and returns it, or nil if heap is empty. Second return parameter is true, unless the heap was empty and there was nothing to pop.
func (*Heap[T]) Push ¶
func (heap *Heap[T]) Push(values ...T)
Push adds a value onto the heap and bubbles it up accordingly.
func (*Heap[T]) UnmarshalJSON ¶
UnmarshalJSON @implements json.Unmarshaler
type Iterator ¶
type Iterator[T comparable] struct { // contains filtered or unexported fields }
Iterator returns a stateful iterator whose values can be fetched by an index.
func (*Iterator[T]) Begin ¶
func (iterator *Iterator[T]) Begin()
Begin resets the iterator to its initial state (one-before-first) Call Next() to fetch the first element if any.
func (*Iterator[T]) End ¶
func (iterator *Iterator[T]) End()
End moves the iterator past the last element (one-past-the-end). Call Prev() to fetch the last element if any.
func (*Iterator[T]) First ¶
First moves the iterator to the first element and returns true if there was a first element in the container. If First() returns true, then first element's index and value can be retrieved by Index() and Value(). Modifies the state of the iterator.
func (*Iterator[T]) Index ¶
Index returns the current element's index. Does not modify the state of the iterator.
func (*Iterator[T]) Last ¶
Last moves the iterator to the last element and returns true if there was a last element in the container. If Last() returns true, then last element's index and value can be retrieved by Index() and Value(). Modifies the state of the iterator.
func (*Iterator[T]) Next ¶
Next moves the iterator to the next element and returns true if there was a next element in the container. If Next() returns true, then next element's index and value can be retrieved by Index() and Value(). If Next() was called for the first time, then it will point the iterator to the first element if it exists. Modifies the state of the iterator.
func (*Iterator[T]) NextTo ¶
NextTo moves the iterator to the next element from current position that satisfies the condition given by the passed function, and returns true if there was a next element in the container. If NextTo() returns true, then next element's index and value can be retrieved by Index() and Value(). Modifies the state of the iterator.
func (*Iterator[T]) Prev ¶
Prev moves the iterator to the previous element and returns true if there was a previous element in the container. If Prev() returns true, then previous element's index and value can be retrieved by Index() and Value(). Modifies the state of the iterator.
func (*Iterator[T]) PrevTo ¶
PrevTo moves the iterator to the previous element from current position that satisfies the condition given by the passed function, and returns true if there was a next element in the container. If PrevTo() returns true, then next element's index and value can be retrieved by Index() and Value(). Modifies the state of the iterator.