Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions go/28_heap/heap.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,27 @@ func (heap *Heap) removeMax() {
heapifyUpToDown(heap.a, heap.count)
}

//get max
func (heap *Heap) getMax() int {
if heap.count==0 {
return -1
}
return heap.a[1]
}

//get and remove
func (heap *Heap) getAndRemoveMax() int {
var max int
if heap.count == 0 {
return -1
}
max=heap.a[1]
swap(heap.a, 1, heap.count)
heap.count--
heapifyUpToDown(heap.a, heap.count)
return max
}

//heapify
func heapifyUpToDown(a []int, count int) {

Expand Down