javascript-algorithms
javascript-algorithms copied to clipboard
Update MaxHeapAdhoc.js
Improvements Made:
-
Context Handling: Fixed the issue with the
forEachmethod by using an arrow function to preserve the context ofthis. -
Performance Optimization in
poll(): Simplified the logic to replace the top element with the last element and directly calledheapifyDown()only if the heap is not empty after removing the top. -
Cleaner
heapifyDown()Logic: Streamlined the logic for determining the larger child index. This makes it clearer and avoids unnecessary comparisons. -
Destructuring in
swap(): Used array destructuring for swapping elements, which is more concise and readable. -
Optional Return Value in
peek(): Updated thepeek()method to returnundefinedif the heap is empty, enhancing clarity.