-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriority-queue.js
More file actions
211 lines (158 loc) · 5.25 KB
/
priority-queue.js
File metadata and controls
211 lines (158 loc) · 5.25 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
This implmentation uses bitwise operators
if you are not familiar with bitwise operators
refer to: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Right_shift
assumptions:
smaller number = highest priority
enhacements:
-seperate variables for data and priority
-validate priority is a number upon insertion
steps:
1. create queue
2. create node from data
3. insert data at last position
4. compare using half position logic
5. remove data at first position
6. move last piece to first position
7. compare and bring the data down
data
1
5 10
6 11 20 ___
array = [ 1, 5, 10, 6, 11, 20, __ ]
pos = [ 0, 1, 2 , 3, 4 , 5, 6 ]
*/
class Element {
constructor(priority, value) {
this.value = value;
this.priority = priority;
}
}
class PriorityQueue {
constructor() {
this.queue = [];
}
enqueue(element) {
// const newElement = new Element(priority, value);
this.queue.push(element);
this._bubbleUp(this.queue.length - 1);
}
dequeue() {
const result = this.queue[0];
const lastValue = this.queue.pop();
if (this.queue.length > 0) {
this.queue[0] = lastValue;
this._bubbleDown(0);
}
return result;
}
reprioritizeElement(element){
let n = this.queue.indexOf(element);
console.log(n,element)
console.log(this.queue)
if (n > -1) {
let el = this.queue.splice(n, 1);
this._bubbleUp(n);
console.log('e', el)
return el;
}
}
size() {
return this.queue.length;
}
_bubbleUp(position) {
let element = this.queue[position];
// when at 0, an element is already at first position in array
while (position > 0) {
let parentPosition = position >> 1;
let parent = this.queue[parentPosition];
//if parent element has a higher priority (lower value) then break
if (element.priority >= parent.priority) {
break;
}
//else swpap the current element with the parent
this.queue[parentPosition] = element;
this.queue[position] = parent;
//update the position of the element that is being sorted
position = parentPosition;
}
}
_bubbleDown(position) {
while (true) {
let leftChildPos = (position * 2) + 1;
let leftChild = this.queue[leftChildPos];
let rightChildPos = leftChildPos + 1;
let rightChild = this.queue[rightChildPos];
let minIndex = position;
//if child 1 (left child) exists && has a higher priorty than min element
if (leftChild && (leftChild.priority - this.queue[minIndex].priority < 0)) {
//store this position as having a higher priority
minIndex = leftChildPos;
}
//if child 2 (right child) exists && has a higher priorty than min element
if (rightChild && (rightChild.priority - this.queue[minIndex].priority < 0)) {
minIndex = rightChildPos;
}
//if a higher prioriry element was found
if (minIndex !== position) {
let pointer = this.queue[minIndex];
//swap values
this.queue[minIndex] = this.queue[position];
this.queue[position] = pointer;
//update position value
position = minIndex;
}
else {
break;
}
}
}
data() {
return this.queue;
}
peek() {
return this.queue[0];
}
print() {
console.log(this.queue);
console.log(this.queue.map(function(val) { return val.priority }));
}
}
module.exports.PriorityQueue = PriorityQueue;
/* TESTS
const priorityQueue = new PriorityQueue();
priorityQueue.enqueue(5); // [5]
priorityQueue.enqueue(1); // [1,5]
priorityQueue.enqueue(10); // [1,5,10]
priorityQueue.enqueue(4); // [1,4,10,5]
priorityQueue.enqueue(11); // [1,4,10,11]
priorityQueue.enqueue(9); // [1,4,9,5,11,10]
priorityQueue.print(); // [1,4,9,5,11,10]
priorityQueue.dequeue(); //1
priorityQueue.print(); // [4,5,9,10,11]
priorityQueue.dequeue(); //4
priorityQueue.print(); // [5,10,9,11]
priorityQueue.dequeue(); //5
priorityQueue.print(); // [9,10,11]
priorityQueue.enqueue(9); // [9,9,11,10]
priorityQueue.enqueue(11); // [9,9,11,10,11]
priorityQueue.enqueue(10); // [9,9,10,10,11,11]
priorityQueue.enqueue(12); // [9,9,10,10,11,11,12]
priorityQueue.print(); // [9,9,10,10,11,11,12]
priorityQueue.dequeue(); // [9]
priorityQueue.print(); // [9,10,10,12,11,11]
priorityQueue.dequeue(); // [9]
priorityQueue.print(); // [10,11,10,12,11]
priorityQueue.dequeue(); // [10]
priorityQueue.print(); // [10,11,12,11]
priorityQueue.dequeue(); // [10]
priorityQueue.print(); // [11,12,11]
priorityQueue.dequeue(); // [11]
priorityQueue.print(); // [11,12]
priorityQueue.dequeue(); // [11]
priorityQueue.print(); // [12]
priorityQueue.dequeue(); // [12]
priorityQueue.print(); // [null]
priorityQueue.dequeue(); // undefined
priorityQueue.print(); // [null]
*/