【Golang】 堆heap的使用

本文详细介绍了Go语言标准库container中heap的使用,包括heap接口的定义、所需实现的方法,以及如何自定义数据结构以适配heap。通过示例展示了如何初始化heap、插入元素、删除元素等操作,帮助理解Go语言中的优先级队列实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

重点介绍

  • 在标准库 container中,含有三种数据类型,分别是heap,list,ring;
  • Go语言提供heap容器,但是不提供相关操作,故需要我们进行相关函数的重写;

  • 导包
package main

import (
	"container/heap"
	"fmt"
)
  • heap中只实现了一个接口
type Interface interface {
	sort.Interface
	Push(x interface{})
	Pop() interface{}
}
  • 这个唯一接口中,还继承了 sort的接口,如下
type Interface interface {
	Len() int
	Less(i, j int) bool
	Swap(i, j int)
}
  • 故要用标准库的heap,需要实现下列五个接口函数,如下
{
	Len() int
	Less(i, j int) bool
	Swap(i, j int)
	Push(x interface{})
	Pop() interface{}
}
  • 在heap中,提供了一些方法,如下
{
	h := &IntHeap{}  //创建IntHeap类型的原始数据
	func Init(h Interface)  //对heap进行初始化,生成小根堆(或大根堆)
	func Push(h Interface, x interface{})  //往堆插入内容
	func Pop(h Interface) interface{}  //从堆顶中取出元素
	func Remove(h Interface, i int) interface{}  //从指定位置删除数据,并返回删除的数据
	func Fix(h Interface, i int)  //从i位置发生改变后,对堆再平衡,优先级队列使用到了该方法
}


实践

package main

import (
	"fmt"
	"container/heap"
)
func main() {
	h := &IntHeap{2, 1, 5, 6, 4, 3, 7, 9, 8, 0}  //创建slice
	heap.Init(h)  //初始化heap

	fmt.Println(*h)
	//[0 1 3 6 2 5 7 9 8 4]
	fmt.Println(heap.Pop(h))
	//0
	
	heap.Push(h, 6)

	fmt.Println(*h)
	//[1 2 3 6 4 5 7 9 8 6]
	for len(*h) > 0 {
		fmt.Printf("%d ", heap.Pop(h))
	}
	//1 2 3 4 5 6 6 7 8 9
}


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{}) {
	*h = append(*h, x.(int))
}

func (h *IntHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n - 1]  //注意这里,虽然取得是索引n - 1位置的元素,实际上对应的是h[0],即堆顶元素
	*h = old[0 : n - 1]
	return x
}
好的,我可以为您解答有关golang实现排序的问题。在golang中,可以使用container/heap包来实现排序。具体实现步骤如下: 1. 定义一个类型用于表示需要排序的元素。这个类型需要实现container/heap包中的heap.Interface接口。 2. 通过实现heap.Interface接口中的Less方法来定义元素之间的比较方式。 3. 定义一个结构体,它拥有一组需要排序的元素。 4. 在结构体上定义Len、Less和Swap方法,这些方法实现heap.Interface接口中的方法。 5. 实现Push和Pop方法,它们是heap.Interface接口中的方法。 6. 使用结构体的Push和Pop方法来进行排序。 这是一个简单的golang实现排序的示例代码: package main import ( "container/heap" "fmt" ) 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{}) { *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 } func main() { h := &IntHeap{2, 1, 5, 4, 3} heap.Init(h) fmt.Printf("Before heap sort: %v\n", h) heap.Sort(h) fmt.Printf("After heap sort: %v\n", h) } 在这个示例代码中,我们定义了一个IntHeap类型来表示需要进行排序的元素。它实现了heap.Interface接口中的Less方法。我们还定义了一个结构体,它拥有一个IntHeap类型的变量。在Push和Pop方法中,我们分别使用append和切片操作来实现的操作。在main函数中,我们创建了一个IntHeap类型的变量h,并使用heap.Init方法来初始化它。然后,我们使用heap.Sort方法来对其进行排序。最终的输出结果是:Before heap sort: &[2 1 5 4 3] After heap sort: &[1 2 3 4 5]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值