-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathheap.js
297 lines (249 loc) · 7.35 KB
/
heap.js
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
const minHeapJSCode = `
class MinHeap {
constructor () {
/* Initialing the array heap and adding a dummy element at index 0 */
this.heap = [null]
}
peek() {
/* Accessing the min element at index 1 in the heap array */
return this.heap[1]
}
push (node) {
/* Inserting the new node at the end of the heap array */
this.heap.push(node)
/* Finding the correct position for the new node */
if (this.heap.length > 1) {
let current = this.heap.length - 1
/* Traversing up the parent node until the current node (current) is greater than the parent (current/2)*/
while (current > 1 && this.heap[Math.floor(current/2)] > this.heap[current]) {
/* Swapping the two nodes by using the ES6 destructuring syntax*/
[this.heap[Math.floor(current/2)], this.heap[current]] = [this.heap[current], this.heap[Math.floor(current/2)]]
current = Math.floor(current/2)
}
}
}
pop() {
/* Smallest element is at the index 1 in the heap array */
let smallest = this.heap[1]
/* When there are more than two elements in the array, we put the right most element at the first position
and start comparing nodes with the child nodes
*/
if (this.heap.length > 2) {
this.heap[1] = this.heap[this.heap.length-1]
this.heap.splice(this.heap.length - 1)
if (this.heap.length === 3) {
if (this.heap[1] > this.heap[2]) {
[this.heap[1], this.heap[2]] = [this.heap[2], this.heap[1]]
}
return smallest
}
let current = 1
let leftChildIndex = current * 2
let rightChildIndex = current * 2 + 1
while (this.heap[leftChildIndex] &&
this.heap[rightChildIndex] &&
(this.heap[current] > this.heap[leftChildIndex] ||
this.heap[current] > this.heap[rightChildIndex])) {
if (this.heap[leftChildIndex] < this.heap[rightChildIndex]) {
[this.heap[current], this.heap[leftChildIndex]] = [this.heap[leftChildIndex], this.heap[current]]
current = leftChildIndex
} else {
[this.heap[current], this.heap[rightChildIndex]] = [this.heap[rightChildIndex], this.heap[current]]
current = rightChildIndex
}
leftChildIndex = current * 2
rightChildIndex = current * 2 + 1
}
}
/* If there are only two elements in the array, we directly splice out the first element */
else if (this.heap.length === 2) {
this.heap.splice(1, 1)
} else {
return null
}
return smallest
}
/**
* Your MinHeap object will be instantiated and called as such:
* var obj = new MinHeap()
* obj.push(1)
* obj.push(2)
* obj.peek() // will return 1
* obj.pop() // remove 1
* obj.peek() // will return 2
*/
}
`;
const minHeapPythonCode = `
class min_heap:
def __init__(self):
self.h = [0]
def shift_up(self, i):
while i // 2 > 0:
if self.h[i] < self.h[i // 2]:
self.h[i], self.h[i//2] = self.h[i//2], self.h[i]
i = i // 2
def shift_down(self, i):
while (i * 2) <= len(self.h)-1:
mc = self.minChild(i)
if self.h[i] > self.h[mc]:
self.h[i], self.h[mc] = self.h[mc], self.h[i]
i = mc
def minChild(self, i):
if i * 2 + 1 > len(self.h)-1:
return i * 2
if self.h[i*2] < self.h[i*2+1]:
return i * 2
else:
return i * 2 + 1
def heappop(self):
if len(self.h) == 1:
return None
ans = self.h[1]
self.h[1] = self.h[len(self.h)-1]
self.h.pop()
self.shift_down(1)
return ans
def heappush(self, a):
self.h.append(a)
self.shift_up(len(self.h)-1)
def build_heap(self, A):
self.h = [0] + A
i = 1
while (i < len(self.h)):
self.shift_down(i)
i = i + 1
# 使用:
h = min_heap()
h.build_heap([5, 6, 2, 3])
h.heappush(1)
h.heappop() # 1
h.heappop() # 2
h.heappush(1)
h.heappop() # 1
h.heappop() # 3
`;
const minHeapJavaCode = `
// by @CaptainZ
import java.util.Arrays;
import java.util.Comparator;
/**
* 用完全二叉树来构建 堆
* 前置条件 起点为 1
* 那么 子节点为 i <<1 和 i<<1 + 1
* 核心方法为
* shiftdown 交换下沉
* shiftup 交换上浮
* <p>
* build 构建堆
*/
public class MinHeap {
int size = 0;
int queue[];
public Heap(int initialCapacity) {
if (initialCapacity < 1)
throw new IllegalArgumentException();
this.queue = new int[initialCapacity];
}
public Heap(int[] arr) {
size = arr.length;
queue = new int[arr.length + 1];
int i = 1;
for (int val : arr) {
queue[i++] = val;
}
}
public void shiftDown(int i) {
int temp = queue[i];
while ((i << 1) <= size) {
int child = i << 1;
// child!=size 判断当前元素是否包含右节点
if (child != size && queue[child + 1] < queue[child]) {
child++;
}
if (temp > queue[child]) {
queue[i] = queue[child];
i = child;
} else {
break;
}
}
queue[i] = temp;
}
public void shiftUp(int i) {
int temp = queue[i];
while ((i >> 1) > 0) {
if (temp < queue[i >> 1]) {
queue[i] = queue[i >> 1];
i >>= 1;
} else {
break;
}
}
queue[i] = temp;
}
public int peek() {
int res = queue[1];
return res;
}
public int pop() {
int res = queue[1];
queue[1] = queue[size--];
shiftDown(1);
return res;
}
public void push(int val) {
if (size == queue.length - 1) {
queue = Arrays.copyOf(queue, size << 1+1);
}
queue[++size] = val;
shiftUp(size);
}
public void buildHeap() {
for (int i = size >> 1; i >= 0; i--) {
shiftDown(i);
}
}
public static void main(String[] args) {
int arr[] = new int[]{2,7,4,1,8,1};
Heap heap = new Heap(arr);
heap.buildHeap();
System.out.println(heap.peek());
heap.push(5);
while (heap.size > 0) {
int num = heap.pop();
System.out.printf(num + "");
}
}
}
`;
module.exports = {
logo: require("../imgs/heap.svg"),
title: "堆",
list: [
{
text: "小顶堆",
problems: [
{
title: "1046. 最后一块石头的重量(这道题需要用大顶堆,不过都差不多)",
id: "last-stone-weight",
},
],
codes: [
{
language: "js",
text: minHeapJSCode,
},
{
language: "py",
text: minHeapPythonCode,
},
{
language: "java",
text: minHeapJavaCode,
},
],
},
],
link: "",
};