-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinHeap.js
More file actions
71 lines (71 loc) · 1.75 KB
/
MinHeap.js
File metadata and controls
71 lines (71 loc) · 1.75 KB
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
/*
* 最小堆
* 堆的本质是一种树形结构
*/
module.exports = class MinHeap {
constructor() {
this.data = [];
this.count = 0;
}
// 初始化数组
init(arr) {
this.data = arr;
this.count = arr.length;
for(var i = Math.floor(this.count/2-1); i >= 0; i--) {
this.shiftDown(i);
}
}
// 提升元素
// 把小的元素位置往上提升
shiftUp(k) {
while(k > 0 && this.data[Math.floor((k-1)/2)] > this.data[k]) {
this.swap(this.data, Math.floor((k-1)/2), k);
k = Math.floor((k-1)/2);
}
}
// 下降元素
// 当顶部弹出元素后,用于重新获得最小的元素
shiftDown(k) {
while(2*k+1 < this.count) {
var j = 2*k+1;
if(j+1 < this.count && this.data[j+1] < this.data[j]) {
j++;
}
if(this.data[k] <= this.data[j]) break;
this.swap(this.data, k, j);
k = j;
}
}
// 向最小堆中插入一个新元素
insert(element) {
this.data.push(element);
this.shiftUp(this.count);
this.count++;
}
// 从最小堆中取出堆顶元素
extractMin() {
var ret = this.data[0];
this.swap(this.data, 0, this.count-1);
this.count--;
this.shiftDown(0);
return ret;
}
// 获取最小堆中的堆顶元素
getMin() {
return this.data[0];
}
// 返回堆中元素的个数
size() {
return this.count;
}
// 判断当前堆中是否为空
isEmpty() {
return this.count === 0;
}
// 交换元素
swap(arr, a, b) {
var t = arr[a];
arr[a] = arr[b];
arr[b] = t;
}
}