MCPcopy
hub / github.com/trekhleb/javascript-algorithms / heapifyUp

Method heapifyUp

src/data-structures/heap/Heap.js:223–236  ·  view source on GitHub ↗

* @param {number} [customStartIndex]

(customStartIndex)

Source from the content-addressed store, hash-verified

221 * @param {number} [customStartIndex]
222 */
223 heapifyUp(customStartIndex) {
224 // Take the last element (last in array or the bottom left in a tree)
225 // in the heap container and lift it up until it is in the correct
226 // order with respect to its parent element.
227 let currentIndex = customStartIndex || this.heapContainer.length - 1;
228
229 while (
230 this.hasParent(currentIndex)
231 && !this.pairIsInCorrectOrder(this.parent(currentIndex), this.heapContainer[currentIndex])
232 ) {
233 this.swap(currentIndex, this.getParentIndex(currentIndex));
234 currentIndex = this.getParentIndex(currentIndex);
235 }
236 }
237
238 /**
239 * @param {number} [customStartIndex]

Callers 2

addMethod · 0.95
removeMethod · 0.95

Calls 5

hasParentMethod · 0.95
pairIsInCorrectOrderMethod · 0.95
parentMethod · 0.95
swapMethod · 0.95
getParentIndexMethod · 0.95

Tested by

no test coverage detected