-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriority-queue.js
More file actions
81 lines (67 loc) · 2.56 KB
/
Copy pathpriority-queue.js
File metadata and controls
81 lines (67 loc) · 2.56 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
78
79
80
81
class Node {
constructor(val,priority){
this.value = val;
this.priority = priority;
}
}
class PriorityQueue {
constructor(){
this.values = [];
}
enqueue(val, priority){
const newNode = new Node(val, priority);
this.values.push(newNode);
if(this.values.length === 1) return this.values;
const bubbleUp= (node,idx) =>{
if(idx === 0) return this.values;
const parentIdx = Math.floor((idx - 1)/2);
const parentNode = this.values[parentIdx];
const parentPriority = this.values[parentIdx].priority;
if(parentPriority< node.priority) return this.values;
[this.values[parentIdx], this.values[idx]] = [node,parentNode];
return bubbleUp(node,parentIdx);
}
const length = this.values.length;
bubbleUp(this.values[length - 1],length-1);
return this.values;
}
dequeue(){
if(this.values.length <= 1 ) return this.values;
let extractValue;
if(this.values.length === 1) {
extractValue = this.values.pop();
return [];
}
const firstNode = this.values[0];
const lastNode = this.values[this.values.length-1];
[this.values[0],this.values[this.values.length-1]] = [lastNode,firstNode];
extractValue = this.values.pop();
const length = this.values.length;
const sinkDown = (node,idx) =>{
const leftChildIdx = idx*2 + 1;
const rightChildIdx = idx*2 + 2;
const leftChild = leftChildIdx <= length - 1 ? this.values[leftChildIdx] : null;
const rightChild = rightChildIdx <= length - 1 ? this.values[rightChildIdx] : null;
if(!leftChild) return this.values;
let minNode;
let minIdx;
if(rightChild){
minNode = leftChild.priority <= rightChild.priority ? leftChild : rightChild;
minIdx = leftChild.priority <= rightChild.priority ? leftChildIdx : rightChildIdx;
}else{
minNode = leftChild;
minIdx=leftChildIdx;
}
if(node.priority < this.values[minIdx].priority) return this.values;
[this.values[idx], this.values[minIdx]] = [minNode,node];
sinkDown(node,minIdx);
}
sinkDown(this.values[0],0);
return this.values;
}
}
const queue = new PriorityQueue();
queue.enqueue('c',3);
queue.enqueue('b',2);
queue.enqueue('d',4);
queue.enqueue('a',1);