Skip to content

fixed heap: index of parent bug #468

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions data-structure/heap/heap.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ func (h *Heap) Peek() (e Element) {

func (h *Heap) precolateUp(index int) {
// 上滤,新元素在堆中上滤直到找出正确位置
needUp, parent := index, index>>1
needUp, parent := index, (index-1)>>1
for needUp > 0 && h.less(h.heap[needUp], h.heap[parent]) {
h.heap[parent], h.heap[needUp] = h.heap[needUp], h.heap[parent]
needUp, parent = parent, parent>>1
needUp, parent = parent, (parent-1)>>1
}
}

Expand Down
45 changes: 44 additions & 1 deletion data-structure/heap/heap_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package heap

import (
"github.com/stretchr/testify/assert"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

type Int int
Expand Down Expand Up @@ -39,6 +41,28 @@ func Test_MinHeap(t *testing.T) {
}
}

// leetcode s215 bug
func Test_MinHeap_2(t *testing.T) {
assert := assert.New(t)

h := New(true)
for _, num := range []Int{-7576, 4209, 9414, 5451, -5927, 5860, -1170, -8248, 4803, 9364, -2880, -3983, 3463,
-555, 7491, 6544, 1671, 4898, -4888, -5685, 1488, 1072, -3841, 7299, -991, -5525, -197,
-2082, -7256, 1134, -9602, 9398, 2097, 9831, -1377, 8362, -1476, -5141, 9906, -7646,
} {
h.Insert(num)
}

res := make([]int, 0, h.Len())
for h.Len() > 0 {
res = append(res, int(h.Extract().(Int)))
}

for i := 0; i < len(res)-1; i++ {
assert.True(res[i] <= res[i+1], fmt.Sprintf("error at i={%d}", i))
}
}

func Test_MaxHeap(t *testing.T) {
assert := assert.New(t)

Expand Down Expand Up @@ -102,3 +126,22 @@ func Test_precolate(t *testing.T) {
assert.True(res[i] < res[i+1])
}
}

func Test_cal_parent_index(t *testing.T) {
assert := assert.New(t)

for index := 0; index < 10; index++ {
if index%2 == 0 {
assert.NotEqual((index-1)>>1, index>>1, fmt.Sprintf("index={%d}", index))
} else {
assert.Equal((index-1)>>1, index>>1, fmt.Sprintf("index={%d}", index))
}
}
}

func Test_cal_child_index(t *testing.T) {
assert := assert.New(t)
for index := 0; index < 100; index++ {
assert.Equal(index*2+1, index<<1+1, fmt.Sprintf("index={%d}", index))
}
}
4 changes: 2 additions & 2 deletions leetcode/heap/findKthLargest.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ You may assume k is always valid, 1 ≤ k ≤ array's length.

package lheap

/*
// 快速选择算法
func findKthLargest(nums []int, k int) int {
if len(nums) == 1 {
Expand All @@ -32,8 +33,8 @@ func findKthLargest(nums []int, k int) int {
}
return findKthLargest(nums[idx+1:], k-idx-1)
}
*/

/*
// 基于堆的实现
import "github.com/TTWShell/algorithms/data-structure/heap"

Expand All @@ -59,4 +60,3 @@ func findKthLargest(nums []int, k int) int {
}
return int(heap.Peek().(findKthLargestInt))
}
*/
Loading
Loading