-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinarySearchPriorityQueue.js
More file actions
70 lines (57 loc) · 1.66 KB
/
Copy pathBinarySearchPriorityQueue.js
File metadata and controls
70 lines (57 loc) · 1.66 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
function QueueElement(element, priority) {
this.element = element;
this.priority = priority;
}
class PriorityQueue {
constructor() {
this.items = [];
}
// Insert with binary search to find the correct insertion index
enQueue(element, priority) {
const item = new QueueElement(element, priority);
const index = this.findInsertIndex(priority);
this.items.splice(index, 0, item); // Insert at the correct position
}
// Binary search to find the correct index based on priority
findInsertIndex(priority) {
let low = 0;
let high = this.items.length - 1;
while (low <= high) {
const mid = Math.floor((low + high) / 2);
if (this.items[mid].priority === priority) {
// If priorities are the same, we insert here
return mid;
} else if (this.items[mid].priority < priority) {
low = mid + 1; // Target priority is higher, move right
} else {
high = mid - 1; // Target priority is lower, move left
}
}
return low; // Low is now the correct insertion point
}
deQueue() {
return this.items.shift(); // Remove the highest priority element
}
firstItem() {
return this.items[0];
}
isEmpty() {
return this.items.length === 0;
}
clear() {
this.items = [];
}
size() {
return this.items.length;
}
print() {
console.log(this.items.map(item => `${item.element} (priority: ${item.priority})`));
}
}
// Example usage
const queue = new PriorityQueue();
queue.enQueue("Task 1", 1);
queue.enQueue("Task 2", 3);
queue.enQueue("Task 3", 2);
queue.enQueue("Task 4", 4);
queue.print(); // Expected order based on priority: Task 1, Task 3, Task 2, Task 4