Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit edf8808

Browse files
feat(pq): improves docs and usability of priority queues
1 parent 827177f commit edf8808

File tree

2 files changed

+99
-1
lines changed

2 files changed

+99
-1
lines changed
Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
const Heap = require('./heap');
22

3-
class PriorityQueue extends Heap { }
3+
class PriorityQueue extends Heap {
4+
constructor(iterable = [], comparator = (a, b) => a[0] - b[0]) {
5+
super(comparator);
6+
Array.from(iterable).forEach((el) => this.add(el));
7+
}
8+
9+
/**
10+
* Add data to the Queue with Priority
11+
* @param {[number, any]|any} value - Pair with [priority, value]
12+
* any object as value is also possible if a custom comparator is passed in.
13+
* @returns {void}
14+
*/
15+
enqueue(value) {
16+
super.add(value);
17+
}
18+
19+
/**
20+
* Remove from the queue the element with the highest priority.
21+
* @returns {[number, any]|any}
22+
*/
23+
dequeue() {
24+
return super.remove();
25+
}
26+
}
427

528
module.exports = PriorityQueue;
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
const { PriorityQueue } = require('../..');
2+
3+
describe('Priorty Queue (as MinHeap default)', () => {
4+
const num = 1;
5+
const obj = { a: 1, b: 2 };
6+
let pq;
7+
8+
describe('with default contructor', () => {
9+
beforeEach(() => {
10+
pq = new PriorityQueue();
11+
});
12+
13+
describe('.enqueue', () => {
14+
it('should enqueue [priority, element]', () => {
15+
pq.enqueue([Infinity, 2]);
16+
pq.enqueue([0, 1]);
17+
pq.enqueue([100, { a: 1, b: 2 }]);
18+
expect(pq.size).toEqual(3);
19+
expect(pq.peek()).toEqual([0, 1]);
20+
});
21+
});
22+
23+
describe('.dequeue', () => {
24+
it('should enqueue and dequeue elements on priority order', () => {
25+
pq.enqueue([100, obj]);
26+
pq.enqueue([Infinity, 2]);
27+
pq.enqueue([0, num]);
28+
29+
expect(pq.dequeue()).toEqual([0, num]);
30+
expect(pq.size).toEqual(2);
31+
expect(pq.dequeue()).toEqual([100, obj]);
32+
expect(pq.dequeue()).toEqual([Infinity, 2]);
33+
expect(pq.size).toEqual(0);
34+
});
35+
36+
it('should handle case when priorty was forgotten', () => {
37+
expect(() => pq.enqueue({ a: 100 })).not.toThrow();
38+
expect(() => pq.enqueue({ b: 200 })).toThrow();
39+
});
40+
});
41+
});
42+
43+
describe('with default values', () => {
44+
it('should add values on creation', () => {
45+
pq = new PriorityQueue([[100, obj], [Infinity, 2], [0, num]]);
46+
expect(pq.size).toEqual(3);
47+
expect(pq.peek()).toEqual([0, num]);
48+
expect(pq.dequeue()).toEqual([0, num]);
49+
expect(pq.size).toEqual(2);
50+
});
51+
});
52+
53+
describe('with custom comparator', () => {
54+
const alice = { name: 'Alice', grade: 80, assistance: 1 };
55+
const bob = { name: 'Bob', grade: 93, assistance: 0.7 };
56+
const ana = { name: 'Ana', grade: 98, assistance: 0.8 };
57+
58+
it('should become MaxPriortyQueue and compare objects', () => {
59+
pq = new PriorityQueue([], (a, b) => b.grade * b.assistance - a.grade * a.assistance);
60+
pq.enqueue(alice);
61+
pq.enqueue(ana);
62+
pq.enqueue(bob);
63+
expect(pq.size).toEqual(3);
64+
expect(pq.dequeue()).toEqual(alice);
65+
expect(pq.dequeue()).toEqual(ana);
66+
expect(pq.dequeue()).toEqual(bob);
67+
});
68+
69+
it('should handle errors', () => {
70+
pq = new PriorityQueue([], (a, b) => b.grade - a.grade);
71+
expect(() => pq.enqueue(alice)).not.toThrow();
72+
expect(() => pq.enqueue({ name: 'Oops', error: 98 })).toThrow();
73+
});
74+
});
75+
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /