<script id="jsbin-javascript">
function BinaryHeap(scoreFunciton) {
push : function (element) {
//if non js code do array resizing if array is full
this._content.push(element);
this.swim(this._content.length - 1);
return this._content.length;
if ( typeof this._content === 'undefined' ||
this._content.length <= 0) throw new Error('the heap does not exist or is empty');
var result = this._content[0];
// if length is 1 then pop and that is it
this.swap(0, this._content.length - 1);
if (this._content.length > 0) {
this.sink(this._content[0]);
compare: function (n1,n2) {
var a = this._content[n1];
var b = this._content[n2];
if (typeof a !== 'number') a = Number(a);
if (typeof b !== 'number') b = Number(b);
else if ( a == b) return 0;
swap : function (n1, n2) {
var temp = this._content[n1];
this._content[n1] = this._content[n2];
this._content[n2] = temp;
// should do type checking
while ( n >= 0 && this.compare(n, Math.floor(n/2)) >= 0) {
this.swap(n, Math.floor(n/2));
while ( 2 * n <= this._content.length - 1) {
if ( childIndex < this._content.length && this.compare(childIndex, childIndex + 1) < 0)
this.swap(n, childIndex);
<script id="jsbin-source-javascript" type="text/javascript">function BinaryHeap(scoreFunciton) {
push : function (element) {
//if non js code do array resizing if array is full
this._content.push(element);
this.swim(this._content.length - 1);
return this._content.length;
if ( typeof this._content === 'undefined' ||
this._content.length <= 0) throw new Error('the heap does not exist or is empty');
var result = this._content[0];
// if length is 1 then pop and that is it
this.swap(0, this._content.length - 1);
if (this._content.length > 0) {
this.sink(this._content[0]);
compare: function (n1,n2) {
var a = this._content[n1];
var b = this._content[n2];
if (typeof a !== 'number') a = Number(a);
if (typeof b !== 'number') b = Number(b);
else if ( a == b) return 0;
swap : function (n1, n2) {
var temp = this._content[n1];
this._content[n1] = this._content[n2];
this._content[n2] = temp;
// should do type checking
while ( n >= 0 && this.compare(n, Math.floor(n/2)) >= 0) {
this.swap(n, Math.floor(n/2));
while ( 2 * n <= this._content.length - 1) {
if ( childIndex < this._content.length && this.compare(childIndex, childIndex + 1) < 0)
this.swap(n, childIndex);