mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-10-31 18:37:48 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			46 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			46 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // File: heap.go
 | |
| // Created Time: 2023-01-12
 | |
| // Author: Reanon (793584285@qq.com)
 | |
| 
 | |
| package chapter_heap
 | |
| 
 | |
| // Go 语言中可以通过实现 heap.Interface 来构建整数大顶堆
 | |
| // 实现 heap.Interface 需要同时实现 sort.Interface
 | |
| type intHeap []any
 | |
| 
 | |
| // Push heap.Interface 的函数,实现推入元素到堆
 | |
| func (h *intHeap) Push(x any) {
 | |
| 	// Push 和 Pop 使用 pointer receiver 作为参数
 | |
| 	// 因为它们不仅会对切片的内容进行调整,还会修改切片的长度。
 | |
| 	*h = append(*h, x.(int))
 | |
| }
 | |
| 
 | |
| // Pop heap.Interface 的函数,实现弹出堆顶元素
 | |
| func (h *intHeap) Pop() any {
 | |
| 	// 待出堆元素存放在最后
 | |
| 	last := (*h)[len(*h)-1]
 | |
| 	*h = (*h)[:len(*h)-1]
 | |
| 	return last
 | |
| }
 | |
| 
 | |
| // Len sort.Interface 的函数
 | |
| func (h *intHeap) Len() int {
 | |
| 	return len(*h)
 | |
| }
 | |
| 
 | |
| // Less sort.Interface 的函数
 | |
| func (h *intHeap) Less(i, j int) bool {
 | |
| 	// 如果实现小顶堆,则需要调整为小于号
 | |
| 	return (*h)[i].(int) > (*h)[j].(int)
 | |
| }
 | |
| 
 | |
| // Swap sort.Interface 的函数
 | |
| func (h *intHeap) Swap(i, j int) {
 | |
| 	(*h)[i], (*h)[j] = (*h)[j], (*h)[i]
 | |
| }
 | |
| 
 | |
| // Top 获取堆顶元素
 | |
| func (h *intHeap) Top() any {
 | |
| 	return (*h)[0]
 | |
| }
 | 
