From cb60157dd1e7ca6914238706502b9862005ad916 Mon Sep 17 00:00:00 2001 From: norangebit Date: Mon, 20 May 2019 22:24:31 +0200 Subject: [PATCH] fix bug fix pullDown --- .../queue/priority/BinaryHeap.kt | 26 ++++----- .../queue/priority/BinaryHeapTest.kt | 58 +++++++++++++++++++ 2 files changed, 71 insertions(+), 13 deletions(-) diff --git a/src/main/kotlin/it/norangeb/algorithms/datastructures/queue/priority/BinaryHeap.kt b/src/main/kotlin/it/norangeb/algorithms/datastructures/queue/priority/BinaryHeap.kt index d453bff..8a13448 100644 --- a/src/main/kotlin/it/norangeb/algorithms/datastructures/queue/priority/BinaryHeap.kt +++ b/src/main/kotlin/it/norangeb/algorithms/datastructures/queue/priority/BinaryHeap.kt @@ -30,7 +30,7 @@ import arrow.core.Option import arrow.core.toOption class BinaryHeap private constructor( - private var array: Array, + var array: Array, private val shouldExchange: (T, T) -> Boolean ) : PriorityQueue { private var size: Int = 0 @@ -80,23 +80,23 @@ class BinaryHeap private constructor( } private fun pullDown(array: Array, k: Int) { - var i = k - while (i * WAY <= size) { - val child = i * WAY - - when { - array[child + 1] == null -> exchange(array, i, child) + var i = k * WAY + while (i <= size) { + val child = when { + array[i + 1] == null -> i shouldExchange( - array[child] ?: return, - array[child + 1] ?: return - ) -> exchange(array, i, child + 1) - else -> exchange(array, i, child) + array[i] ?: return, + array[i + 1] ?: return + ) -> i + 1 + else -> i } - i *= WAY + exchange(array, child / 2, child) + + i = 2 * child } - pushUp(array, i) + pushUp(array, i / 2) } private fun exchange(array: Array, i: Int, j: Int) { diff --git a/src/test/kotlin/it/norangeb/algorithms/datastructures/queue/priority/BinaryHeapTest.kt b/src/test/kotlin/it/norangeb/algorithms/datastructures/queue/priority/BinaryHeapTest.kt index 84cafb6..0292979 100644 --- a/src/test/kotlin/it/norangeb/algorithms/datastructures/queue/priority/BinaryHeapTest.kt +++ b/src/test/kotlin/it/norangeb/algorithms/datastructures/queue/priority/BinaryHeapTest.kt @@ -64,6 +64,64 @@ class BinaryHeapTest { array `should equal` arrayOf(Int.MAX_VALUE, 23, 7, 12, 3, 5) } + @Test + fun testMinQueue() { + val heap = BinaryHeap.createMinPriorityQueue() + + 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 fun testComparableMax() { val heap = BinaryHeap.createMaxPriorityQueue()