Finished TileHeap conversion

main
Bryson Zimmerman 11 months ago
parent 3e55377a09
commit 6600fc0e9a

@ -1,5 +1,6 @@
package technology.zim.data
import technology.zim.World
import kotlin.math.abs
//Translated code from CS222 MaxHeap homework
@ -13,12 +14,13 @@ class TileHeap(val end: Tile) {
}
fun popMin(): Tile {
if(dat.isEmpty()) {
if(dat.isEmpty()) {
throw ArrayIndexOutOfBoundsException()
}
val ret = dat[1]
dat[1] = dat[dat.size - 1]
dat.removeLast()
siftDown(1)
return ret

@ -1,3 +1,4 @@
import technology.zim.data.Tile
import technology.zim.data.TileHeap
import kotlin.test.Test
import kotlin.test.BeforeTest
@ -6,19 +7,19 @@ import kotlin.test.BeforeTest
class TileHeapTest {
companion object {
var heap = TileHeap()
var heap = TileHeap(Tile(20, 20))
}
@BeforeTest
fun setUp() {
heap = TileHeap()
arrayOf(10, 20, 15, 17, 9, 21).forEach { item ->
heap = TileHeap(Tile(20, 20))
arrayOf(Tile(0, 0), Tile(1, 1), Tile(5, 5), Tile(4, 4), Tile(19, 19), Tile(2, 2)).forEach { item ->
heap.insert(item)
}
}
@Test
fun sortTest() {
arrayOf(9, 10, 15, 17, 20, 21 ).forEach { item ->
arrayOf(Tile(19, 19), Tile(5, 5), Tile(4, 4), Tile(2, 2), Tile(1, 1), Tile(0, 0) ).forEach { item ->
val popped = heap.popMin()
println("$item: Got $popped")

Loading…
Cancel
Save