* Sinks down a value from the specified index to maintain the heap property. * @param {number} currIdx - The index of the value to be sunk down. * @private
(currIdx)
| 74 | * @private |
| 75 | */ |
| 76 | #sinkDown(currIdx) { |
| 77 | let childOneIdx = currIdx * 2 + 1 |
| 78 | |
| 79 | while (childOneIdx < this.size()) { |
| 80 | const childTwoIdx = childOneIdx + 1 < this.size() ? childOneIdx + 1 : -1 |
| 81 | const swapIdx = |
| 82 | childTwoIdx !== -1 && |
| 83 | this.comparator(this.heap[childTwoIdx], this.heap[childOneIdx]) |
| 84 | ? childTwoIdx |
| 85 | : childOneIdx |
| 86 | |
| 87 | if (this.comparator(this.heap[swapIdx], this.heap[currIdx])) { |
| 88 | this.#swap(currIdx, swapIdx) |
| 89 | currIdx = swapIdx |
| 90 | childOneIdx = currIdx * 2 + 1 |
| 91 | } else { |
| 92 | return |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Retrieves the top element of the heap without removing it. |
no test coverage detected