Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sealed trait Tree[E]
- case class Empty[E]() extends Tree[E]
- case class Node[E](el: E, left: Tree[E], right: Tree[E]) extends Tree[E]
- def toListSimple[A](tree: Tree[A]): List[A] = tree match {
- case Node(el, left, right) => toListSimple(left) ::: (el :: toListSimple(right))
- case Empty() => Nil
- }
- def toList[A](tree: Tree[A]): List[A] = {
- def toListBuffer(tree: Tree[A], b: ListBuffer[A]): ListBuffer[A] = tree match {
- case Node(el, left, right) => {
- toListBuffer(left, b)
- b += el
- toListBuffer(right, b)
- }
- case Empty() => b
- }
- toListBuffer(tree, new ListBuffer[A]).toList
- }
Advertisement
Add Comment
Please, Sign In to add comment