Skip to content

Commit e1827e1

Browse files
committed
Add comments to code
1 parent a65c831 commit e1827e1

File tree

3 files changed

+133
-1
lines changed

3 files changed

+133
-1
lines changed

src/index.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
11
import { IPriorityQueue } from "../typing/priority-queue";
22

3+
/**
4+
* The priority queue is a collection in which items can be added at any time, but the only item that can be removed is the one with the highest priority.
5+
* A heap is a complete binary tree in which the value of a node is less than or greater than all the values in its subtrees.
6+
* By convention, the smallest or largest element is the one with the highest priority.
7+
* This lib using "Max Heap" as the default to heapify.
8+
* @example Changing from Max Priority Queue to Min Priority Queue
9+
* const p = new PriorityQueue(function(a, b) { return a > b; });
10+
* @example Add elements to queue
11+
* const p = new PriorityQueue();
12+
* p.push(1); // adding "1" to queue
13+
* p.push(2); // adding "2" to queue
14+
* p.push(3); // adding "3" to queue
15+
* // After "push", queue looks like this: [3, 1, 2]
16+
* @example Extract the largest or smallest element from the queue
17+
* const elmenet = p.pop(); // return "3" which is the largest element in queue
18+
* // After "pop", queue looks like this: [2, 1]
19+
* @example Peek the element (get the largest or smallest element without removing it from queue)
20+
* const elmenet = p.top(); // return "3" which is the largest element in queue
21+
* // After "pop", queue looks like this: [3, 1, 2]
22+
* @example Get the size of the queue
23+
* const size = p.size(); // return "3", because the queue has 3 elements
24+
* @example Check whether the queue is empty or not
25+
* const isEmpty = p.empty();
26+
* // return true, if the queue has elements
27+
* // return false, if the queue is empty
28+
* @example Extract queue to array
29+
* const array = p.toArray(); // This will extract all elements from queue to array
30+
* // return array = [3, 1, 2];
31+
*/
332
export class PriorityQueue<T> implements IPriorityQueue<T> {
433
private queue: T[];
534
private comparator: (item1: T, item2: T) => boolean;
@@ -11,6 +40,7 @@ export class PriorityQueue<T> implements IPriorityQueue<T> {
1140

1241
/**
1342
* Inserts the specified element into this priority queue.
43+
* Everytime adding new element to queue, the queue is started "sift up" to rebuild the heap
1444
* @param value
1545
*/
1646
public push(value: T) {
@@ -35,6 +65,7 @@ export class PriorityQueue<T> implements IPriorityQueue<T> {
3565

3666
/**
3767
* Retrieves and removes the head of this queue, or returns null if this queue is empty.
68+
* Everytime pop element from queue, the queue is started "sift down" to rebuild the heap
3869
*/
3970
public pop() {
4071
if (this.queue.length === 0) {

test/fixtures/test-case.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
11
export const testCase1 = [2, 7, 4, 1, 8, 1];
22
export const testCase2 = [4, 3, 4, 3, 2];
3+
4+
export const testCase3 = [
5+
{ text: 'a', value: 2 },
6+
{ text: 'b', value: 7 },
7+
{ text: 'c', value: 4 },
8+
{ text: 'd', value: 1 },
9+
{ text: 'e', value: 8 },
10+
{ text: 'f', value: 1 },
11+
];

test/priority-queue.spec.ts

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { PriorityQueue } from '../src';
22
import { IPriorityQueue } from '../typing/priority-queue';
3-
import { testCase1, testCase2 } from './fixtures/test-case';
3+
import { testCase1, testCase2, testCase3 } from './fixtures/test-case';
44

55
describe('testing priority queue', () => {
66
let p: IPriorityQueue<number>;
@@ -80,6 +80,98 @@ describe('testing priority queue', () => {
8080

8181
});
8282

83+
describe('testing with array of object', () => {
84+
it('should return max heap of test case 3', () => {
85+
p = new PriorityQueue(function (a: any, b: any) {
86+
return a.value < b.value;
87+
});
88+
89+
for (let i = 0; i < testCase3.length; i++) {
90+
p.push(testCase3[i] as any);
91+
}
92+
const actual = p.toArray();
93+
const expected = [
94+
{ text: 'e', value: 8 },
95+
{ text: 'b', value: 7 },
96+
{ text: 'c', value: 4 },
97+
{ text: 'd', value: 1 },
98+
{ text: 'a', value: 2 },
99+
{ text: 'f', value: 1 },
100+
];
101+
expect(actual).toEqual(expected);
102+
});
103+
104+
it('should return min heap of test case 3', () => {
105+
p = new PriorityQueue(function (a: any, b: any) {
106+
return a.value > b.value;
107+
});
108+
109+
for (let i = 0; i < testCase3.length; i++) {
110+
p.push(testCase3[i] as any);
111+
}
112+
const actual = p.toArray();
113+
const expected = [
114+
{ text: 'd', value: 1 },
115+
{ text: 'a', value: 2 },
116+
{ text: 'f', value: 1 },
117+
{ text: 'b', value: 7 },
118+
{ text: 'e', value: 8 },
119+
{ text: 'c', value: 4 }
120+
];
121+
expect(actual).toEqual(expected);
122+
});
123+
124+
it('should extract the "max" value & rebuild "max" heap of test case 3', () => {
125+
p = new PriorityQueue(function (a: any, b: any) {
126+
return a.value < b.value;
127+
});
128+
129+
for (let i = 0; i < testCase3.length; i++) {
130+
p.push(testCase3[i] as any);
131+
}
132+
133+
expect(p.size()).toEqual(testCase3.length);
134+
const max = p.pop();
135+
expect(max).toEqual({ text: 'e', value: 8 });
136+
expect(p.size()).toEqual(testCase3.length - 1);
137+
138+
const actual = p.toArray();
139+
const expected = [
140+
{ text: 'b', value: 7 },
141+
{ text: 'a', value: 2 },
142+
{ text: 'c', value: 4 },
143+
{ text: 'd', value: 1 },
144+
{ text: 'f', value: 1 }
145+
];
146+
expect(actual).toEqual(expected);
147+
});
148+
149+
it('should extract the "min" value & rebuild "min" heap of test case 3', () => {
150+
p = new PriorityQueue(function (a: any, b: any) {
151+
return a.value > b.value;
152+
});
153+
154+
for (let i = 0; i < testCase3.length; i++) {
155+
p.push(testCase3[i] as any);
156+
}
157+
158+
expect(p.size()).toEqual(testCase3.length);
159+
const min = p.pop();
160+
expect(min).toEqual({ text: 'd', value: 1 });
161+
expect(p.size()).toEqual(testCase3.length - 1);
162+
163+
const actual = p.toArray();
164+
const expected = [
165+
{ text: 'f', value: 1 },
166+
{ text: 'a', value: 2 },
167+
{ text: 'c', value: 4 },
168+
{ text: 'b', value: 7 },
169+
{ text: 'e', value: 8 }
170+
];
171+
expect(actual).toEqual(expected);
172+
});
173+
});
174+
83175
describe('min priority queue', () => {
84176
beforeEach(() => {
85177
p = new PriorityQueue(function (a, b) {

0 commit comments

Comments
 (0)