fix(heap): add go codes
This commit is contained in:
parent
264a2ab6bc
commit
3dcdd1c72d
@ -4,6 +4,11 @@
|
|||||||
|
|
||||||
package chapter_heap
|
package chapter_heap
|
||||||
|
|
||||||
|
import (
|
||||||
|
"container/heap"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
// Go 语言中可以通过实现 heap.Interface 来构建整数大顶堆
|
// Go 语言中可以通过实现 heap.Interface 来构建整数大顶堆
|
||||||
// 实现 heap.Interface 需要同时实现 sort.Interface
|
// 实现 heap.Interface 需要同时实现 sort.Interface
|
||||||
type intHeap []any
|
type intHeap []any
|
||||||
@ -43,3 +48,38 @@ func (h *intHeap) Swap(i, j int) {
|
|||||||
func (h *intHeap) Top() any {
|
func (h *intHeap) Top() any {
|
||||||
return (*h)[0]
|
return (*h)[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Driver Code */
|
||||||
|
func main() {
|
||||||
|
/* 初始化堆 */
|
||||||
|
// 初始化大顶堆
|
||||||
|
maxHeap := &intHeap{}
|
||||||
|
heap.Init(maxHeap)
|
||||||
|
/* 元素入堆 */
|
||||||
|
// 调用 heap.Interface 的方法,来添加元素
|
||||||
|
heap.Push(maxHeap, 1)
|
||||||
|
heap.Push(maxHeap, 3)
|
||||||
|
heap.Push(maxHeap, 2)
|
||||||
|
heap.Push(maxHeap, 4)
|
||||||
|
heap.Push(maxHeap, 5)
|
||||||
|
|
||||||
|
/* 获取堆顶元素 */
|
||||||
|
top := maxHeap.Top()
|
||||||
|
fmt.Printf("堆顶元素为 %d\n", top)
|
||||||
|
|
||||||
|
/* 堆顶元素出堆 */
|
||||||
|
// 调用 heap.Interface 的方法,来移除元素
|
||||||
|
heap.Pop(maxHeap)
|
||||||
|
heap.Pop(maxHeap)
|
||||||
|
heap.Pop(maxHeap)
|
||||||
|
heap.Pop(maxHeap)
|
||||||
|
heap.Pop(maxHeap)
|
||||||
|
|
||||||
|
/* 获取堆大小 */
|
||||||
|
size := len(*maxHeap)
|
||||||
|
fmt.Printf("堆元素数量为 %d\n", size)
|
||||||
|
|
||||||
|
/* 判断堆是否为空 */
|
||||||
|
isEmpty := len(*maxHeap) == 0
|
||||||
|
fmt.Printf("堆是否为空 %t\n", isEmpty)
|
||||||
|
}
|
||||||
|
@ -136,6 +136,41 @@ comments: true
|
|||||||
func (h *intHeap) Top() any {
|
func (h *intHeap) Top() any {
|
||||||
return (*h)[0]
|
return (*h)[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Driver Code */
|
||||||
|
func TestHeap(t *testing.T) {
|
||||||
|
/* 初始化堆 */
|
||||||
|
// 初始化大顶堆
|
||||||
|
maxHeap := &intHeap{}
|
||||||
|
heap.Init(maxHeap)
|
||||||
|
/* 元素入堆 */
|
||||||
|
// 调用 heap.Interface 的方法,来添加元素
|
||||||
|
heap.Push(maxHeap, 1)
|
||||||
|
heap.Push(maxHeap, 3)
|
||||||
|
heap.Push(maxHeap, 2)
|
||||||
|
heap.Push(maxHeap, 4)
|
||||||
|
heap.Push(maxHeap, 5)
|
||||||
|
|
||||||
|
/* 获取堆顶元素 */
|
||||||
|
top := maxHeap.Top()
|
||||||
|
fmt.Printf("堆顶元素为 %d\n", top)
|
||||||
|
|
||||||
|
/* 堆顶元素出堆 */
|
||||||
|
// 调用 heap.Interface 的方法,来移除元素
|
||||||
|
heap.Pop(maxHeap)
|
||||||
|
heap.Pop(maxHeap)
|
||||||
|
heap.Pop(maxHeap)
|
||||||
|
heap.Pop(maxHeap)
|
||||||
|
heap.Pop(maxHeap)
|
||||||
|
|
||||||
|
/* 获取堆大小 */
|
||||||
|
size := len(*maxHeap)
|
||||||
|
fmt.Printf("堆元素数量为 %d\n", size)
|
||||||
|
|
||||||
|
/* 判断堆是否为空 */
|
||||||
|
isEmpty := len(*maxHeap) == 0
|
||||||
|
fmt.Printf("堆是否为空 %t\n", isEmpty)
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "JavaScript"
|
=== "JavaScript"
|
||||||
@ -317,7 +352,6 @@ comments: true
|
|||||||
func (h *maxHeap) peek() any {
|
func (h *maxHeap) peek() any {
|
||||||
return h.data[0]
|
return h.data[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "JavaScript"
|
=== "JavaScript"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user