Nodejs Array Heap heapify()

Here you can find the source of heapify()

Method Source Code

Array.prototype.heapify = function() {
  //iterative solution
  this.forEach(function(num, index){
    //for each executes a provided function once per array element
    //maintain a max heap
    if (index != 0) {
      var childPos = index;
      while (childPos > 0) {
        var parentPos = this.parentPos(childPos);
        if (this[childPos] > this[parentPos]) {
          this.swap(childPos, parentPos);
          childPos = parentPos;/*from w w  w .  j  a va2 s  .co m*/
        } else {
          break;
        }
      }
    }
  }.bind(this))
}

Related

  1. heapify()
    Array.prototype.heapify = function(){
        var len = this.length,
            i = Math.floor(len/2);
        while(i >= 0){
            this.sift(i--);
        return this;
    function heapSort(arr){
    ...