Error in the solution for the challenge remove an element from a max heap

link: https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-remove-an-element-from-a-max-heap/301710.

The error is in the insert method of the solution provided.

var MaxHeap = function() {
  this.heap = [null];
  this.insert = (ele) => {
    var index = this.heap.length;
    var arr = [...this.heap];
    arr.push(ele);
    while (ele > arr[Math.floor(index / 2)] && index > 1) {
      arr[index] = arr[Math.floor(index / 2)];
      arr[Math.floor(index / 2)] = ele;
      index = arr[Math.floor(index / 2)];  // (*)
    }
    this.heap = arr;
  }
//....rest of the code

the new index should be the index of the parent element so line (*) should be :

index = Math.floor(index / 2);

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.