Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: trekhleb/javascript-algorithms
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: aniruddhaadak80/javascript-algorithms
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: patch-4
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 1 commit
  • 1 file changed
  • 1 contributor

Commits on Nov 1, 2024

  1. 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 .
    aniruddhaadak80 authored Nov 1, 2024
    Copy the full SHA
    49e5971 View commit details
Showing with 23 additions and 31 deletions.
  1. +23 −31 src/data-structures/heap/MaxHeapAdhoc.js
54 changes: 23 additions & 31 deletions src/data-structures/heap/MaxHeapAdhoc.js
Original file line number Diff line number Diff line change
@@ -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]];
}
}