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) Clear()
- func (heap *Heap) Empty() bool
- func (heap *Heap) FromJSON(data []byte) error
- func (heap *Heap) Iterator() Iterator
- func (heap *Heap) MarshalJSON() ([]byte, error)
- func (heap *Heap) Peek() (value interface{}, ok bool)
- func (heap *Heap) Pop() (value interface{}, ok bool)
- func (heap *Heap) Push(values ...interface{})
- func (heap *Heap) Size() int
- func (heap *Heap) String() string
- func (heap *Heap) ToJSON() ([]byte, error)
- func (heap *Heap) UnmarshalJSON(bytes []byte) error
- func (heap *Heap) Values() []interface{}
- type Iterator
- func (iterator *Iterator) Begin()
- func (iterator *Iterator) End()
- func (iterator *Iterator) First() bool
- func (iterator *Iterator) Index() int
- func (iterator *Iterator) Last() bool
- func (iterator *Iterator) Next() bool
- func (iterator *Iterator) NextTo(f func(index int, value interface{}) bool) bool
- func (iterator *Iterator) Prev() bool
- func (iterator *Iterator) PrevTo(f func(index int, value interface{}) bool) bool
- func (iterator *Iterator) Value() interface{}
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Heap ¶
type Heap struct { Comparator utils.Comparator // contains filtered or unexported fields }
Heap holds elements in an array-list
func NewWith ¶
func NewWith(comparator utils.Comparator) *Heap
NewWith instantiates a new empty heap tree with the custom comparator.
func NewWithIntComparator ¶
func NewWithIntComparator() *Heap
NewWithIntComparator instantiates a new empty heap with the IntComparator, i.e. elements are of type int.
func NewWithStringComparator ¶
func NewWithStringComparator() *Heap
NewWithStringComparator instantiates a new empty heap with the StringComparator, i.e. elements are of type string.
func (*Heap) FromJSON ¶ added in v1.9.0
FromJSON populates the heap from the input JSON representation.
func (*Heap) Iterator ¶
Iterator returns a stateful iterator whose values can be fetched by an index.
func (*Heap) MarshalJSON ¶ added in v1.15.0
MarshalJSON @implements json.Marshaler
func (*Heap) 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) 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) Push ¶
func (heap *Heap) Push(values ...interface{})
Push adds a value onto the heap and bubbles it up accordingly.
func (*Heap) UnmarshalJSON ¶ added in v1.15.0
UnmarshalJSON @implements json.Unmarshaler
type Iterator ¶
type Iterator struct {
// contains filtered or unexported fields
}
Iterator returns a stateful iterator whose values can be fetched by an index.
func (*Iterator) Begin ¶ added in v1.2.0
func (iterator *Iterator) Begin()
Begin resets the iterator to its initial state (one-before-first) Call Next() to fetch the first element if any.
func (*Iterator) End ¶ added in v1.2.0
func (iterator *Iterator) End()
End moves the iterator past the last element (one-past-the-end). Call Prev() to fetch the last element if any.
func (*Iterator) First ¶ added in v1.2.0
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) Index ¶
Index returns the current element's index. Does not modify the state of the iterator.
func (*Iterator) Last ¶ added in v1.2.0
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) 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) NextTo ¶ added in v1.13.0
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) Prev ¶ added in v1.1.0
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) PrevTo ¶ added in v1.13.0
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.