Skip to content
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
59 changes: 30 additions & 29 deletions Data-Structures/Heap/BinaryHeap.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,38 @@ class BinaryHeap {
* The heap array that stores elements.
* @member {Array}
*/
this.heap = []
this.heap = [];

/**
* The comparator function used for ordering elements in the heap.
* @member {Function}
*/
this.comparator = comparatorFunction
this.comparator = comparatorFunction;
}

/**
* Inserts a new value into the heap.
* @param {*} value - The value to be inserted into the heap.
*/
insert(value) {
this.heap.push(value)
this.#bubbleUp(this.heap.length - 1)
this.heap.push(value);
this.#bubbleUp(this.heap.length - 1);
}

/**
* Returns the number of elements in the heap.
* @returns {number} - The number of elements in the heap.
*/
size() {
return this.heap.length
return this.heap.length;
}

/**
* Checks if the heap is empty.
* @returns {boolean} - True if the heap is empty, false otherwise.
*/
empty() {
return this.size() === 0
return this.size() === 0;
}

/**
Expand All @@ -56,15 +56,15 @@ class BinaryHeap {
* @private
*/
#bubbleUp(currIdx) {
let parentIdx = Math.floor((currIdx - 1) / 2)
let parentIdx = Math.floor((currIdx - 1) / 2);

while (
currIdx > 0 &&
this.comparator(this.heap[currIdx], this.heap[parentIdx])
) {
this.#swap(currIdx, parentIdx)
currIdx = parentIdx
parentIdx = Math.floor((currIdx - 1) / 2)
this.#swap(currIdx, parentIdx);
currIdx = parentIdx;
parentIdx = Math.floor((currIdx - 1) / 2);
}
}

Expand All @@ -74,22 +74,23 @@ class BinaryHeap {
* @private
*/
#sinkDown(currIdx) {
let childOneIdx = currIdx * 2 + 1
let childOneIdx = currIdx * 2 + 1;

while (childOneIdx < this.size()) {
const childTwoIdx = childOneIdx + 1 < this.size() ? childOneIdx + 1 : -1
const childTwoIdx =
childOneIdx + 1 < this.size() ? childOneIdx + 1 : -1;
const swapIdx =
childTwoIdx !== -1 &&
this.comparator(this.heap[childTwoIdx], this.heap[childOneIdx])
? childTwoIdx
: childOneIdx
: childOneIdx;

if (this.comparator(this.heap[swapIdx], this.heap[currIdx])) {
this.#swap(currIdx, swapIdx)
currIdx = swapIdx
childOneIdx = currIdx * 2 + 1
this.#swap(currIdx, swapIdx);
currIdx = swapIdx;
childOneIdx = currIdx * 2 + 1;
} else {
return
return;
}
}
}
Expand All @@ -99,23 +100,23 @@ class BinaryHeap {
* @returns {*} - The top element of the heap.
*/
peek() {
return this.heap[0]
return this.heap[0];
}

/**
* Removes and returns the top element of the heap.
* @returns {*} - The top element of the heap.
*/
extractTop() {
const top = this.peek()
const last = this.heap.pop()
const top = this.peek();
const last = this.heap.pop();

if (!this.empty()) {
this.heap[0] = last
this.#sinkDown(0)
this.heap[0] = last;
this.#sinkDown(0);
}

return top
return top;
}

/**
Expand All @@ -125,10 +126,10 @@ class BinaryHeap {
* @private
*/
#swap(index1, index2) {
;[this.heap[index1], this.heap[index2]] = [
[this.heap[index1], this.heap[index2]] = [
this.heap[index2],
this.heap[index1]
]
this.heap[index1],
];
}
}

Expand All @@ -138,14 +139,14 @@ class BinaryHeap {
* @param {*} b - The second element to compare.
* @returns {boolean} - True if 'a' should have higher priority than 'b' in the Min Heap, false otherwise.
*/
const minHeapComparator = (a, b) => a < b
const minHeapComparator = (a, b) => a < b;

/**
* Comparator function for creating a Max Heap.
* @param {*} a - The first element to compare.
* @param {*} b - The second element to compare.
* @returns {boolean} - True if 'a' should have higher priority than 'b' in the Max Heap, false otherwise.
*/
const maxHeapComparator = (a, b) => a > b
const maxHeapComparator = (a, b) => a > b;

export { BinaryHeap, minHeapComparator, maxHeapComparator }
export { BinaryHeap, minHeapComparator, maxHeapComparator };