fix pullDown
This commit is contained in:
Raffaele Mignone 2019-05-20 22:24:31 +02:00
parent 872867559f
commit cb60157dd1
Signed by: norangebit
GPG Key ID: F5255658CB220573
2 changed files with 71 additions and 13 deletions

View File

@ -30,7 +30,7 @@ import arrow.core.Option
import arrow.core.toOption import arrow.core.toOption
class BinaryHeap<T> private constructor( class BinaryHeap<T> private constructor(
private var array: Array<T?>, var array: Array<T?>,
private val shouldExchange: (T, T) -> Boolean private val shouldExchange: (T, T) -> Boolean
) : PriorityQueue<T> { ) : PriorityQueue<T> {
private var size: Int = 0 private var size: Int = 0
@ -80,23 +80,23 @@ class BinaryHeap<T> private constructor(
} }
private fun pullDown(array: Array<T?>, k: Int) { private fun pullDown(array: Array<T?>, k: Int) {
var i = k var i = k * WAY
while (i * WAY <= size) { while (i <= size) {
val child = i * WAY val child = when {
array[i + 1] == null -> i
when {
array[child + 1] == null -> exchange(array, i, child)
shouldExchange( shouldExchange(
array[child] ?: return, array[i] ?: return,
array[child + 1] ?: return array[i + 1] ?: return
) -> exchange(array, i, child + 1) ) -> i + 1
else -> exchange(array, i, child) else -> i
} }
i *= WAY exchange(array, child / 2, child)
i = 2 * child
} }
pushUp(array, i) pushUp(array, i / 2)
} }
private fun exchange(array: Array<T?>, i: Int, j: Int) { private fun exchange(array: Array<T?>, i: Int, j: Int) {

View File

@ -64,6 +64,64 @@ class BinaryHeapTest {
array `should equal` arrayOf(Int.MAX_VALUE, 23, 7, 12, 3, 5) array `should equal` arrayOf(Int.MAX_VALUE, 23, 7, 12, 3, 5)
} }
@Test
fun testMinQueue() {
val heap = BinaryHeap.createMinPriorityQueue<Double>()
heap.insert(0.37)
heap.peek().map { it `should be equal to` 0.37 }
heap.insert(0.19)
heap.peek().map { it `should be equal to` 0.19 }
heap.insert(0.34)
heap.peek().map { it `should be equal to` 0.19 }
heap.insert(0.26)
heap.peek().map { it `should be equal to` 0.19 }
heap.insert(0.16)
heap.peek().map { it `should be equal to` 0.16 }
heap.insert(0.40)
heap.peek().map { it `should be equal to` 0.16 }
heap.insert(0.28)
heap.peek().map { it `should be equal to` 0.16 }
heap.insert(0.38)
heap.insert(0.17)
heap.insert(0.29)
heap.insert(0.32)
heap.insert(0.35)
heap.insert(0.36)
heap.insert(0.52)
heap.peek().map { it `should be equal to` 0.16 }
heap.pop().map { it `should be equal to` 0.16 }
heap.pop().map { it `should be equal to` 0.17 }
heap.pop().map { it `should be equal to` 0.19 }
heap.pop().map { it `should be equal to` 0.26 }
heap.pop().map { it `should be equal to` 0.28 }
heap.pop().map { it `should be equal to` 0.29 }
heap.pop().map { it `should be equal to` 0.32 }
heap.pop().map { it `should be equal to` 0.34 }
heap.pop().map { it `should be equal to` 0.35 }
heap.pop().map { it `should be equal to` 0.36 }
heap.pop().map { it `should be equal to` 0.37 }
heap.pop().map { it `should be equal to` 0.38 }
heap.pop().map { it `should be equal to` 0.40 }
heap.pop().map { it `should be equal to` 0.52 }
heap.pop() `should equal` None
}
@Test @Test
fun testComparableMax() { fun testComparableMax() {
val heap = BinaryHeap.createMaxPriorityQueue<Int>() val heap = BinaryHeap.createMaxPriorityQueue<Int>()