Skip to content

proposal: container: Generic Binary Heap #73346

Closed as duplicate of#47632
Closed as duplicate of#47632
@chlovec

Description

@chlovec

Proposal Details

I already have submitted a proposal for an array heap in #73345. However, there are situations such as in data stream and large data processing where array heaps will not work.

I am proposing a Binary Heap for handling of large data sets.

The APIs are:

type heapNode[T any] struct {
value T
height int
left *heapNode[T]
right *heapNode[T]
}

// Constructor function for the heapNode
func newHeapNode[T any](value T) *heapNode[T] {
return &heapNode[T]{value: value, size: 1, height: 1}
}

type BHeap[T any] struct {
lessFunc func(a, b T) bool
root *heapNode[T]
}

// Constructor function for the binary heap
func NewBHeap[T any](lessFunc func(a, b T) bool) *BHeap[T]

// Returns the value of the topmost node of the heap along with true
// This value is equivalent to the minimum or maximum value of all the values in the heap, depending on the less function
// Will return zeroValue of T and false if heap is empty
// Time complexity is O(1)
func (bh *BHeap[T]) Top() (T, bool)

// Removes the topmost node from the heap and returns the value along with true
// Will return the zeroValue of T along with false if the heap is empty
func (bh *BHeap[T]) Pop() (T, bool)

// Pushes T onto the heap
// Time complexity is O(logn)
func (bh *BHeap[T]) Push(value T)

Metadata

Metadata

Assignees

No one assigned

    Labels

    LibraryProposalIssues describing a requested change to the Go standard library or x/ libraries, but not to a toolProposal

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions