Skip to content

Commit da56f1a

Browse files
committed
Added priority queue
1 parent 0d604bc commit da56f1a

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

data-structures/priorityQueue.js

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/**
2+
* @author Rashik Ansar
3+
*
4+
* Implemntaion of priority queue
5+
* Lower the priority value higher its priority
6+
* under the hood its implementing minBinaryHeap
7+
*/
8+
class PriorityQueue {
9+
constructor() {
10+
this.values = [];
11+
}
12+
13+
/**
14+
* Adding data to the queue
15+
* @param {*} data Data to add into queue
16+
* @param {*} priority Priority of the data
17+
* @returns {PriorityQueue}
18+
*/
19+
enqueue(data, priority) {
20+
let temp = new Node(data, priority);
21+
this.values.push(temp);
22+
this.bubbleUp();
23+
return this;
24+
}
25+
26+
/**
27+
* removing a node from the queue
28+
* @returns {Node}
29+
*/
30+
dequeue() {
31+
const min = this.values[0];
32+
const end = this.values.pop();
33+
if (this.values.length > 0) {
34+
this.values[0] = end;
35+
this.sinkDown();
36+
}
37+
return min;
38+
}
39+
40+
/**
41+
* enqueue helper function
42+
*/
43+
bubbleUp() {
44+
let index = this.values.length - 1;
45+
const element = this.values[index];
46+
while (index > 0) {
47+
let parentIndex = Math.floor((index - 1) / 2);
48+
let parent = this.values[parentIndex];
49+
// if (element.priority <= parent.priority) break; //maxBinaryHeap condition
50+
if (element.priority >= parent.priority) break; //minBinaryHeap condition
51+
this.values[parentIndex] = element;
52+
this.values[index] = parent;
53+
index = parentIndex;
54+
}
55+
}
56+
57+
/**
58+
* dequeue helper function
59+
*/
60+
sinkDown() {
61+
let index = 0;
62+
const length = this.values.length;
63+
const element = this.values[index];
64+
while (true) {
65+
let leftChildIndex = 2 * index + 1;
66+
let rightChildIndex = 2 * index + 2;
67+
let leftChild;
68+
let rightChild;
69+
let swap = null;
70+
71+
if (leftChildIndex < length) {
72+
leftChild = this.values[leftChildIndex];
73+
// Change below comparision operators to make maxBinaryHeap
74+
if (leftChild.priority < element.priority) {
75+
swap = leftChildIndex;
76+
}
77+
}
78+
79+
if (rightChildIndex < length) {
80+
rightChild = this.values[rightChildIndex];
81+
// Change below comparision operators to make maxBinaryHeap
82+
if (
83+
(!swap && rightChild.priority < element.priority) ||
84+
(swap && rightChild.priority < leftChild.priority)
85+
) {
86+
swap = rightChildIndex;
87+
}
88+
}
89+
90+
if (!swap) break;
91+
92+
this.values[index] = this.values[swap];
93+
this.values[swap] = element;
94+
index = swap;
95+
}
96+
}
97+
}
98+
99+
class Node {
100+
constructor(data, priority) {
101+
this.data = data;
102+
this.priority = priority;
103+
}
104+
}
105+
106+
let a = new PriorityQueue();
107+
a.enqueue('Common Cold', 10);
108+
a.enqueue('Gunshot wound', 2);
109+
a.enqueue('Fever', 8);
110+
111+
console.log(a);
112+
a.dequeue();
113+
console.log(a);
114+
a.dequeue();
115+
console.log(a);
116+
a.dequeue();
117+
console.log(a);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy