Created
September 30, 2014 19:56
-
-
Save MasseGuillaume/b23a744a9aa2b77fe1b6 to your computer and use it in GitHub Desktop.
node ordering
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed trait Node[T] | |
case class Leaf[T](v: T) extends Node[T] | |
case class Branch[T](l: Node[T], r: Node[T]) extends Node[T] | |
implicit def nodeOrdering[W, C](implicit num: Numeric[W]): Ordering[Node[(W, C)]] = { | |
def weight(a: Node[(W, C)]): Tuple2[W, C] = a match { | |
case Leaf((w, c)) => (w, c) | |
case Branch(l, r) => { | |
val (wl, cl) = weight(l) | |
val (wr, _) = weight(r) | |
(num.plus(wl, wr), cl) | |
} | |
} | |
Ordering.by(weight) | |
// No implicit Ordering defined for (W, C). | |
// not enough arguments for method by: (implicit ord: scala.math.Ordering[(W, C)])scala.math.Ordering[Node[(W, C)]]. Unspecified value parameter ord. | |
} |
Author
MasseGuillaume
commented
Sep 30, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment