From 3e55377a090e055b11b21735de9b79616f43ea9c Mon Sep 17 00:00:00 2001 From: Bryson Zimmerman Date: Tue, 5 Nov 2024 23:32:46 -0500 Subject: [PATCH] Finished MinHeap, began conversion to TileHeap --- .../kotlin/data/{MinHeap.kt => TileHeap.kt} | 45 +++++++++++-------- .../{MinHeapTest.kt => TileHeapTest.kt} | 16 ++++--- 2 files changed, 36 insertions(+), 25 deletions(-) rename src/main/kotlin/data/{MinHeap.kt => TileHeap.kt} (62%) rename src/test/kotlin/{MinHeapTest.kt => TileHeapTest.kt} (54%) diff --git a/src/main/kotlin/data/MinHeap.kt b/src/main/kotlin/data/TileHeap.kt similarity index 62% rename from src/main/kotlin/data/MinHeap.kt rename to src/main/kotlin/data/TileHeap.kt index d4a5884..c8610b8 100644 --- a/src/main/kotlin/data/MinHeap.kt +++ b/src/main/kotlin/data/TileHeap.kt @@ -1,27 +1,30 @@ package technology.zim.data +import kotlin.math.abs + //Translated code from CS222 MaxHeap homework //Cannot use index 0 due to Integer limitations -class MinHeap() { - val dat = ArrayList(2) - +class TileHeap(val end: Tile) { + val dat = ArrayList() init { - dat.add(Int.MIN_VALUE) + //Shove some data into the zero slot + if(dat.isEmpty()) + dat.add(Tile(0, 0)) } - fun popMin(): Int { + fun popMin(): Tile { if(dat.isEmpty()) { throw ArrayIndexOutOfBoundsException() } - val ret = dat.first() + val ret = dat[1] dat[1] = dat[dat.size - 1] siftDown(1) return ret } - fun insert(value: Int) { + fun insert(value: Tile) { dat.add(value) siftUp(dat.size - 1) } @@ -30,7 +33,7 @@ class MinHeap() { if(dat.isEmpty()) throw ArrayIndexOutOfBoundsException() - if(dat[index] < dat[parent(index)] && index > 1) { + if(hValue(dat[index]) < hValue(dat[parent(index)]) && index > 1) { swap(index, parent(index)) siftUp(parent(index)) } @@ -38,26 +41,26 @@ class MinHeap() { private fun siftDown(index: Int) { if(!isLeaf(index)) { - var maxIndex = index + var minValueIndex = index val l = leftChild(index) - if(dat[l] < dat[index] && leftChild(index) <= dat.size) { - maxIndex = l + if(l < dat.size && hValue(dat[l]) < hValue(dat[minValueIndex]) ) { + minValueIndex = l } val r = rightChild(index) - if(dat[r] < dat[index] && rightChild(index) <= dat.size) { - maxIndex = r + if(r < dat.size && hValue(dat[r]) < hValue(dat[minValueIndex])) { + minValueIndex = r } - if(maxIndex != index) { - swap(index, maxIndex) - siftDown(maxIndex) + if(minValueIndex != index) { + swap(index, minValueIndex) + siftDown(minValueIndex) } } } - fun peekMax(): Int { + fun peekMax(): Tile { if(dat.isEmpty()) { throw ArrayIndexOutOfBoundsException() } @@ -65,7 +68,7 @@ class MinHeap() { return dat.last() } - fun peekMin(): Int { + fun peekMin(): Tile { if(dat.isEmpty()) { throw ArrayIndexOutOfBoundsException() } @@ -75,7 +78,7 @@ class MinHeap() { //Helper functions for navigating the heap private fun parent(index: kotlin.Int): kotlin.Int = index / 2 private fun leftChild(index: kotlin.Int): kotlin.Int = index * 2 - private fun rightChild(index: kotlin.Int): kotlin.Int = (index * 2) - 1 + private fun rightChild(index: kotlin.Int): kotlin.Int = (index * 2) + 1 private fun swap(index1: kotlin.Int, index2: kotlin.Int) { val index1tmp = dat[index1] @@ -87,4 +90,8 @@ class MinHeap() { return index > (dat.size / 2) } + private fun hValue(prospect: Tile): Int { + return abs(prospect.x() - end.x()) + abs(prospect.y() - end.y()) + } + } \ No newline at end of file diff --git a/src/test/kotlin/MinHeapTest.kt b/src/test/kotlin/TileHeapTest.kt similarity index 54% rename from src/test/kotlin/MinHeapTest.kt rename to src/test/kotlin/TileHeapTest.kt index f72d45a..43286dd 100644 --- a/src/test/kotlin/MinHeapTest.kt +++ b/src/test/kotlin/TileHeapTest.kt @@ -1,14 +1,16 @@ -import technology.zim.data.MinHeap +import technology.zim.data.TileHeap import kotlin.test.Test import kotlin.test.BeforeTest -class MinHeapTest { +//TODO: update to use Tile adjustment to heap + +class TileHeapTest { companion object { - var heap = MinHeap() + var heap = TileHeap() } @BeforeTest fun setUp() { - heap = MinHeap() + heap = TileHeap() arrayOf(10, 20, 15, 17, 9, 21).forEach { item -> heap.insert(item) } @@ -17,8 +19,10 @@ class MinHeapTest { @Test fun sortTest() { arrayOf(9, 10, 15, 17, 20, 21 ).forEach { item -> - println(item) - assert(heap.popMin() == item) + val popped = heap.popMin() + println("$item: Got $popped") + + assert(popped == item) } } } \ No newline at end of file