Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update MaxHeapAdhoc.js #1460

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 23 additions & 31 deletions src/data-structures/heap/MaxHeapAdhoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we explicitly check? !this.isEmpty() here at line no 27 as we are returning undefined if this.isEmpty() is true.
So the lines 25 to 29 will execute only if !this.isEmpty() is true. This check is redundant here.

}
return top;
}

Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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]];
}
}

Expand Down