-
Notifications
You must be signed in to change notification settings - Fork 1
/
heap.js
executable file
·82 lines (67 loc) · 1.51 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
// 堆是一个完全二叉树
// 左侧子节点:2 * index + 1
// 右侧子节点:2 * index + 2
// 父节点位置:(index - 1) / 2
class MiniHeap {
constructor() {
this.heap = [];
}
getLeftIndex(index) {
return (index << 1) + 1;
}
getRightIndex(index) {
return (index << 1) + 2;
}
getParentIndex(index) {
return (index - 1) >> 1;
}
peek() {
return this.heap[0];
}
size() {
return this.heap.length;
}
swap(i1, i2) {
[this.heap[i1], this.heap[i2]] = [this.heap[i2], this.heap[i1]];
}
shiftUp(index) {
const parentIndex = this.getParentIndex(index);
if (this.heap[parentIndex] > this.heap[index]) {
this.swap(parentIndex, index);
this.shiftUp(parentIndex);
}
}
shiftDown(index) {
const leftIndex = this.getLeftIndex(index);
const rightIndex = this.getRightIndex(index);
if (this.heap[leftIndex] < this.heap[index]) {
this.swap(index, leftIndex);
this.shiftDown(leftIndex);
}
if (this.heap[rightIndex] < this.heap[index]) {
this.swap(index, rightIndex);
this.shiftDown(rightIndex);
}
}
insert(target) {
this.heap.push(target);
this.shiftUp(this.size() - 1);
}
pop() {
this.heap[0] = this.heap[this.size() - 1];
const top = this.heap.pop();
this.shiftDown(0);
return top;
}
}
// demo
const heap = new MiniHeap();
heap.insert(2);
heap.insert(7);
heap.insert(4);
heap.insert(3);
heap.insert(9);
heap.insert(5);
console.error(heap);
heap.pop();
console.error(heap);