目錄
container/heap是什麼
堆(英語:heap)是計算機科學中一類特殊的數據結構的統稱。堆通常是一個可以被看做一棵樹的數組對象。堆總是滿足下列性質:
-
堆中某個節點的值總是不大於或不小於其父節點的值;
-
堆總是一棵完全二叉樹。
將根節點最大的堆叫做最大堆或大根堆,根節點最小的堆叫做最小堆或小根堆。
- 堆的初始化。如何初始化,構建大根堆和小根堆
- 堆的插入元素和刪除元素
- 堆的排序
- 堆的向上調整函數和向下調整函數
上述4個問題搞明白之後再去看源碼,會更清楚實現。
container/heap提供的方法
container/heap
爲小根堆,即每個節點的值都小於它的子樹的所有元素的值。heap包爲實現了heap.Interface
的類型提供了堆方法:Init/Push/Pop/Remove/Fix。
由於heap.Interface
包含了sort.Interface
,所以,目標類型需要包含如下方法:Len/Less/Swap, Push/Pop。
type Interface interface {
sort.Interface
Push(x interface{}) // add x as element Len()
Pop() interface{} // remove and return element Len() - 1.
}
container/heap的源碼
見文章分析:https://studygolang.com/articles/13173
func Fix(h Interface, i int) // 修改第i個元素後,調用本函數修復堆 複雜度O(log(n)),其中n等於h.Len()。
func Init(h Interface) //初始化一個堆。一個堆在使用任何堆操作之前應先初始化。複雜度爲O(n)
func Pop(h Interface) interface{} //刪除並返回堆h中的最小元素(不影響約束性)。
func Push(h Interface, x interface{}) //向堆h中插入元素x,並保持堆的約束性。
func Remove(h Interface, i int) interface{} //刪除堆中的第i個元素,並保持堆的約束性。
container/heap用途
1. int slice類型的小根堆
// This example demonstrates an integer heap built using the heap interface.
package main
import (
"container/heap"
"fmt"
)
// An IntHeap is a min-heap of ints.
type IntHeap []int
func (h IntHeap) Len() int { return len(h) }
func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] } // 小根堆 > 大根堆
func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *IntHeap) Push(x interface{}) {
// Push and Pop use pointer receivers because they modify the slice's length,
// not just its contents.
*h = append(*h, x.(int))
}
func (h *IntHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}
// This example inserts several ints into an IntHeap, checks the minimum,
// and removes them in order of priority.
func main() {
h := &IntHeap{2, 1, 5}
heap.Init(h)
heap.Push(h, 3)
fmt.Printf("minimum: %d\n", (*h)[0])
for h.Len() > 0 {
fmt.Printf("%d ", heap.Pop(h))
}
}
2. 實現優先級隊列(重要:k8s優先級隊列)
// This example demonstrates a priority queue built using the heap interface.
package main
import (
"container/heap"
"fmt"
)
// An Item is something we manage in a priority queue.
type Item struct {
value string // The value of the item; arbitrary.
priority int // The priority of the item in the queue.
// The index is needed by update and is maintained by the heap.Interface methods.
index int // The index of the item in the heap.
}
// A PriorityQueue implements heap.Interface and holds Items.
type PriorityQueue []*Item
func (pq PriorityQueue) Len() int { return len(pq) }
func (pq PriorityQueue) Less(i, j int) bool {
// We want Pop to give us the highest, not lowest, priority so we use greater than here.
return pq[i].priority > pq[j].priority
}
func (pq PriorityQueue) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
pq[i].index = i
pq[j].index = j
}
func (pq *PriorityQueue) Push(x interface{}) {
n := len(*pq)
item := x.(*Item)
item.index = n
*pq = append(*pq, item)
}
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 string, priority int) {
item.value = value
item.priority = priority
heap.Fix(pq, item.index)
}
// This example creates a PriorityQueue with some items, adds and manipulates an item,
// and then removes the items in priority order.
func main() {
// Some items and their priorities.
items := map[string]int{
"banana": 3, "apple": 2, "pear": 4,
}
// Create a priority queue, put the items in it, and
// establish the priority queue (heap) invariants.
pq := make(PriorityQueue, len(items))
i := 0
for value, priority := range items {
pq[i] = &Item{
value: value,
priority: priority,
index: i,
}
i++
}
heap.Init(&pq)
// Insert a new item and then modify its priority.
item := &Item{
value: "orange",
priority: 1,
}
heap.Push(&pq, item)
pq.update(item, item.value, 5)
// Take the items out; they arrive in decreasing priority order.
for pq.Len() > 0 {
item := heap.Pop(&pq).(*Item)
fmt.Printf("%.2d:%s ", item.priority, item.value)
}
}
3. 處理最小的K個數或者最大的K個數,處理海量數據
- 讀入k個數構建大小爲k的大根堆
- 依次讀入剩餘數據,當前數據比大根堆的堆頂數據小,替換並調整滿足大根堆特性
- 當前數據如果比堆頂數據大,拋棄此數
主要是能夠分析堆的初始化、排序、調整、及對堆的應用場景進行掌握。