首先是最小堆算法的golang實現(xiàn):
package main
// MinHeap 最小堆的結(jié)構(gòu)
type MinHeap struct {
heapSize int
heap []int
}
// LEFT 返回子樹左邊的元素
func (A *MinHeap) LEFT(i int) int {
return i << 1
}
// RIGHT 返回子樹右邊的元素
func (A *MinHeap) RIGHT(i int) int {
return (i<<1 + 1)
}
// PARENT 返回父結(jié)點
func (A *MinHeap) PARENT(i int) int {
return i >> 1
}
// MinHeapify 最小化堆
func (A *MinHeap) MinHeapify(i int) {
smallest := i
l := A.LEFT(i + 1)
r := A.RIGHT(i + 1)
if l <= A.heapSize && A.heap[l-1] < A.heap[i] {
smallest = l - 1
}
if r <= A.heapSize && A.heap[r-1] < A.heap[smallest] {
smallest = r - 1
}
if smallest != i {
A.heap[smallest], A.heap[i] = A.heap[i], A.heap[smallest]
A.MinHeapify(smallest)
}
}
// BuildMinHeap 構(gòu)建最小堆
func (A *MinHeap) BuildMinHeap() {
for i := A.heapSize / 2; i >= 0; i-- {
A.MinHeapify(i)
}
}
// GetHeapSize 獲得堆大小
func (A *MinHeap) GetHeapSize() int {
return A.heapSize
}
// AlterHeapSize 更改堆大小
func (A *MinHeap) AlterHeapSize(i int) {
A.heapSize = i
}
// GetElement 獲得堆中的元素
func (A *MinHeap) GetElement(i int) int {
return A.heap[i]
}
// SetElement 更新堆中的元素
func (A *MinHeap) SetElement(i int, key int) {
A.heap[i] = key
}
// Swap 交換堆中的元素
func (A *MinHeap) Swap(i int, j int) {
A.heap[i], A.heap[j] = A.heap[j], A.heap[i]
}
// Append 向堆中追加元素
func (A *MinHeap) Append(i int) {
A.heap = append(A.heap, i)
}
// NewMinHeap 最小里堆的構(gòu)造函數(shù)
func NewMinHeap(heapSize int, a []int) *MinHeap {
minHeap := MinHeap{heapSize: heapSize, heap: a}
minHeap.BuildMinHeap()
return &minHeap
}
然后是基于最小堆的最小隊列的golang實現(xiàn):
package main
import (
"errors"
"fmt"
)
// MinHeapInterface 最小堆的接口
type MinHeapInterface interface {
LEFT(i int) int
RIGHT(i int) int
PARENT(i int) int
MinHeapify(i int)
BuildMinHeap()
GetHeapSize() int
AlterHeapSize(int)
GetElement(i int) int
Swap(i int, j int)
SetElement(i int, key int)
Append(i int)
}
// MinPriorityQueue 最小隊列的接口(只要實現(xiàn)了最小隊列的那些方法就能實現(xiàn)最小隊列)
type MinPriorityQueue interface {
MinHeapInterface
}
// Insert 把元素key插入到隊列S中
func Insert(S MinPriorityQueue, key int) {
const MaxInt = int(^uint(0) >> 1)
S.AlterHeapSize(S.GetHeapSize() + 1)
S.Append(MaxInt)
DecreaseKey(S, S.GetHeapSize()-1, key)
}
// Minimum 返回S中具有最小關(guān)鍵字的元素
func Minimum(S MinPriorityQueue) int {
return S.GetElement(0)
}
// ExtractMin 去掉并返回S中具有最小關(guān)鍵字的元素
func ExtractMin(S MinHeapInterface) (int, error) {
heapSize := S.GetHeapSize()
if heapSize < 1 {
return 0, errors.New("HEAP UNDERFLOW")
}
min := S.GetElement(0)
S.Swap(0, heapSize-1)
S.AlterHeapSize((heapSize - 1))
S.MinHeapify(0)
return min, nil
}
// DecreaseKey 將元素i的關(guān)鍵字減少到key,這里假設(shè)key的值不小于i的原關(guān)鍵字值
func DecreaseKey(S MinHeapInterface, i int, key int) error {
if key >= S.GetElement(i) {
return errors.New("new key is bigger than current key")
}
S.SetElement(i, key)
for i > 0 && S.GetElement(S.PARENT(i)) > S.GetElement(i) {
S.Swap(i, S.PARENT(i))
i = S.PARENT(i)
}
return nil
}
func main() {
s := []int{15, 13, 9, 5, 12, 8, 7, 4, 0, 6, 2, 1}
a := NewMinHeap(len(s), s)
for a.heapSize > 0 {
i, _ := ExtractMin(a)
fmt.Println(i)
}
}