-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpq.js
More file actions
77 lines (70 loc) · 1.99 KB
/
pq.js
File metadata and controls
77 lines (70 loc) · 1.99 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
66
67
68
69
70
71
72
73
74
75
76
77
class Node {
constructor(value, priority){
this.value = value;
this.priority = priority;
}
}
class MinBinaryHeap {
constructor(){
this.values = [];
}
enqueue(value, priority){
let newNode = new Node(value, priority);
this.values.push(newNode);
let index = this.values.length - 1;
while(index > 0){
let parentIndex = Math.floor((index -1)/2);
let parent = this.values[parentIndex];
if(newNode.priority >= parent.priority) break;
this.values[parentIndex] = newNode;
this.values[index] = parent;
index = parentIndex;
}
}
dequeue(){
const removed = this.values[0];
const end = this.values.pop();
if(this.values.length > 0){
this.values[0] = end;
this.sinkDown();
}
return removed;
}
sinkDown(){
let idx = 0;
const length = this.values.length;
const element = this.values[0];
while(true){
let leftChildIdx = 2 * idx + 1;
let rightChildIdx = 2 * idx + 2;
let leftChild, rightChild;
let swap = null;
if(leftChildIdx < length){
leftChild = this.values[leftChildIdx];
if(leftChild.priority < element.priority){
swap = leftChildIdx;
}
}
if(rightChildIdx < length){
rightChild = this.values[rightChildIdx];
if(
(swap === null && rightChild.priority < element.priority) || (swap !== null && rightChild.priority < leftChild.priority)
){
swap = rightChildIdx;
}
}
if(swap === null) break;
this.values[idx] = this.values[swap];
this.values[swap] = element;
idx = swap;
}
}
}
let priorityQueue = new MinBinaryHeap();
priorityQueue.enqueue("This is cool", 5);
priorityQueue.enqueue("This is not cool at all!", 3);
priorityQueue.enqueue("This is a real life emergency!", 1);
priorityQueue.enqueue("This is a realer life emergency!", 0);
priorityQueue.enqueue("This is a fake life emergency!", 2);
priorityQueue.dequeue();
console.log(priorityQueue)