-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path23.最小堆.js
72 lines (64 loc) · 1.85 KB
/
23.最小堆.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
/**
* 最小堆:首先是一个完全二叉树,然后是每个节点都小于等于其左右孩子节点。
* 新增元素:将元素插入到堆的底部,然后向上调整,直到满足最小堆要求。
* 删除元素:将堆顶元素与堆中最后一个元素交换,然后删除最后一个元素,然后向下调整,直到满足最小堆要求。
*/
class MinHeap {
constructor() {
this.heap = []; // 存放堆中的元素
}
add(value) {
this.heap.push(value);
this.slideUp(this.heap.length - 1);
console.log('heap', this.heap.toString())
}
remove() {
if (this.heap.length === 0) return undefined;
this.swap(0, this.heap.length - 1);
const value = this.heap.pop();
this.slideDown(0);
console.log('heap', this.heap.toString())
return value;
}
// 向上调整
slideUp(index) {
if (index > 0) {
const parentIndex = Math.floor((index - 1) / 2);
if (this.heap[parentIndex] > this.heap[index]) {
this.swap(index, parentIndex);
this.slideUp(parentIndex);
}
}
}
// 向下调整
slideDown(index) {
const len = this.heap.length;
if (index < len - 1) {
const leftIndex = index * 2 + 1;
const rightIndex = index * 2 + 2;
let minIndex = index;
if (leftIndex < len && this.heap[leftIndex] < this.heap[minIndex]) {
minIndex = leftIndex;
}
if (rightIndex < len && this.heap[rightIndex] < this.heap[minIndex]) {
minIndex = rightIndex;
}
if (index !== minIndex) {
this.swap(index, minIndex);
this.slideDown(minIndex);
}
}
}
swap(i, j) {
[this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]];
}
}
const minHeap = new MinHeap();
minHeap.add(3);
minHeap.add(2);
minHeap.remove();
minHeap.add(4);
minHeap.add(6);
minHeap.remove();
minHeap.add(5);
minHeap.add(1);