parent
ab2421ffad
commit
7a71ea6744
@ -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)
|
||||
}
|
||||
|
||||
}
|
@ -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…
Reference in new issue