forked from nim-ka/aocutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinheap.js
60 lines (48 loc) · 1.2 KB
/
binheap.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
BinHeap = class BinHeap {
constructor(cond = (p, c) => p < c) {
this.cond = cond
this.data = []
}
getTop() { return this.data[0] }
getParent(idx) { return idx / 2 | 0 }
getChildLeft(idx) { return 2 * idx }
getChildRight(idx) { return 2 * idx + 1 }
insert(val) {
this.up(this.data.push(val) - 1)
}
extract() {
let res = this.data[0]
if (this.data.length > 1) {
this.data[0] = this.data.pop()
this.down(0)
} else {
this.data = []
}
return res
}
up(idx) {
while (this.getParent(idx) != idx && !this.cond(this.data[this.getParent(idx)], this.data[idx])) {
let tmp = this.data[this.getParent(idx)]
this.data[this.getParent(idx)] = this.data[idx]
this.data[idx] = tmp
idx = this.getParent(idx)
}
}
down(idx) {
let largest = idx
let left = this.getChildLeft(idx)
let right = this.getChildRight(idx)
if (left < this.data.length && this.cond(this.data[left], this.data[largest])) {
largest = left
}
if (right < this.data.length && this.cond(this.data[right], this.data[largest])) {
largest = right
}
if (largest != idx) {
let tmp = this.data[largest]
this.data[largest] = this.data[idx]
this.data[idx] = tmp
this.down(largest)
}
}
}