go---详解container heap


目录

container/heap是什么

container/heap提供的方法

container/heap的源码

container/heap用途

1. int slice类型的小根堆

2. 实现优先级队列(重要:k8s优先级队列)

3. 处理最小的K个数或者最大的K个数,处理海量数据


container/heap是什么

堆(英语:heap)是计算机科学中一类特殊的数据结构的统称。堆通常是一个可以被看做一棵树的数组对象。堆总是满足下列性质:

  • 堆中某个节点的值总是不大于或不小于其父节点的值;

  • 堆总是一棵完全二叉树。

将根节点最大的堆叫做最大堆或大根堆,根节点最小的堆叫做最小堆或小根堆。

  1. 堆的初始化。如何初始化,构建大根堆和小根堆
  2. 堆的插入元素和删除元素
  3. 堆的排序
  4. 堆的向上调整函数和向下调整函数

上述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的大根堆
  • 依次读入剩余数据,当前数据比大根堆的堆顶数据小,替换并调整满足大根堆特性
  • 当前数据如果比堆顶数据大,抛弃此数

主要是能够分析堆的初始化、排序、调整、及对堆的应用场景进行掌握。


原文链接:https://blog.csdn.net/li_101357/article/details/90111230