Merge pull request #258 from ShiMaRing/master

fix: polling from an empty heap will cause panic
This commit is contained in:
Yudong Jin 2023-01-15 18:20:21 +08:00 committed by GitHub
commit 736e368d8b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 2 additions and 0 deletions

View file

@ -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)

View file

@ -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)