-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpriority-queue.js
73 lines (70 loc) · 2.3 KB
/
priority-queue.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
65
66
67
68
69
70
71
// priority queue with max heap data structure;
// for min and max heap data structure look at heap data structure directory;
class MaxPriorityQueue {
constructor(size) {
this.size = size;
this.queue = [];
this.parentIndex = (i) => Math.floor(i - 1 / 2);
this.l_index = (i) => i * 2 + 1;
this.r_index = (i) => i * 2 + 2;
}
insert(item) {
if (isNaN(item)) { throw new Error('the argument must be a comparable or number') }
if (this.queue.length >= this.size) {
console.log('the array is full')
return;
}
this.queue.push(item);
this.heapifyUp();
console.log(this.queue);
return item;
}
remove(item) {
if (isNaN(item)) { throw new Error('the argument must be a comparable or number') }
if (this.queue.length <= 0) {
console.log('the array is empty')
return;
}
let itemIndex;
for (let i = 0; i < this.queue.length; i++) {
if (this.queue[i] === item) {
itemIndex = i;
break;
}
}
if (isNaN(itemIndex)) {
console.log('the item arent there')
return;
} // check for item are exists;
[this.queue[itemIndex], this.queue[this.queue.length - 1]] = [this.queue[this.queue.length - 1], this.queue[itemIndex]]
this.queue.pop();
this.heapifyDown(itemIndex);
console.log(this.queue);
return item
}
heapifyUp() {
let index = this.queue.length - 1;
while (this.parentIndex(index) >= 0 && this.queue[this.parentIndex(index)] < this.queue[index]) {
[this.queue[index], this.queue[this.parentIndex(index)]] = [this.queue[this.parentIndex(index)], this.queue[index]];
index = this.parentIndex(index);
}
}
heapifyDown(rm_index) {
let index = rm_index;
while (this.l_index(index) < this.queue.length) {
let childIndex = this.l_index(index);
if (this.r_index(index) < this.queue.length &&
this.queue[this.l_index(index)] < this.queue[this.r_index(index)]
) { childIndex = this.r_index(index); }
if (this.queue[childIndex] > this.queue[index]) {
[this.queue[childIndex], this.queue[index]] = [this.queue[index], this.queue[childIndex]];
index = childIndex
} else {
break;
}
}
}
}
const priorityQueue = new MaxPriorityQueue(10);
priorityQueue.insert("ITEM");
priorityQueue.remove("ITEM");