Repository navigation
Expand file tree
/
Copy pathPriorityQueue.js
More file actions
65 lines (58 loc) · 2.02 KB
/
Copy pathPriorityQueue.js
File metadata and controls
65 lines (58 loc) · 2.02 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
class Node {
constructor(val, priority = 5) {
this.val = val;
this.priority = priority;
}
}
class PriorityQueue {
constructor() {
this.values = [];
}
enqueue(val, priority) {
const newNode = new Node(val, priority);
this.values.push(newNode);
let currentIndex = this.values.length-1;
let parentIndex = Math.floor((currentIndex-1)/2);
while (currentIndex > 0 && this.values[currentIndex].priority < this.values[parentIndex].priority) {
[this.values[currentIndex], this.values[parentIndex]] = [this.values[parentIndex], this.values[currentIndex]];
currentIndex = parentIndex;
parentIndex = Math.floor((currentIndex-1)/2);
}
}
dequeue() {
if(this.values.length === 0) return null;
if(this.values.length === 1) return this.values.pop();
const extractedMin = this.values.splice(0, 1, this.values.pop())[0];
let willSwapOccur = true;
let parentIdx = 0;
while(willSwapOccur) {
const parent = this.values[parentIdx];
const leftChildIdx = 2 * parentIdx + 1;
const rightChildIdx = 2 * parentIdx + 2;
const leftChild = this.values[leftChildIdx];
const rightChild = this.values[rightChildIdx];
if((leftChild !== undefined && leftChild.priority < parent.priority) || (rightChild !== undefined && rightChild.priority < parent.priority)) {
if (rightChild !== undefined && rightChild.priority < leftChild.priority) {
this.values[parentIdx] = rightChild;
this.values[rightChildIdx] = parent;
parentIdx = rightChildIdx;
} else {
this.values[parentIdx] = leftChild;
this.values[leftChildIdx] = parent;
parentIdx = leftChildIdx;
}
} else {
willSwapOccur = false;
}
}
return extractedMin;
}
}
var erList = new PriorityQueue();
erList.enqueue("common cold", 5);
erList.enqueue("gunshot wound", 1);
erList.enqueue("high fever", 4);
erList.enqueue("broken arm", 2);
console.log(erList.values);
erList.dequeue();
console.log(erList.values);