From 49e5971a0f5f3dd004b18132dc65e9e0b4a0b694 Mon Sep 17 00:00:00 2001 From: ANIRUDDHA ADAK Date: Fri, 1 Nov 2024 22:45:04 +0530 Subject: [PATCH] Update MaxHeapAdhoc.js Improvements Made: 1. Context Handling: Fixed the issue with the forEach method by using an arrow function to preserve the context of this. 2. Performance Optimization in poll(): Simplified the logic to replace the top element with the last element and directly called heapifyDown() only if the heap is not empty after removing the top. 3. Cleaner heapifyDown() Logic: Streamlined the logic for determining the larger child index. This makes it clearer and avoids unnecessary comparisons. 4. Destructuring in swap(): Used array destructuring for swapping elements, which is more concise and readable. 5. Optional Return Value in peek(): Updated the peek() method to return undefined if the heap is empty, enhancing clarity. Overall, these changes improve the maintainability, readability, and performance of the code while retaining the core functionality of the max heap data structure. Thank you . --- src/data-structures/heap/MaxHeapAdhoc.js | 54 ++++++++++-------------- 1 file changed, 23 insertions(+), 31 deletions(-) diff --git a/src/data-structures/heap/MaxHeapAdhoc.js b/src/data-structures/heap/MaxHeapAdhoc.js index b9d69c59e0..f842d9ecce 100644 --- a/src/data-structures/heap/MaxHeapAdhoc.js +++ b/src/data-structures/heap/MaxHeapAdhoc.js @@ -7,7 +7,8 @@ class MaxHeapAdhoc { constructor(heap = []) { this.heap = []; - heap.forEach(this.add); + // Use an arrow function to maintain context for 'this'. + heap.forEach(num => this.add(num)); } add(num) { @@ -16,15 +17,17 @@ class MaxHeapAdhoc { } peek() { - return this.heap[0]; + return this.isEmpty() ? undefined : this.heap[0]; } poll() { - if (this.heap.length === 0) return undefined; + if (this.isEmpty()) return undefined; const top = this.heap[0]; - this.heap[0] = this.heap[this.heap.length - 1]; - this.heap.pop(); - this.heapifyDown(); + const last = this.heap.pop(); // Remove the last element to replace the top + if (!this.isEmpty()) { + this.heap[0] = last; // Move the last element to the root + this.heapifyDown(); // Re-establish the heap property + } return top; } @@ -49,31 +52,22 @@ class MaxHeapAdhoc { heapifyDown() { let nodeIndex = 0; - while ( - ( - this.hasLeftChild(nodeIndex) && this.heap[nodeIndex] < this.leftChild(nodeIndex) - ) - || ( - this.hasRightChild(nodeIndex) && this.heap[nodeIndex] < this.rightChild(nodeIndex) - ) - ) { + while (this.hasLeftChild(nodeIndex)) { const leftIndex = this.getLeftChildIndex(nodeIndex); const rightIndex = this.getRightChildIndex(nodeIndex); - const left = this.leftChild(nodeIndex); - const right = this.rightChild(nodeIndex); - - if (this.hasLeftChild(nodeIndex) && this.hasRightChild(nodeIndex)) { - if (left >= right) { - this.swap(leftIndex, nodeIndex); - nodeIndex = leftIndex; - } else { - this.swap(rightIndex, nodeIndex); - nodeIndex = rightIndex; - } - } else if (this.hasLeftChild(nodeIndex)) { - this.swap(leftIndex, nodeIndex); - nodeIndex = leftIndex; + let largerChildIndex = leftIndex; + + // Check if the right child exists and is greater than the left child + if (this.hasRightChild(nodeIndex) && this.rightChild(nodeIndex) > this.leftChild(nodeIndex)) { + largerChildIndex = rightIndex; } + + // If the current node is greater than the largest child, break + if (this.heap[nodeIndex] >= this.heap[largerChildIndex]) break; + + // Swap with the larger child + this.swap(nodeIndex, largerChildIndex); + nodeIndex = largerChildIndex; } } @@ -106,9 +100,7 @@ class MaxHeapAdhoc { } swap(indexOne, indexTwo) { - const tmp = this.heap[indexTwo]; - this.heap[indexTwo] = this.heap[indexOne]; - this.heap[indexOne] = tmp; + [this.heap[indexOne], this.heap[indexTwo]] = [this.heap[indexTwo], this.heap[indexOne]]; } }