MCPcopy Index your code
hub / github.com/TheAlgorithms/JavaScript / #sinkDown

Method #sinkDown

Data-Structures/Heap/BinaryHeap.js:76–95  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

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.

Callers 1

extractTopMethod · 0.95

Calls 2

sizeMethod · 0.95
#swapMethod · 0.95

Tested by

no test coverage detected