Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions Data-Structures/Heap/MaxHeap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
class MaxHeap {
constructor() {
this.heap = [];
}

insert(value) {
this.heap.push(value);
this.bubbleUp();
}

extractMax() {
if (this.isEmpty()) {
return null;
}

if (this.heap.length === 1) {
return this.heap.pop();
}

const max = this.heap[0];
this.heap[0] = this.heap.pop();
this.sinkDown(0);
return max;
}

isEmpty() {
return this.heap.length === 0;
}

peek() {
return this.isEmpty() ? null : this.heap[0];
}

bubbleUp() {
let index = this.heap.length - 1;

while (index > 0) {
const element = this.heap[index];
const parentIndex = Math.floor((index - 1) / 2);
const parent = this.heap[parentIndex];

if (element <= parent) {
break;
}

this.heap[index] = parent;
this.heap[parentIndex] = element;
index = parentIndex;
}
}

sinkDown(index) {
const leftChildIndex = 2 * index + 1;
const rightChildIndex = 2 * index + 2;
const length = this.heap.length;
let largest = index;

if (leftChildIndex < length && this.heap[leftChildIndex] > this.heap[largest]) {
largest = leftChildIndex;
}

if (rightChildIndex < length && this.heap[rightChildIndex] > this.heap[largest]) {
largest = rightChildIndex;
}

if (largest !== index) {
const temp = this.heap[index];
this.heap[index] = this.heap[largest];
this.heap[largest] = temp;
this.sinkDown(largest);
}
}
}

// Example usage:
const maxHeap = new MaxHeap();

maxHeap.insert(10);
maxHeap.insert(5);
maxHeap.insert(15);
maxHeap.insert(3);
maxHeap.insert(7);

console.log("Max Heap:");
console.log(maxHeap.heap);

console.log("Extract Max:", maxHeap.extractMax()); // Should return 15
console.log("Max Heap after extraction:");
console.log(maxHeap.heap);