Base class for Heap Implementations.
Extends Instance PropertiesProperty | Type | Default Value | Description |
count | Number | the current number of elements. | |
isEmpty | Boolean | true if the Heap is empty. | |
keys | Array | the keys of all items in the heap. | |
values | Array | the values contained in the heap. | |
function (){ this.__heap = []; }
Heapify the heap after the root has been removed
Argumentsthe index of the root
function (index){ throw new Error("NOT IMPLEMENTED"); }
Perform the heapify operation after the an item as been added to the bottom of the heap.
Argumentsthe index in which the new item was added
function (index){ throw new Error("NOT IMPLEMENTED"); }
Empty the heap.
Sourcefunction (){ this.__heap.length = 0; }
Determine if the heap contains a particular key.
Argumentskey to test.
Boolean
true if the key is contained in this heap.
function (key){ var heap = this.__heap; for (var i = heap.length - 1; i >= 0; i--) { if (heap[i].key === key) { return true; } } return false; }
Determine if the heap contains a particular value.
Argumentsvalue to test.
Boolean
true if the value is contained in this heap.
function (value){ var heap = this.__heap; for (var i = heap.length - 1; i >= 0; i--) { if (heap[i].value === value) { return true; } } return false; }
Insert a key value into the key
Argumentsfunction (key,value){ if (!base.isString(key)) { var l = this.__heap.push(this.__makeNode(key, value)); this.__upHeap(l - 1); } else { throw new TypeError("Invalid key"); } }
Gets he value of the root node with out removing it.
Returnsthe value of the root
function (){ var heap = this.__heap, l = heap.length, ret; if (l) { ret = heap[0]; } return ret ? ret.value : ret; }
Gets the key of the root node without removing it.
Returnsthe key of the root
function (){ var heap = this.__heap, l = heap.length, ret; if (l) { ret = heap[0]; } return ret ? ret.key : ret; }
Print the heap.
Sourcefunction (){ this.__printNode(0, 0); }
Removes the root from the heap
Returnsthe value of the root
function (){ var heap = this.__heap, l = heap.length, ret; if (l) { ret = heap[0]; if (l === 1) { heap.length = 0; } else { heap[0] = heap.pop(); this.__downHeap(0); } } return ret ? ret.value : ret; }
MIT https://github.com/C2FO/comb/raw/master/LICENSE
git clone git://github.com/C2FO/comb.git