Skip to content

Commit

Permalink
stop allowing repeated elements in priority queues
Browse files Browse the repository at this point in the history
  • Loading branch information
felipernb committed Aug 3, 2014
1 parent b08ebac commit fd04111
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion data_structures/priority_queue.js
Expand Up @@ -11,7 +11,7 @@ function PriorityQueue(initialItems) {

var self = this;
MinHeap.call(this, function (a, b) {
return self._priority[a] < self._priority[b] ? -1 : 1;
return self.priority(a) < self.priority(b) ? -1 : 1;
});

this._priority = {};
Expand Down
27 changes: 27 additions & 0 deletions test/data_structures/priority_queue.js
Expand Up @@ -75,8 +75,35 @@ describe('Min Priority Queue', function () {
assert.equal(q.extract(), 'e');
assert.equal(q.extract(), 'c');
assert.equal(q.extract(), 'd');
assert(q.isEmpty());
});

it('should just update the priority when trying to insert an element that ' +
' already exists', function () {
var q = new PriorityQueue({
a: 10,
b: 2091,
c: 4,
d: 1,
e: 5
});

assert(!q.isEmpty());

q.insert('b', 0);
q.insert('a', 1);
q.insert('c', 50);
q.insert('d', 1000);
q.insert('e', 2);

assert.equal(q.extract(), 'b');
assert.equal(q.extract(), 'a');
assert.equal(q.extract(), 'e');
assert.equal(q.extract(), 'c');
assert.equal(q.extract(), 'd');
assert(q.isEmpty());
});

});


0 comments on commit fd04111

Please sign in to comment.