Code coverage report for lib/dataStructures/binaryHeap.js

Statements: 96.23% (51 / 53)      Branches: 88.89% (16 / 18)      Functions: 90% (9 / 10)      Lines: 98.08% (51 / 52)      Ignored: none     

All files » lib/dataStructures/ » binaryHeap.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 911 34   34 10 10 10 50         1       1 151   1 149   1 149     1 118   118 118     1 191 151 151   151 73 73 73         1 133 133 133   133 133   133 266 175   175 103 103         133 133   133 69 69 69             1   14 14   14 14   14     1  
var binaryHeap = function(data) {
	this.array  = [];
 
	if (data && (data instanceof Array)) {
		this.array = data;
		var length = this.array.length;
		for (var i = Math.floor((length - 1)/2); i >= 0; i--) {
			this.bubbleDown(i, this.array[i]);
		}
	}
};
 
binaryHeap.prototype.shouldSwap = function(childData, parentData) {
	throw "This method is not implemented. Concrete implementations should implement.";
};
 
binaryHeap.prototype.getParentIndex = function (childIndex) {
	return Math.floor((childIndex - 1) / 2);
};
binaryHeap.prototype.getLeftChild = function (parentIndex) {
	return parentIndex * 2 + 1;
};
binaryHeap.prototype.getRightChild = function (parentIndex){
	return parentIndex * 2 + 2;
};
 
binaryHeap.prototype.add = function(data) {
	Iif (data === undefined) { throw "data must be valid to add"; }
 
	this.array.push(data);
	this.bubbleUp(this.array.length - 1, data);
};
 
binaryHeap.prototype.bubbleUp = function(childIndex, childData) {
	if (childIndex > 0) {
		var parentIndex = this.getParentIndex(childIndex);
		var parentData = this.array[parentIndex];
 
		if (this.shouldSwap(childData, parentData)) {
			this.array[parentIndex] = childData;
			this.array[childIndex] = parentData;
			this.bubbleUp(parentIndex, childData);
		}
	}
};
 
binaryHeap.prototype.bubbleDown = function(parentIndex, parentData) {
	Eif (parentIndex < this.array.length) {
		var targetIndex = parentIndex;
		var targetData = parentData;
 
		var leftChildIndex = this.getLeftChild(parentIndex);
		var rightChildIndex = this.getRightChild(parentIndex);
 
		var trySwap = function (index, array, shouldSwap) {
			if (index < array.length) {
				var data = array[index];
 
				if (shouldSwap(data, targetData)) {
					targetIndex = index;
					targetData = data;
				}
			}
		};
 
		trySwap(leftChildIndex, this.array, this.shouldSwap);
		trySwap(rightChildIndex, this.array, this.shouldSwap);
 
		if (targetIndex !== parentIndex) {
			this.array[parentIndex] = targetData;
			this.array[targetIndex] = parentData;
			this.bubbleDown(targetIndex, parentData);
		}
 
	}
 
};
 
binaryHeap.prototype.removeHead = function() {
 
	var headNode = this.array[0];
	var tailNode = this.array.pop();
 
	this.array[0] = tailNode;
	this.bubbleDown(0, tailNode);
 
	return headNode;
};
 
module.exports = binaryHeap;