patternjavascriptMinor
Javascript PriorityQueue based on object property
Viewed 0 times
javascriptpriorityqueuepropertybasedobject
Problem
I wrote this class which is a priority queue based on a numeric property of any object. As far as I can tell, the following code is working as intended. Are there any stylistic tendencies that I am getting wrong, or any gotchas I should look out for?
```
//Constants
PriorityQueue.MAX_HEAP = 0;
PriorityQueue.MIN_HEAP = 1;
/**
* This is an improved Priority Queue data type implementation that can be used to sort any object type.
* It uses a technique called a binary heap.
*
* For more on binary heaps see: http://en.wikipedia.org/wiki/Binary_heap
*
* @param criteria The criteria by which to sort the objects. This should be a property of the objects you're sorting.
* @param heapType either PriorityQueue.MAX_HEAP or PriorityQueue.MIN_HEAP.
**/
function PriorityQueue(criteria,heapType) {
this.length = 0; //The current length of heap.
var queue = [];
var isMax = false;
//Constructor
if (heapType==PriorityQueue.MAX_HEAP) {
isMax = true;
} else if (heapType==PriorityQueue.MIN_HEAP) {
isMax = false;
} else {
throw heapType + " not supported.";
}
/**
* Inserts the value into the heap and sorts it.
*
* @param value The object to insert into the heap.
**/
this.insert = function(value) {
if (!value.hasOwnProperty(criteria)) {
throw "Cannot insert " + value + " because it does not have a property by the name of " + criteria + ".";
}
queue.push(value);
length++;
bubbleUp(length-1);
}
/**
* Peeks at the highest priority element.
*
* @return the highest priority element
**/
this.getHighestPriorityElement = function() {
return queue[0];
}
/**
* Removes and returns the highest priority element from the queue.
*
* @return the highest priority element
**/
this.shiftHighestPriorityElement = function() {
if (length queue[target][criteria]) {
return true;
```
//Constants
PriorityQueue.MAX_HEAP = 0;
PriorityQueue.MIN_HEAP = 1;
/**
* This is an improved Priority Queue data type implementation that can be used to sort any object type.
* It uses a technique called a binary heap.
*
* For more on binary heaps see: http://en.wikipedia.org/wiki/Binary_heap
*
* @param criteria The criteria by which to sort the objects. This should be a property of the objects you're sorting.
* @param heapType either PriorityQueue.MAX_HEAP or PriorityQueue.MIN_HEAP.
**/
function PriorityQueue(criteria,heapType) {
this.length = 0; //The current length of heap.
var queue = [];
var isMax = false;
//Constructor
if (heapType==PriorityQueue.MAX_HEAP) {
isMax = true;
} else if (heapType==PriorityQueue.MIN_HEAP) {
isMax = false;
} else {
throw heapType + " not supported.";
}
/**
* Inserts the value into the heap and sorts it.
*
* @param value The object to insert into the heap.
**/
this.insert = function(value) {
if (!value.hasOwnProperty(criteria)) {
throw "Cannot insert " + value + " because it does not have a property by the name of " + criteria + ".";
}
queue.push(value);
length++;
bubbleUp(length-1);
}
/**
* Peeks at the highest priority element.
*
* @return the highest priority element
**/
this.getHighestPriorityElement = function() {
return queue[0];
}
/**
* Removes and returns the highest priority element from the queue.
*
* @return the highest priority element
**/
this.shiftHighestPriorityElement = function() {
if (length queue[target][criteria]) {
return true;
Solution
You're off to a great start. +1 for providing comments.
Here are some tips:
1) Define variables before modifying them.
So the constants
Old Code:
New Code:
Side note: In
2) Use
More info
Example:
Old Code:
New Code:
3) Split up functions longer than 8 - 12 lines of code into smaller functions.
Use the prototype on an object to attach functions to the constructor instead of including all the functionality within.
Old Code:
New Code:
4) Sometimes it's better to just return an boolean expression.
Old Code:
New Code:
5) Make sure to create test units.
Try out qunit.js
6) Simplify
Old Code:
New Code:
7) Use
Old code:
New Code:
Or
Final code:
Here are some tips:
1) Define variables before modifying them.
So the constants
MAX_HEAP and MIN_HEAP should be defined after PriorityQueue.Old Code:
PriorityQueue.MAX_HEAP = 0;
PriorityQueue.MIN_HEAP = 1;
function PriorityQueue(criteria, heapType) {
//...
}New Code:
function PriorityQueue(criteria, heapType) {
//...
}
PriorityQueue.MAX_HEAP = 0;
PriorityQueue.MIN_HEAP = 1;Side note: In
shiftHighestPriorityElement(), _queue isn't defined. I think it should be queue.2) Use
this to attach attributes to a Constructor.More info
Example:
Old Code:
//..
function PriorityQueue(criteria, heapType) {
this.length = 0;
var queue = [];
var isMax = false;
//...New Code:
//..
function PriorityQueue(criteria, heapType) {
this.length = 0;
this.queue = [];
this.isMax = false;
//...3) Split up functions longer than 8 - 12 lines of code into smaller functions.
Use the prototype on an object to attach functions to the constructor instead of including all the functionality within.
Old Code:
function PriorityQueue(criteria, heapType) {
//...
this.insert = function (value) {
if (!value.hasOwnProperty(criteria)) {
throw "Cannot insert " + value + " because it does not have a property by the name of " + criteria + ".";
}
queue.push(value);
length++;
bubbleUp(length - 1);
}
//...
var swap = function (self, target) {
var placeHolder = queue[self];
queue[self] = queue[target];
queue[target] = placeHolder;
}
//...
}New Code:
function PriorityQueue(criteria, heapType) {
//...
}
PriorityQueue.prototype.insert = function (value) {
if (!value.hasOwnProperty(this.criteria)) {
throw "Cannot insert " + value + " because it does not have a property by the name of " + this.criteria + ".";
}
this.queue.push(value);
this.length++;
this.bubbleUp(this.length - 1);
}
PriorityQueue.prototype.swap = function (self, target) {
var placeHolder = this.queue[self];
this.queue[self] = this.queue[target];
this.queue[target] = placeHolder;
}4) Sometimes it's better to just return an boolean expression.
Old Code:
if (queue[self][criteria] < queue[target][criteria]) {
return true;
} else {
return false;
}New Code:
return (queue[self][criteria] < queue[target][criteria]);5) Make sure to create test units.
Try out qunit.js
6) Simplify
if and else conditions.Old Code:
var isMax = false;
//Constructor
if (heapType==PriorityQueue.MAX_HEAP) {
isMax = true;
} else if (heapType==PriorityQueue.MIN_HEAP) {
isMax = false;
} else {
throw heapType + " not supported.";
}New Code:
this.isMax = !!heapType;
if ( heapType !== 0 || heapType !== 1 ){
throw heapType + " not supported.";
}7) Use
=== instead of == when comparing for value and type of primitive values.Old code:
} else if (value == 0) {New Code:
} else if (value === 0) {Or
} else if (!value) {Final code:
function PriorityQueue(criteria, heapType) {
this.criteria = criteria;
this.length = 0;
this.queue = [];
this.isMax = !!heapType;
if ( heapType !== 0 || heapType !== 1 ){
throw heapType + " not supported.";
}
}
PriorityQueue.prototype.insert = function (value) {
if (!value.hasOwnProperty(this.criteria)) {
throw "Cannot insert " + value + " because it does not have a property by the name of " + this.criteria + ".";
}
this.queue.push(value);
this.length++;
this.bubbleUp(this.length - 1);
};
PriorityQueue.prototype.getHighestPriorityElement = function () {
return this.queue[0];
};
PriorityQueue.prototype.shiftHighestPriorityElement = function () {
if (length this.queue[target][this.criteria]);
} else {
return (this.queue[self][this.criteria] < this.queue[target][this.criteria]);
}
};
PriorityQueue.prototype.getParentOf = function (index) {
return Math.floor(index / 2) - 1;
};
PriorityQueue.prototype.getLeftOf = function (index) {
return index * 2 + 1;
};
PriorityQueue.prototype.getRightOf = function (index) {
return index * 2 + 2;
};
PriorityQueue.MAX_HEAP = 0;
PriorityQueue.MIN_HEAP = 1;Code Snippets
PriorityQueue.MAX_HEAP = 0;
PriorityQueue.MIN_HEAP = 1;
function PriorityQueue(criteria, heapType) {
//...
}function PriorityQueue(criteria, heapType) {
//...
}
PriorityQueue.MAX_HEAP = 0;
PriorityQueue.MIN_HEAP = 1;//..
function PriorityQueue(criteria, heapType) {
this.length = 0;
var queue = [];
var isMax = false;
//...//..
function PriorityQueue(criteria, heapType) {
this.length = 0;
this.queue = [];
this.isMax = false;
//...function PriorityQueue(criteria, heapType) {
//...
this.insert = function (value) {
if (!value.hasOwnProperty(criteria)) {
throw "Cannot insert " + value + " because it does not have a property by the name of " + criteria + ".";
}
queue.push(value);
length++;
bubbleUp(length - 1);
}
//...
var swap = function (self, target) {
var placeHolder = queue[self];
queue[self] = queue[target];
queue[target] = placeHolder;
}
//...
}Context
StackExchange Code Review Q#15853, answer score: 8
Revisions (0)
No revisions yet.