package main
import "fmt"
type MinHeap struct {
arr []int
}
func NewMinHeap() *MinHeap {
return &MinHeap{arr: make([]int, 0)}
}
func (m *MinHeap) InsertVal(val int) {
m.arr = append(m.arr, val)
index := len(m.arr) - 1
parentIndex := (index - 1) / 2
for index > 0 && m.arr[parentIndex] > m.arr[index] {
m.arr[parentIndex], m.arr[index] = m.arr[index], m.arr[parentIndex]
index = parentIndex
parentIndex = (index - 1) / 2
}
}
func (m *MinHeap) Heapify(index int) {
leftChildIndex := 2*index + 1
rightChildIndex := 2*index + 2
smallestIndex := index
if leftChildIndex < len(m.arr) && m.arr[leftChildIndex] < m.arr[smallestIndex] {
smallestIndex = leftChildIndex
}
if rightChildIndex < len(m.arr) && m.arr[rightChildIndex] < m.arr[smallestIndex] {
smallestIndex = rightChildIndex
}
if smallestIndex != index {
m.arr[index], m.arr[smallestIndex] = m.arr[smallestIndex], m.arr[index]
m.Heapify(smallestIndex)
}
}
func (m *MinHeap) ExtractMin() int {
if len(m.arr) == 0 {
return -1
}
min := m.arr[0]
m.arr[0] = m.arr[len(m.arr)-1]
m.arr = m.arr[:len(m.arr)-1]
m.Heapify(0)
return min
}
func main() {
minHeap := NewMinHeap()
minHeap.InsertVal(5)
minHeap.InsertVal(3)
minHeap.InsertVal(17)
minHeap.InsertVal(10)
minHeap.InsertVal(84)
minHeap.InsertVal(19)
minHeap.InsertVal(6)
minHeap.InsertVal(22)
minHeap.InsertVal(9)
fmt.Println("Extracted minimum: ", minHeap.ExtractMin())
}
package main
import (
"container/heap"
"fmt"
)
// Item represents an item in the priority queue.
type Item struct {
value interface{} // The value of the item; arbitrary.
priority int // The priority of the item in the queue.
index int // The index of the item in the heap.
}
// PriorityQueue implements a priority queue data structure.
type PriorityQueue []*Item
// Len returns the length of the priority queue.
func (pq PriorityQueue) Len() int {
return len(pq)
}
// Less returns true if the item with index i has higher priority than the item with index j.
func (pq PriorityQueue) Less(i, j int) bool {
return pq[i].priority < pq[j].priority
}
// Swap swaps the items with indexes i and j in the priority queue.
func (pq PriorityQueue) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
pq[i].index = i
pq[j].index = j
}
// Push adds an item to the priority queue.
func (pq *PriorityQueue) Push(x interface{}) {
n := len(*pq)
item := x.(*Item)
item.index = n
*pq = append(*pq, item)
}
// Pop removes the highest priority item from the priority queue and returns it.
func (pq *PriorityQueue) Pop() interface{} {
old := *pq
n := len(old)
item := old[n-1]
item.index = -1 // for safety
*pq = old[0 : n-1]
return item
}
// Update modifies the priority and value of an Item in the queue.
func (pq *PriorityQueue) Update(item *Item, value interface{}, priority int) {
item.value = value
item.priority = priority
heap.Fix(pq, item.index)
}
func main() {
pq := make(PriorityQueue, 0)
heap.Init(&pq)
heap.Push(&pq, &Item{value: "A", priority: 5})
heap.Push(&pq, &Item{value: "B", priority: 3})
heap.Push(&pq, &Item{value: "C", priority: 10})
heap.Push(&pq, &Item{value: "D", priority: 1})
for pq.Len() > 0 {
item := heap.Pop(&pq).(*Item)
fmt.Printf("Value: %s, Priority: %d\n", item.value, item.priority)
}
}