Remove an Element from a Max Heap
See original GitHub issueAffected page
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:
- Created 2 years ago
- Comments:8 (8 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Great…Now I can start working on this issue.
Side note - sample solutions on forum also will need updating: https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-insert-an-element-into-a-max-heap/301703 https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-remove-an-element-from-a-max-heap/301710