fix: polling from an empty heap will cause panic

This commit is contained in:
Gaosong 2023-01-15 11:50:04 +08:00
parent 33e84ff50b
commit 811f15cf3c
2 changed files with 2 additions and 0 deletions

View File

@ -98,6 +98,7 @@ func (h *maxHeap) poll() any {
// 判空处理
if h.isEmpty() {
fmt.Println("error")
return nil
}
// 交换根结点与最右叶结点(即交换首元素与尾元素)
h.swap(0, h.size()-1)

View File

@ -606,6 +606,7 @@ comments: true
// 判空处理
if h.isEmpty() {
fmt.Println("error")
return nil
}
// 交换根结点与最右叶结点(即交换首元素与尾元素)
h.swap(0, h.size()-1)