question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Remove an Element from a Max Heap

See original GitHub issue

Affected page

https://www.freecodecamp.org/learn/coding-interview-prep/data-structures/remove-an-element-from-a-max-heap

Your code

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;
  }
  this.print = () => {
    return this.heap.slice(1);
  }
  // Only change code below this line

  // Only change code above this line
};



Expected behavior

insert method to add according to maxheap rules

Screenshots

System

  • Device: laptop [e.g. iPhone6, Laptop]
  • OS: Windows 10[e.g. iOS 14, Windows 10, Ubuntu 20.4]
  • Browser: chrome [e.g. chrome, safari]
  • Version: [e.g. 22]

Additional context

change line 10 index = arr[Math.floor(index / 2)]; to index = Math.floor(index / 2);

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
anshul2807commented, Aug 20, 2021

ators will guide you through thi

Great…Now I can start working on this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Insertion and Deletion in Heaps - GeeksforGeeks
The standard deletion operation on Heap is to delete the element present at the root node of the Heap. That is if it...
Read more >
Max Heap Deletion Step By Step - randerson112358 - Medium
Step 1: Delete the node that contains the value you want deleted in the heap. ... The value that we want to delete...
Read more >
How to delete in a heap data structure? - Stack Overflow
If your element is given by reference, it is however possible to remove it in O(logn) by simply 'replacing' it with the last...
Read more >
Deletion from a Max Heap in Data Structure - Tutorialspoint
Deletion from a Max Heap in Data Structure - Here we will see how to delete elements from binary max heap data structures....
Read more >
Deleting the maximum key in a heap - UCSD CSE
Deleting the maximum key in a heap · 1. The key in the root is the maximum key. Save its value to return...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found