Skip to content

Commit 0742487

Browse files
authored
Merge pull request #468 from TTWShell/bugfix/s215
fixed heap: index of parent bug
2 parents 51cf0e6 + 051326f commit 0742487

File tree

4 files changed

+1631
-6
lines changed

4 files changed

+1631
-6
lines changed

data-structure/heap/heap.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ func (h *Heap) Peek() (e Element) {
6767

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

data-structure/heap/heap_test.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package heap
22

33
import (
4-
"github.com/stretchr/testify/assert"
4+
"fmt"
55
"testing"
6+
7+
"github.com/stretchr/testify/assert"
68
)
79

810
type Int int
@@ -39,6 +41,28 @@ func Test_MinHeap(t *testing.T) {
3941
}
4042
}
4143

44+
// leetcode s215 bug
45+
func Test_MinHeap_2(t *testing.T) {
46+
assert := assert.New(t)
47+
48+
h := New(true)
49+
for _, num := range []Int{-7576, 4209, 9414, 5451, -5927, 5860, -1170, -8248, 4803, 9364, -2880, -3983, 3463,
50+
-555, 7491, 6544, 1671, 4898, -4888, -5685, 1488, 1072, -3841, 7299, -991, -5525, -197,
51+
-2082, -7256, 1134, -9602, 9398, 2097, 9831, -1377, 8362, -1476, -5141, 9906, -7646,
52+
} {
53+
h.Insert(num)
54+
}
55+
56+
res := make([]int, 0, h.Len())
57+
for h.Len() > 0 {
58+
res = append(res, int(h.Extract().(Int)))
59+
}
60+
61+
for i := 0; i < len(res)-1; i++ {
62+
assert.True(res[i] <= res[i+1], fmt.Sprintf("error at i={%d}", i))
63+
}
64+
}
65+
4266
func Test_MaxHeap(t *testing.T) {
4367
assert := assert.New(t)
4468

@@ -102,3 +126,22 @@ func Test_precolate(t *testing.T) {
102126
assert.True(res[i] < res[i+1])
103127
}
104128
}
129+
130+
func Test_cal_parent_index(t *testing.T) {
131+
assert := assert.New(t)
132+
133+
for index := 0; index < 10; index++ {
134+
if index%2 == 0 {
135+
assert.NotEqual((index-1)>>1, index>>1, fmt.Sprintf("index={%d}", index))
136+
} else {
137+
assert.Equal((index-1)>>1, index>>1, fmt.Sprintf("index={%d}", index))
138+
}
139+
}
140+
}
141+
142+
func Test_cal_child_index(t *testing.T) {
143+
assert := assert.New(t)
144+
for index := 0; index < 100; index++ {
145+
assert.Equal(index*2+1, index<<1+1, fmt.Sprintf("index={%d}", index))
146+
}
147+
}

leetcode/heap/findKthLargest.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ You may assume k is always valid, 1 ≤ k ≤ array's length.
99

1010
package lheap
1111

12+
/*
1213
// 快速选择算法
1314
func findKthLargest(nums []int, k int) int {
1415
if len(nums) == 1 {
@@ -32,8 +33,8 @@ func findKthLargest(nums []int, k int) int {
3233
}
3334
return findKthLargest(nums[idx+1:], k-idx-1)
3435
}
36+
*/
3537

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

@@ -59,4 +60,3 @@ func findKthLargest(nums []int, k int) int {
5960
}
6061
return int(heap.Peek().(findKthLargestInt))
6162
}
62-
*/

0 commit comments

Comments
 (0)