fix: polling from an empty heap will cause panic
This commit is contained in:
parent
33e84ff50b
commit
811f15cf3c
|
|
@ -98,6 +98,7 @@ func (h *maxHeap) poll() any {
|
||||||
// 判空处理
|
// 判空处理
|
||||||
if h.isEmpty() {
|
if h.isEmpty() {
|
||||||
fmt.Println("error")
|
fmt.Println("error")
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
// 交换根结点与最右叶结点(即交换首元素与尾元素)
|
// 交换根结点与最右叶结点(即交换首元素与尾元素)
|
||||||
h.swap(0, h.size()-1)
|
h.swap(0, h.size()-1)
|
||||||
|
|
|
||||||
|
|
@ -606,6 +606,7 @@ comments: true
|
||||||
// 判空处理
|
// 判空处理
|
||||||
if h.isEmpty() {
|
if h.isEmpty() {
|
||||||
fmt.Println("error")
|
fmt.Println("error")
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
// 交换根结点与最右叶结点(即交换首元素与尾元素)
|
// 交换根结点与最右叶结点(即交换首元素与尾元素)
|
||||||
h.swap(0, h.size()-1)
|
h.swap(0, h.size()-1)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue