
离线 TA的专栏
- 打卡等级:热心大叔
- 打卡总天数:232
- 打卡月天数:0
- 打卡总奖励:4143
- 最近打卡:2025-04-20 08:44:27
|
包 heap 为所有实现了 heap.Interface 的类型提供堆操作。 一个堆即是一棵树, 这棵树的每个节点的值都比它的子节点的值要小, 而整棵树最小的值位于树根(root), 也即是索引 0 的位置上。
包 heap 采样接口的形式来满足不同的类型的比较。 - type Interface interface {
- sort.Interface
- Push(x any) // add x as element Len()
- Pop() any // remove and return element Len() - 1.
- }
- // sort.Interface
- type Interface interface {
- Len() int
- Less(i, j int) bool
- Swap(i, j int)
- }
复制代码因此,你要比较的对象要先实现
简单排序
- type myHeap []int
- func (h *myHeap) Less(i, j int) bool {
- return (*h)[i] < (*h)[j]
- }
- func (h *myHeap) Swap(i, j int) {
- (*h)[i], (*h)[j] = (*h)[j], (*h)[i]
- }
- func (h *myHeap) Len() int {
- return len(*h)
- }
- // 把最后一个弹出,因为最小的值已经被换到了最后
- func (h *myHeap) Pop() (v any) {
- *h, v = (*h)[:h.Len()-1], (*h)[h.Len()-1]
- return
- }
- func (h *myHeap) Push(v any) {
- *h = append(*h, v.(int))
- }
- // 从小到大排序
- func HeapSort(data []int) []int {
- hp := &myHeap{}
- for _, v := range data {
- heap.Push(hp, v)
- }
- heap.Init(hp)
- res := make([]int, hp.Len())
- for i := 0; hp.Len() > 0; i++ {
- res[i] = heap.Pop(hp).(int)
- }
- return res
- }
- // data = 6, 0, 1, 7, 9, 4, 3, 8, 2, 5
- // hp 中存储的是 0, 2, 1, 6, 5, 4, 3, 8, 7, 9
复制代码 优先队列
使用一个 priority 字段来表示优先级大小,使用 index 字段来表示索引。 - type Item struct {
- value string
- priority int
- index int
- }
- type PriorityQueue []*Item
- func (pq PriorityQueue) Len() int { return len(pq) }
- func (pq PriorityQueue) Less(i, j int) bool {
- 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 any) {
- n := len(*pq)
- item := x.(*Item)
- item.index = n
- *pq = append(*pq, item)
- }
- func (pq *PriorityQueue) Pop() any {
- old := *pq
- n := len(old)
- item := old[n-1]
- old[n-1] = nil // avoid memory leak
- item.index = -1 // for safety
- *pq = old[0 : n-1]
- return item
- }
- func (pq *PriorityQueue) update(item *Item, value string, priority int) {
- item.value = value
- item.priority = priority
- heap.Fix(pq, item.index)
- }
- func PriorityQueueTest() {
- items := map[string]int{
- "banana": 3, "apple": 2, "pear": 4,
- }
- 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)
- item := &Item{
- value: "orange",
- priority: 1,
- }
- heap.Push(&pq, item)
- pq.update(item, item.value, 5)
- for pq.Len() > 0 {
- item := heap.Pop(&pq).(*Item)
- fmt.Printf("%.2d:%s ", item.priority, item.value)
- }
- }
复制代码 按时间排序
- type TimeSortedQueueItem struct {
- Time int64
- Value interface{}
- }
- type TimeSortedQueue []*TimeSortedQueueItem
- func (q TimeSortedQueue) Len() int { return len(q) }
- func (q TimeSortedQueue) Less(i, j int) bool { return q[i].Time < q[j].Time }
- func (q TimeSortedQueue) Swap(i, j int) { q[i], q[j] = q[j], q[i] }
- func (q *TimeSortedQueue) Push(v interface{}) {
- *q = append(*q, v.(*TimeSortedQueueItem))
- }
- func (q *TimeSortedQueue) Pop() interface{} {
- n := len(*q)
- item := (*q)[n-1]
- *q = (*q)[0 : n-1]
- return item
- }
- func NewTimeSortedQueue(items ...*TimeSortedQueueItem) *TimeSortedQueue {
- q := make(TimeSortedQueue, len(items))
- for i, item := range items {
- q[i] = item
- }
- heap.Init(&q)
- return &q
- }
- func (q *TimeSortedQueue) PushItem(time int64, value interface{}) {
- heap.Push(q, &TimeSortedQueueItem{
- Time: time,
- Value: value,
- })
- }
- func (q *TimeSortedQueue) PopItem() interface{} {
- if q.Len() == 0 {
- return nil
- }
- return heap.Pop(q).(*TimeSortedQueueItem).Value
- }
复制代码到此这篇关于golang中的container/heap包使用的文章就介绍到这了,更多相关golang container/heap包内容请搜索晓枫资讯以前的文章或继续浏览下面的相关文章希望大家以后多多支持晓枫资讯!
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
晓枫资讯-科技资讯社区-免责声明
免责声明:以上内容为本网站转自其它媒体,相关信息仅为传递更多信息之目的,不代表本网观点,亦不代表本网站赞同其观点或证实其内容的真实性。
1、注册用户在本社区发表、转载的任何作品仅代表其个人观点,不代表本社区认同其观点。
2、管理员及版主有权在不事先通知或不经作者准许的情况下删除其在本社区所发表的文章。
3、本社区的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,举报反馈:  进行删除处理。
4、本社区一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
5、以上声明内容的最终解释权归《晓枫资讯-科技资讯社区》所有。
|