1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package heap provides heap operations for any type that implements
// heap.Interface. A heap is a tree with the property that each node is the
// minimum-valued node in its subtree.
//
// The minimum element in the tree is the root, at index 0.
//
// A heap is a common way to implement a priority queue. To build a priority
// queue, implement the Heap interface with the (negative) priority as the
// ordering for the Less method, so Push adds items while Pop removes the
// highest-priority item from the queue. The Examples include such an
// implementation; the file example_pq_test.go has the complete source.
//
package heap
import "sort"
// The Interface type describes the requirements
// for a type using the routines in this package.
// Any type that implements it may be used as a
// min-heap with the following invariants (established after
// Init has been called or if the data is empty or sorted):
//
// !h.Less(j, i) for 0 <= i < h.Len() and 2*i+1 <= j <= 2*i+2 and j < h.Len()
//
// Note that Push and Pop in this interface are for package heap's
// implementation to call. To add and remove things from the heap,
// use heap.Push and heap.Pop.
type Interface interface {
sort.Interface
Push(x interface{}) // add x as element Len()
Pop() interface{} // remove and return element Len() - 1.
}
// 初始化,调整一般数据就已经OK了
func Init(h Interface) {
// heapify
n := h.Len()
for i := n/2 - 1; i >= 0; i-- {
down(h, i, n)
}
}
//把元素放在最后面,然后向上调整
func Push(h Interface, x interface{}) {
h.Push(x)
up(h, h.Len()-1)
}
//交换第一个和最后一个元素,然后从第一个元素开始向下调整,调整到n-1,然后弹出n-1的数据
func Pop(h Interface) interface{} {
n := h.Len() - 1
h.Swap(0, n)
down(h, 0, n)
return h.Pop()
}
// 移除下标i的数据,即把下标i和最后一个元素互换,然后调整
//先向下调整,如果不需要调整,则位置不变,即也不需要向上调整了,不然就需要向上调整了
func Remove(h Interface, i int) interface{} {
n := h.Len() - 1
if n != i {
h.Swap(i, n)
if !down(h, i, n) {
up(h, i)
}
}
return h.Pop()
}
// 下标i的调整逻辑
func Fix(h Interface, i int) {
if !down(h, i, h.Len()) {
up(h, i)
}
}
//向上调整逻辑
func up(h Interface, j int) {
for {
i := (j - 1) / 2 // 父节点的下标
if i == j || !h.Less(j, i) { //已经到顶了,或者不满足要求了,则跳出for循环
break
}
h.Swap(i, j) //否则交互父节点和当前节点
j = i //继续向上调整
}
}
//向下调整逻辑
func down(h Interface, i0, n int) bool {
i := i0
for {
j1 := 2*i + 1
if j1 >= n || j1 < 0 { // j1 < 0 after int overflow 溢出或者已经到堆底了,则跳出
break
}
j := j1 // 左边子节点
if j2 := j1 + 1; j2 < n && h.Less(j2, j1) {
j = j2 // = 2*i + 2 // 右边节点存在并且比左节点的值更大(大顶堆)或更小(小顶堆) 则选择右节点
}
if !h.Less(j, i) { //不满足条件,跳出
break
}
h.Swap(i, j) //交换,继续向下调整
i = j
}
return i > i0 //判断是否有调整
}
|