-
Notifications
You must be signed in to change notification settings - Fork 0
/
minBinaryHeap.js
executable file
·64 lines (53 loc) · 1.62 KB
/
minBinaryHeap.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
class Node {
constructor(v, p) {
this.v = v
this.p = p
}
}
class MinBinaryHeap {
constructor() {
this.values = []
}
swap(i, j) {
const temp = this.values[i]
this.values[i] = this.values[j]
this.values[j] = temp
}
enqueue(v, p) {
const new_vertex = new Node(v, p)
const i = this.values.push(new_vertex) - 1
const bubbleUp = i => {
const parent_i = (i - (i % 2 === 0 ? 2 : 1)) / 2
if (this.values[parent_i] && new_vertex.p < this.values[parent_i].p) {
this.swap(i, parent_i)
bubbleUp(parent_i)
} else {
return
}
}
bubbleUp(i)
return this.values.length
}
dequeue() {
this.swap(0, this.values.length - 1)
const old_head = this.values.pop()
const bubbleDown = i => {
const first_child_i = 2 * i + 1
const second_child_i = 2 * i + 2
const first_child_p = this.values[first_child_i]
? this.values[first_child_i].p
: Infinity
const second_child_p = this.values[second_child_i]
? this.values[second_child_i].p
: Infinity
const swap_i = first_child_p < second_child_p ? first_child_i : second_child_i
if (!this.values[swap_i]) return
if (this.values[i].p > this.values[swap_i].p) {
this.swap(i, swap_i)
bubbleDown(swap_i)
}
}
if (this.values.length > 1) bubbleDown(0)
return old_head
}
}