MCPcopy Index your code
hub / github.com/TheAlgorithms/JavaScript / sink

Method sink

Data-Structures/Heap/MinPriorityQueue.js:80–108  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

78
79 // this function reorders the heap after every delete function
80 sink() {
81 let k = 1
82 while (2 * k <= this.size || 2 * k + 1 <= this.size) {
83 let minIndex
84 if (this.heap[2 * k] >= this.heap[k]) {
85 if (2 * k + 1 <= this.size && this.heap[2 * k + 1] >= this.heap[k]) {
86 break
87 } else if (2 * k + 1 > this.size) {
88 break
89 }
90 }
91 if (2 * k + 1 > this.size) {
92 minIndex = this.heap[2 * k] < this.heap[k] ? 2 * k : k
93 } else {
94 if (
95 this.heap[k] > this.heap[2 * k] ||
96 this.heap[k] > this.heap[2 * k + 1]
97 ) {
98 minIndex = this.heap[2 * k] < this.heap[2 * k + 1] ? 2 * k : 2 * k + 1
99 } else {
100 minIndex = k
101 }
102 }
103 const temp = this.heap[k]
104 this.heap[k] = this.heap[minIndex]
105 this.heap[minIndex] = temp
106 k = minIndex
107 }
108 }
109
110 // deletes the highest priority value from the heap. The last
111 // element goes to ahead to first position and reorder heap

Callers 2

heapReverseMethod · 0.95
deleteMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected