Finished MinHeap, began conversion to TileHeap

main
Bryson Zimmerman 11 months ago
parent db18ad8039
commit 3e55377a09

@ -1,27 +1,30 @@
package technology.zim.data package technology.zim.data
import kotlin.math.abs
//Translated code from CS222 MaxHeap homework //Translated code from CS222 MaxHeap homework
//Cannot use index 0 due to Integer limitations //Cannot use index 0 due to Integer limitations
class MinHeap() { class TileHeap(val end: Tile) {
val dat = ArrayList<Int>(2) val dat = ArrayList<Tile>()
init { 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()) { if(dat.isEmpty()) {
throw ArrayIndexOutOfBoundsException() throw ArrayIndexOutOfBoundsException()
} }
val ret = dat.first() val ret = dat[1]
dat[1] = dat[dat.size - 1] dat[1] = dat[dat.size - 1]
siftDown(1) siftDown(1)
return ret return ret
} }
fun insert(value: Int) { fun insert(value: Tile) {
dat.add(value) dat.add(value)
siftUp(dat.size - 1) siftUp(dat.size - 1)
} }
@ -30,7 +33,7 @@ class MinHeap() {
if(dat.isEmpty()) if(dat.isEmpty())
throw ArrayIndexOutOfBoundsException() throw ArrayIndexOutOfBoundsException()
if(dat[index] < dat[parent(index)] && index > 1) { if(hValue(dat[index]) < hValue(dat[parent(index)]) && index > 1) {
swap(index, parent(index)) swap(index, parent(index))
siftUp(parent(index)) siftUp(parent(index))
} }
@ -38,26 +41,26 @@ class MinHeap() {
private fun siftDown(index: Int) { private fun siftDown(index: Int) {
if(!isLeaf(index)) { if(!isLeaf(index)) {
var maxIndex = index var minValueIndex = index
val l = leftChild(index) val l = leftChild(index)
if(dat[l] < dat[index] && leftChild(index) <= dat.size) { if(l < dat.size && hValue(dat[l]) < hValue(dat[minValueIndex]) ) {
maxIndex = l minValueIndex = l
} }
val r = rightChild(index) val r = rightChild(index)
if(dat[r] < dat[index] && rightChild(index) <= dat.size) { if(r < dat.size && hValue(dat[r]) < hValue(dat[minValueIndex])) {
maxIndex = r minValueIndex = r
} }
if(maxIndex != index) { if(minValueIndex != index) {
swap(index, maxIndex) swap(index, minValueIndex)
siftDown(maxIndex) siftDown(minValueIndex)
} }
} }
} }
fun peekMax(): Int { fun peekMax(): Tile {
if(dat.isEmpty()) { if(dat.isEmpty()) {
throw ArrayIndexOutOfBoundsException() throw ArrayIndexOutOfBoundsException()
} }
@ -65,7 +68,7 @@ class MinHeap() {
return dat.last() return dat.last()
} }
fun peekMin(): Int { fun peekMin(): Tile {
if(dat.isEmpty()) { if(dat.isEmpty()) {
throw ArrayIndexOutOfBoundsException() throw ArrayIndexOutOfBoundsException()
} }
@ -75,7 +78,7 @@ class MinHeap() {
//Helper functions for navigating the heap //Helper functions for navigating the heap
private fun parent(index: kotlin.Int): kotlin.Int = index / 2 private fun parent(index: kotlin.Int): kotlin.Int = index / 2
private fun leftChild(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) { private fun swap(index1: kotlin.Int, index2: kotlin.Int) {
val index1tmp = dat[index1] val index1tmp = dat[index1]
@ -87,4 +90,8 @@ class MinHeap() {
return index > (dat.size / 2) return index > (dat.size / 2)
} }
private fun hValue(prospect: Tile): Int {
return abs(prospect.x() - end.x()) + abs(prospect.y() - end.y())
}
} }

@ -1,14 +1,16 @@
import technology.zim.data.MinHeap import technology.zim.data.TileHeap
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.BeforeTest import kotlin.test.BeforeTest
class MinHeapTest { //TODO: update to use Tile adjustment to heap
class TileHeapTest {
companion object { companion object {
var heap = MinHeap() var heap = TileHeap()
} }
@BeforeTest @BeforeTest
fun setUp() { fun setUp() {
heap = MinHeap() heap = TileHeap()
arrayOf(10, 20, 15, 17, 9, 21).forEach { item -> arrayOf(10, 20, 15, 17, 9, 21).forEach { item ->
heap.insert(item) heap.insert(item)
} }
@ -17,8 +19,10 @@ class MinHeapTest {
@Test @Test
fun sortTest() { fun sortTest() {
arrayOf(9, 10, 15, 17, 20, 21 ).forEach { item -> arrayOf(9, 10, 15, 17, 20, 21 ).forEach { item ->
println(item) val popped = heap.popMin()
assert(heap.popMin() == item) println("$item: Got $popped")
assert(popped == item)
} }
} }
} }
Loading…
Cancel
Save