Min-Heap in JavaScript As part of my study on fundamentals, I have implemented the following code in JavaScript for a MinHeap data structure.

Please have a look and I look forward to your feedback for any improvements.



const swap = require('../swap');

function Heap(){
  this.heap = [];
}

Heap.prototype.getParent = function(index){
  return (index-1)/2;
}

Heap.prototype.getLeft = function(index){
  return (2*index)+1;
}

Heap.prototype.getRight = function(index){
  return (2*index)+2;
}

//inserts a new value to the min-heap
Heap.prototype.insert = function(value){
  this.heap.push(value);
  this.heapifyUp(this.heap.length-1);
  return;
}

//deletes any node at given index
Heap.prototype.delete = function(index){
  if(index === 0){
    this.heap[0] = this.heap[this.heap.length-1];
    this.heap.splice(this.heap.length-1, 1);
    this.heapifyDown(0);
    return;
  }

  this.heap[index] = this.heap[this.heap.length-1];
  this.heap.splice(this.heap.length-1, 1);
  var parent = this.getParent(index);
  (this.heap[index]&lt;this.heap[parent])? this.heapifyUp(index) : this.heapifyDown(index);
  return;
}

//extracts min value and recalibrates minheap
Heap.prototype.extractMin = function(){
  var temp = this.heap[0];

  swap(this.heap, 0, (this.heap.length-1));
  this.heap.slice((this.heap.length-1), 1);
  this.heapifyDown(0);

  return temp;
}

//decreases the value of node at given index by given value
Heap.prototype.decrease = function(index, value){

  if((this.heap[index]-value)&lt;0) return false;

  this.heap[index] = this.heap[index]-value;
  (index === 0) ? this.heapifyDown(0) : this.heapifyUp(index);
  return;
}


Heap.prototype.heapifyUp = function(index){

  var parent = this.getParent(index);

  while(parent&gt;=0 &amp;&amp; this.heap[parent]&gt;this.heap[index]){
    swap(this.heap, parent, index);
    index = parent;
    parent = this.getParent(index);
  }

}

Heap.prototype.heapifyDown = function(index){

  var left = this.getLeft(index);
  var right = this.getRight(index);

  while(this.heap[index]&gt;this.heap[left] || this.heap[index]&gt;this.heap[right]){
    if(this.heap[index]&gt;this.heap[left]){
      swap(this.heap, left, index);
      index = left;
      left = this.getLeft(index);
    }
    if(this.heap[index]&gt;this.heap[right]){
      swap(this.heap, right, index);
      index = right;
      right = this.getLeft(index);
    }
  }

}
The code seems good to me. You have one bug and some unnecessary code.


First of all, it's not clear what types of values can be stored in the heap. It looks like it's supposed to be positive numbers, but the only place you check for this is in the decrease-key function. But there's actually not anything stopping callers from attempting to store strings, negative numbers, classes, etc. This will lead to bizarre behavior. I think you need to validate the input and make sure only the right type of data is going in.
It's not necessary to end your functions with return
The line


(this.heap[index]&lt;this.heap[parent])? this.heapifyUp(index) : this.heapifyDown(index);



should be written as a normal if, not a ternary.
This line


this.heap.slice((this.heap.length-1), 1);



is repeated several times; perhaps it should be brought into its own helper method with an explanatory name (suggest deleteLastElement()).
In extractMin(), this line:


swap(this.heap, 0, (this.heap.length-1));



performs unnecessary work. The last element is about to be deleted, so you don't need to put anything there. Just say

this.heap[0]=this.heap[this.heap.length-1]`

A conceptual problem is that you only allow accessing nodes by index. How is someone using your code supposed to know what the index is? An item could move around the heap a lot due to inserts and removals and heapify calls. This applies to decrease() and delete()
heapifyDown() has a bug. Consider the heap represented by [4,6,5,7] and what happens if you extractMin().
decreaseKey() has a bug, but I'm not going to find this one for you.