1 Star 0 Fork 0

Paulden/Algorithm

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
min_heap.go 1.22 KB
一键复制 编辑 原始数据 按行查看 历史
Paulden 提交于 2022-08-21 18:36 . implement min heap
package heap
import "gitee.com/guuzaa/algorithm/constraints"
type MinHeap[T constraints.Ordered] struct {
nums []T
length int
}
func NewMinHeap[T constraints.Ordered]() MinHeap[T] {
return MinHeap[T]{make([]T, 0), 0}
}
func (h *MinHeap[T]) Push(val T) {
h.nums = append(h.nums, val)
h.length++
lastParent := parent(h.length)
for i := lastParent; i >= 0; i-- {
moveDown(h.nums, i)
}
}
func (h *MinHeap[T]) Pop() T {
val := h.nums[0]
h.length--
h.nums[0] = h.nums[h.length]
h.nums = h.nums[:h.length]
moveDown(h.nums, 0)
return val
}
func (h *MinHeap[T]) Len() int {
return h.length
}
func (h *MinHeap[T]) IsEmpty() bool {
return h.length == 0
}
func moveDown[T constraints.Ordered](nums []T, parent int) {
last := len(nums) - 1
for {
left, right := leftChild(parent), rightChild(parent)
if left > last {
break
}
child := left
if right <= last && nums[right] < nums[child] {
child = right
}
// ! Note
if nums[parent] > nums[child] {
nums[parent], nums[child] = nums[child], nums[parent]
}
parent = child
}
}
func parent(child int) int {
return child / 2
}
func leftChild(parent int) int {
return (parent * 2) + 1
}
func rightChild(parent int) int {
return (parent + 1) * 2
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/guuzaa/algorithm.git
git@gitee.com:guuzaa/algorithm.git
guuzaa
algorithm
Algorithm
main

搜索帮助