Check in WIP on MinHeap and MazeFinderTest

main
Bryson Zimmerman 11 months ago
parent ab2421ffad
commit 7a71ea6744

@ -1,5 +1,8 @@
package technology.zim package technology.zim
import kotlin.times
import kotlin.toString
/* /*
Adjacency matrix for graph representation. 5k x 5k or 10k x 10k are safe goals, for memory Adjacency matrix for graph representation. 5k x 5k or 10k x 10k are safe goals, for memory
Working with a gigantic grid that's mostly full: better to have a up/right/down/left data in each one or a huge adjacency matrix? Working with a gigantic grid that's mostly full: better to have a up/right/down/left data in each one or a huge adjacency matrix?
@ -14,6 +17,13 @@ class HierarchicalPathfinding {
companion object { companion object {
@JvmStatic @JvmStatic
fun main(args: Array<String>) { fun main(args: Array<String>) {
buildMaze()
}
fun buildMaze() {
val n = 1000 val n = 1000
println("Building world") println("Building world")
World.setSize(n, n) World.setSize(n, n)
@ -26,7 +36,6 @@ class HierarchicalPathfinding {
println(e.message) println(e.message)
} }
val endTime = System.currentTimeMillis() val endTime = System.currentTimeMillis()
println(World.toString()) println(World.toString())
println(n*n) println(n*n)
println((endTime - startTime).toString() + "ms") println((endTime - startTime).toString() + "ms")

@ -1,60 +0,0 @@
package technology.zim.data
class Heap<T> {
private val dat = ArrayList<T>()
fun popMin(): T {
if(dat.isEmpty()) {
throw ArrayIndexOutOfBoundsException()
}
val ret = dat.first()
//TODO: Sift and such
return dat.first()
}
fun popMax(): T {
if(dat.isEmpty()) {
throw ArrayIndexOutOfBoundsException()
}
val ret = dat.last()
return ret
}
fun insert(value: T) {
dat.add(value)
//TODO: siftUp, siftDown
}
private fun siftUp() {
}
private fun siftDown() {
}
private fun delete() {
}
fun peekMax(): T {
if(dat.isEmpty()) {
throw ArrayIndexOutOfBoundsException()
}
return dat.last()
}
fun peekMin(): T {
if(dat.isEmpty()) {
throw ArrayIndexOutOfBoundsException()
}
return dat[0]
}
}

@ -0,0 +1,90 @@
package technology.zim.data
//Translated code from CS222 MaxHeap homework
//Cannot use index 0 due to Integer limitations
class MinHeap() {
val dat = ArrayList<Int>(2)
init {
dat.add(Int.MIN_VALUE)
}
fun popMin(): Int {
if(dat.isEmpty()) {
throw ArrayIndexOutOfBoundsException()
}
val ret = dat.first()
dat[1] = dat[dat.size - 1]
siftDown(1)
return ret
}
fun insert(value: Int) {
dat.add(value)
siftUp(dat.size - 1)
}
private fun siftUp(index: kotlin.Int) {
if(dat.isEmpty())
throw ArrayIndexOutOfBoundsException()
if(dat[index] < dat[parent(index)] && index > 1) {
swap(index, parent(index))
siftUp(parent(index))
}
}
private fun siftDown(index: Int) {
if(!isLeaf(index)) {
var maxIndex = index
val l = leftChild(index)
if(dat[l] < dat[index] && leftChild(index) <= dat.size) {
maxIndex = l
}
val r = rightChild(index)
if(dat[r] < dat[index] && rightChild(index) <= dat.size) {
maxIndex = r
}
if(maxIndex != index) {
swap(index, maxIndex)
siftDown(maxIndex)
}
}
}
fun peekMax(): Int {
if(dat.isEmpty()) {
throw ArrayIndexOutOfBoundsException()
}
return dat.last()
}
fun peekMin(): Int {
if(dat.isEmpty()) {
throw ArrayIndexOutOfBoundsException()
}
return dat[1]
}
//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 swap(index1: kotlin.Int, index2: kotlin.Int) {
val index1tmp = dat[index1]
dat[index1] = dat[index2]
dat[index2] = index1tmp
}
private fun isLeaf(index: Int): Boolean {
return index > (dat.size / 2)
}
}

@ -1,8 +1,9 @@
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.BeforeTest
import technology.zim.MazeFinder import technology.zim.MazeFinder
import technology.zim.World import technology.zim.World
import technology.zim.data.Tile import technology.zim.data.Tile
import kotlin.test.BeforeTest
class MazeFinderTest { class MazeFinderTest {

@ -0,0 +1,24 @@
import technology.zim.data.MinHeap
import kotlin.test.Test
import kotlin.test.BeforeTest
class MinHeapTest {
companion object {
var heap = MinHeap()
}
@BeforeTest
fun setUp() {
heap = MinHeap()
arrayOf(10, 20, 15, 17, 9, 21).forEach { item ->
heap.insert(item)
}
}
@Test
fun sortTest() {
arrayOf(9, 10, 15, 17, 20, 21 ).forEach { item ->
println(item)
assert(heap.popMin() == item)
}
}
}
Loading…
Cancel
Save