compare
does not work:
case class Nodetree[T <% Ordered[T]](val element: T, var father: Nodetree[T] = null, var left: Nodetree[T] = null, var right: Nodetree[T] = null) extends Ordered[Nodetree[T]]{
override def compare(that: Nodetree[T]) = (this.element).compare(that.element)
}
As far as I understand, limiting type T, [T <% Compared[T]]
, it should accept the function compare
. However when trying to test this tree the following error occurs:
java.lang.NullPointerException:
[info] at br.unb.cic.ed.Nodetree.compare(tree.scala:28)
[info] at br.unb.cic.ed.Nodetree.compare(tree.scala:22)
[info] at scala.math.Ordered.$less(Ordered.scala:76)
[info] at scala.math.Ordered.$less$(Ordered.scala:76)
[info] at br.unb.cic.ed.Nodetree.$less(tree.scala:22)
[info] at br.unb.cic.ed.Avltree.insertAVL(avltree.scala:14)
[info] at br.unb.cic.ed.Avltree.insert(avltree.scala:11)
[info] at br.unb.cic.ed.TestAvltree.$anonfun$new$3(TestAvltree.scal\
a:19)
[info] at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mc\
V$sp.java:12)
[info] at org.scalatest.OutcomeOf.outcomeOf(OutcomeOf.scala:85)
Can anyone tell me, or send me some example implementation that is correct?