Finished TileHeap conversion

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

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

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

Loading…
Cancel
Save