Base class for Heap Implementations.

Extends Instance Properties
PropertyTypeDefault ValueDescription
countNumber

the current number of elements.

isEmptyBoolean

true if the Heap is empty.

keysArray

the keys of all items in the heap.

valuesArray

the values contained in the heap.

Constructor

Defined collections/Heap.js Source
function (){
   this.__heap = [];
           
}
            

__downHeap Function Private


Defined collections/Heap.js

Heapify the heap after the root has been removed

Arguments Source
function (index){
   throw new Error("NOT IMPLEMENTED");
           
}
    

__upHeap Function Private


Defined collections/Heap.js

Perform the heapify operation after the an item as been added to the bottom of the heap.

Arguments Source
function (index){
   throw new Error("NOT IMPLEMENTED");
           
}
    

clear Function Public


Defined collections/Heap.js

Empty the heap.

Source
function (){
   this.__heap.length = 0;
           
}
    

containsKey Function Public


Defined collections/Heap.js

Determine if the heap contains a particular key.

Arguments Returns Source
function (key){
   var heap = this.__heap;
   for (var i = heap.length - 1; i >= 0; i--) {
       if (heap[i].key === key) {
           return true;
       }
   }
   return false;
           
}
    

containsValue Function Public


Defined collections/Heap.js

Determine if the heap contains a particular value.

Arguments Returns Source
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 Function Public


Defined collections/Heap.js

Insert a key value into the key

Arguments Source
function (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");
   }
           
}
    

peek Function Public


Defined collections/Heap.js

Gets he value of the root node with out removing it.

Returns Source
function (){
   var heap = this.__heap,
       l = heap.length,
       ret;
   if (l) {
       ret = heap[0];
   }
   return ret ? ret.value : ret;
           
}
    

peekKey Function Public


Defined collections/Heap.js

Gets the key of the root node without removing it.

Returns Source
function (){
   var heap = this.__heap,
       l = heap.length,
       ret;
   if (l) {
       ret = heap[0];
   }
   return ret ? ret.key : ret;
           
}
    

print Function Public


Defined collections/Heap.js

Print the heap.

Source
function (){
   this.__printNode(0, 0);
           
}
    

remove Function Public


Defined collections/Heap.js

Removes the root from the heap

Returns Source
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;
           
}
    

License

MIT https://github.com/C2FO/comb/raw/master/LICENSE

Meta