Skip to content

proposal: container: Generic Binary Heap #73346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
chlovec opened this issue Apr 12, 2025 · 2 comments
Closed

proposal: container: Generic Binary Heap #73346

chlovec opened this issue Apr 12, 2025 · 2 comments
Labels
LibraryProposal Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool Proposal
Milestone

Comments

@chlovec
Copy link

chlovec commented Apr 12, 2025

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)

@gopherbot gopherbot added this to the Proposal milestone Apr 12, 2025
@gabyhelp
Copy link

@gabyhelp gabyhelp added the LibraryProposal Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool label Apr 12, 2025
@seankhliao
Copy link
Member

let's settle on a standard set of apis first in #47632 before thinking about other implementations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LibraryProposal Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool Proposal
Projects
None yet
Development

No branches or pull requests

4 participants