This is an implementation for a leftist heap in Scala.
package my.collections
sealed abstract class Heap[E](implicit val ordering:Ordering[E]) {
import ordering._
def empty: Heap[E] = Heap.empty
def isEmpty: Boolean
def insert(e: E): Heap[E]
def merge(h: Heap[E]): Heap[E] = {
def makeT(e:E,a:Heap[E],b:Heap[E]):Heap[E] = if (a.rank >= b.rank) Node(e,a,b,b.rank+1) else Node(e,b,a,a.rank+1)
(this,h) match {
case (Nil(),_) => h
case (_,Nil()) => this
case (Node(x,l1,r1,_),Node(y,l2,r2,_)) => if (x < y) makeT(x,l1,r1.merge(h)) else makeT(y,l2,this.merge(r2))
}
}
def findMin: E
def deleteMin: Heap[E]
protected def rank:Int
}
object Heap {
private val emptyEl = new Nil[Nothing]
def empty[E] = emptyEl.asInstanceOf[Heap[E]]
}
private case class Node[E](e: E, left: Heap[E], right: Heap[E], rank: Int)(implicit ordering:Ordering[E]) extends Heap[E]()(ordering) {
def deleteMin = left.merge(right)
val findMin = e
def insert(e: E):Heap[E] = Node(e,empty,empty,1).merge(this)
def isEmpty = false
}
private case class Nil[E]()(implicit ordering:Ordering[E]) extends Heap[E]()(ordering) {
def deleteMin = throw new NoSuchElementException
def findMin = throw new NoSuchElementException
def insert(e: E):Heap[E] = Node[E](e,Heap.empty,Heap.empty,1)
def isEmpty = true
protected def rank = 0
}
object PG {
def main(args: Array[String]) {
val e:Heap[Int] = Heap.empty[Int]
val e1:Heap[Int] = e insert 3
val e2:Heap[Int] = e1 insert 5
val e3:Heap[Int] = e2.deleteMin
println()
}
}
This fails with the following error:
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to scala.math.Ordered
at scala.math.LowPriorityOrderingImplicits$$anon$3.compare(Ordering.scala:117)
at scala.math.Ordering$class.lt(Ordering.scala:71)
at scala.math.LowPriorityOrderingImplicits$$anon$3.lt(Ordering.scala:117)
at scala.math.Ordering$Ops.$less(Ordering.scala:100)
at my.collections.Heap.merge(Heap.scala:27)
at my.collections.Node.insert(Heap.scala:53)
at my.collections.PG$.main(Heap.scala:77)
at my.collections.PG.main(Heap.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:115)
My questions are:
- What exactly am I doing wrong, and how do I fix it?
- Is there a systematic way of understanding such errors?
Since you are getting a class cast exception, i would look at possible wrong casts in your code. i can find one cast:
and since
Eis not covariant, this is a cast error,Heap[Nothing]is not a subclass ofHeap[E]!You will have quite some job to make
Ecovariant here, so unless you need this functionality, you may just fix the cast:By the way, if
Heapwas covariant inE(e.g.Heap[+E]), you wouldn’t need to do the cast, because scalac would accept that you returnNil[Nothing]for aHeap[E]. So unless you know exactly why you useasInstanceOfand there is no way around it, it is almost certainly a mistake.