Skip to content

Commit

Permalink
Merge pull request #38 from datastructures-js/add_push_pop
Browse files Browse the repository at this point in the history
add push/pop
  • Loading branch information
eyas-ranjous committed May 30, 2022
2 parents 14eb47a + b992613 commit 6dd335f
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 39 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
## [6.1.0] - 2022-05-30
### Added
- push & pop as alias methods for enqueue & dequeue

## [6.0.0] - 2022-03-19
### Changed
- new version: improved usage and types
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ A heap-based implementation of priority queue in javascript with typescript supp
* [API](#api)
* [constructor](#constructor)
* [fromArray](#fromarray)
* [enqueue](#enqueue)
* [enqueue (push)](#enqueue-push)
* [front](#front)
* [back](#back)
* [dequeue](#dequeue)
* [dequeue (pop)](#dequeue-pop)
* [isEmpty](#isEmpty)
* [size](#size)
* [toArray](#toarray)
Expand Down Expand Up @@ -176,7 +176,7 @@ mpq.dequeue(); // 3
console.log(numbers); // [ 0, -1, -5, -2 ]
```

### enqueue
### enqueue (push)
adds a value based on its comparison with other values in the queue in O(log(n)) runtime.

```js
Expand All @@ -192,7 +192,7 @@ const cars = [
cars.forEach((car) => carsQueue.enqueue(car));

const numbers = [3, -2, 5, 0, -1, -5, 4];
numbers.forEach((num) => numbersQueue.enqueue(num));
numbers.forEach((num) => numbersQueue.push(num));

const bids = [
{ id: 1, value: 1000 },
Expand Down Expand Up @@ -224,7 +224,7 @@ console.log(numbersQueue.back()); // 5
console.log(bidsQueue.back()); // { id: 1, value: 1000 }
```

### dequeue
### dequeue (pop)
removes and returns the element with highest priority in the queue in O(log(n)) runtime.

```js
Expand All @@ -236,9 +236,9 @@ console.log(numbersQueue.dequeue()); // -5
console.log(numbersQueue.dequeue()); // -2
console.log(numbersQueue.dequeue()); // -1

console.log(bidsQueue.dequeue()); // { id: 2, value: 20000 }
console.log(bidsQueue.dequeue()); // { id: 5, value: 12000 }
console.log(bidsQueue.dequeue()); // { id: 7, value: 8000 }
console.log(bidsQueue.pop()); // { id: 2, value: 20000 }
console.log(bidsQueue.pop()); // { id: 5, value: 12000 }
console.log(bidsQueue.pop()); // { id: 7, value: 8000 }
```

### isEmpty
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@datastructures-js/priority-queue",
"version": "6.0.0",
"version": "6.1.0",
"description": "A heap-based implementation of priority queue in javascript with typescript support.",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 2 additions & 0 deletions src/maxPriorityQueue.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ export class MaxPriorityQueue<T> {
front(): T;
back(): T;
enqueue(value: T): MaxPriorityQueue<T>;
push(value: T): MaxPriorityQueue<T>;
dequeue(): T;
pop(): T;
toArray(): T[];
clear(): void;
static fromArray<T>(values: T[], getCompareValue?: IGetCompareValue<T>): MaxPriorityQueue<T>;
Expand Down
19 changes: 19 additions & 0 deletions src/maxPriorityQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ class MaxPriorityQueue {
return this._heap.insert(value);
}

/**
* Adds a value to the queue
* @public
* @param {number|string|object} value
* @returns {MaxPriorityQueue}
*/
push(value) {
return this.enqueue(value);
}

/**
* Removes and returns an element with highest priority in the queue
* @public
Expand All @@ -60,6 +70,15 @@ class MaxPriorityQueue {
return this._heap.extractRoot();
}

/**
* Removes and returns an element with highest priority in the queue
* @public
* @returns {number|string|object}
*/
pop() {
return this.dequeue();
}

/**
* Returns the number of elements in the queue
* @public
Expand Down
2 changes: 2 additions & 0 deletions src/minPriorityQueue.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ export class MinPriorityQueue<T> {
front(): T;
back(): T;
enqueue(value: T): MinPriorityQueue<T>;
push(value: T): MinPriorityQueue<T>;
dequeue(): T;
pop(): T;
toArray(): T[];
clear(): void;
static fromArray<T>(values: T[], getCompareValue?: IGetCompareValue<T>): MinPriorityQueue<T>;
Expand Down
19 changes: 19 additions & 0 deletions src/minPriorityQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ class MinPriorityQueue {
return this._heap.insert(value);
}

/**
* Adds a value to the queue
* @public
* @param {number|string|object} value
* @returns {MinPriorityQueue}
*/
push(value) {
return this.enqueue(value);
}

/**
* Removes and returns an element with highest priority in the queue
* @public
Expand All @@ -59,6 +69,15 @@ class MinPriorityQueue {
return this._heap.extractRoot();
}

/**
* Removes and returns an element with highest priority in the queue
* @public
* @returns {number|string|object}
*/
pop() {
return this.dequeue();
}

/**
* Returns the number of elements in the queue
* @public
Expand Down
2 changes: 2 additions & 0 deletions src/priorityQueue.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ export class PriorityQueue<T> {
front(): T;
back(): T;
enqueue(value: T): PriorityQueue<T>;
push(value: T): PriorityQueue<T>;
dequeue(): T;
pop(): T;
toArray(): T[];
clear(): void;
static fromArray<T>(values: T[], compare: ICompare<T>): PriorityQueue<T>;
Expand Down
19 changes: 19 additions & 0 deletions src/priorityQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ class PriorityQueue {
return this._heap.insert(value);
}

/**
* Adds a value to the queue
* @public
* @param {number|string|object} value
* @returns {PriorityQueue}
*/
push(value) {
return this.enqueue(value);
}

/**
* Removes and returns an element with highest priority in the queue
* @public
Expand All @@ -60,6 +70,15 @@ class PriorityQueue {
return this._heap.extractRoot();
}

/**
* Removes and returns an element with highest priority in the queue
* @public
* @returns {number|string|object}
*/
pop() {
return this.dequeue();
}

/**
* Returns the number of elements in the queue
* @public
Expand Down
20 changes: 10 additions & 10 deletions test/PriorityQueue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ describe('PriorityQueue', () => {
describe('PriorityQueue with max logic', () => {
const maxQ = new PriorityQueue(charComparator);

it('enqueue', () => {
charValues.forEach((value) => maxQ.enqueue(value));
it('enqueue (push)', () => {
charValues.forEach((value) => maxQ.push(value));
});

it('toArray', () => {
Expand All @@ -96,14 +96,14 @@ describe('PriorityQueue', () => {
expect(maxQ.isEmpty()).to.equal(false);
});

it('dequeue', () => {
expect(maxQ.dequeue()).to.deep.equal({ id: 'z' });
expect(maxQ.dequeue()).to.deep.equal({ id: 'x' });
expect(maxQ.dequeue()).to.deep.equal({ id: 'm' });
expect(maxQ.dequeue()).to.deep.equal({ id: 'k' });
expect(maxQ.dequeue()).to.deep.equal({ id: 'f' });
expect(maxQ.dequeue()).to.deep.equal({ id: 'c' });
expect(maxQ.dequeue()).to.deep.equal({ id: 'b' });
it('dequeue (pop)', () => {
expect(maxQ.pop()).to.deep.equal({ id: 'z' });
expect(maxQ.pop()).to.deep.equal({ id: 'x' });
expect(maxQ.pop()).to.deep.equal({ id: 'm' });
expect(maxQ.pop()).to.deep.equal({ id: 'k' });
expect(maxQ.pop()).to.deep.equal({ id: 'f' });
expect(maxQ.pop()).to.deep.equal({ id: 'c' });
expect(maxQ.pop()).to.deep.equal({ id: 'b' });
expect(maxQ.isEmpty()).to.equal(true);
});
});
Expand Down
20 changes: 10 additions & 10 deletions test/maxPriorityQueue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ describe('MaxPriorityQueue', () => {
];
const maxQ = new MaxPriorityQueue((value) => value.id);

it('enqueue', () => {
values.forEach((value) => maxQ.enqueue(value));
it('enqueue (push)', () => {
values.forEach((value) => maxQ.push(value));
});

it('toArray', () => {
Expand All @@ -82,14 +82,14 @@ describe('MaxPriorityQueue', () => {
expect(maxQ.isEmpty()).to.equal(false);
});

it('dequeue', () => {
expect(maxQ.dequeue()).to.deep.equal({ id: 'z' });
expect(maxQ.dequeue()).to.deep.equal({ id: 'x' });
expect(maxQ.dequeue()).to.deep.equal({ id: 'm' });
expect(maxQ.dequeue()).to.deep.equal({ id: 'k' });
expect(maxQ.dequeue()).to.deep.equal({ id: 'f' });
expect(maxQ.dequeue()).to.deep.equal({ id: 'c' });
expect(maxQ.dequeue()).to.deep.equal({ id: 'b' });
it('dequeue (pop)', () => {
expect(maxQ.pop()).to.deep.equal({ id: 'z' });
expect(maxQ.pop()).to.deep.equal({ id: 'x' });
expect(maxQ.pop()).to.deep.equal({ id: 'm' });
expect(maxQ.pop()).to.deep.equal({ id: 'k' });
expect(maxQ.pop()).to.deep.equal({ id: 'f' });
expect(maxQ.pop()).to.deep.equal({ id: 'c' });
expect(maxQ.pop()).to.deep.equal({ id: 'b' });
expect(maxQ.isEmpty()).to.equal(true);
});
});
Expand Down
20 changes: 10 additions & 10 deletions test/minPriorityQueue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ describe('MinPriorityQueue', () => {
];
const minQ = new MinPriorityQueue((value) => value.id);

it('enqueue', () => {
values.forEach((value) => minQ.enqueue(value));
it('enqueue (push)', () => {
values.forEach((value) => minQ.push(value));
});

it('toArray', () => {
Expand All @@ -78,14 +78,14 @@ describe('MinPriorityQueue', () => {
expect(minQ.isEmpty()).to.equal(false);
});

it('dequeue', () => {
expect(minQ.dequeue()).to.deep.equal({ id: 20 });
expect(minQ.dequeue()).to.deep.equal({ id: 30 });
expect(minQ.dequeue()).to.deep.equal({ id: 40 });
expect(minQ.dequeue()).to.deep.equal({ id: 50 });
expect(minQ.dequeue()).to.deep.equal({ id: 60 });
expect(minQ.dequeue()).to.deep.equal({ id: 80 });
expect(minQ.dequeue()).to.deep.equal({ id: 90 });
it('dequeue (pop)', () => {
expect(minQ.pop()).to.deep.equal({ id: 20 });
expect(minQ.pop()).to.deep.equal({ id: 30 });
expect(minQ.pop()).to.deep.equal({ id: 40 });
expect(minQ.pop()).to.deep.equal({ id: 50 });
expect(minQ.pop()).to.deep.equal({ id: 60 });
expect(minQ.pop()).to.deep.equal({ id: 80 });
expect(minQ.pop()).to.deep.equal({ id: 90 });
expect(minQ.isEmpty()).to.equal(true);
});
});
Expand Down

0 comments on commit 6dd335f

Please sign in to comment.