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 eebe71c..d453bff 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 @@ -57,16 +57,8 @@ class BinaryHeap private constructor( return result.toOption() } - private fun print() { - print("|") - array.forEach { - print(" $it |") - } - println() - } - override fun peek(): Option = if (size >= 1) - array[1].toOption() + array[FIRST_ELEMENT].toOption() else None @@ -84,7 +76,6 @@ class BinaryHeap private constructor( exchange(array, k / WAY, k) pushUp(array, k / WAY) } - else -> return } } @@ -109,9 +100,8 @@ class BinaryHeap private constructor( } private fun exchange(array: Array, i: Int, j: Int) { - array[i] = array[j].also { - array[j] = array[i] - } + array[i] = array[j] + .also { array[j] = array[i] } } private fun isFull(): Boolean = size + 1 == array.size diff --git a/src/main/kotlin/it/norangeb/algorithms/exercises/OrderedListSorter.kt b/src/main/kotlin/it/norangeb/algorithms/exercises/OrderedListSorter.kt index aab7296..2d4193e 100644 --- a/src/main/kotlin/it/norangeb/algorithms/exercises/OrderedListSorter.kt +++ b/src/main/kotlin/it/norangeb/algorithms/exercises/OrderedListSorter.kt @@ -60,8 +60,8 @@ object OrderedListSorter { fun > sort(vararg lists: List): List { val heap = BinaryHeap.createMinPriorityQueue, T> { it.value } - lists.forEachIndexed { index, list -> - heap.insert(Node(list.first(), index, 0)) + lists.forEachIndexed { k, list -> + heap.insert(Node(list.first(), k, 0)) } val outList = ArrayList()