[Solved] binary tree creation in SCALA


You probably want something like this:

trait BinTree[+A]
case object Leaf extends BinTree[Nothing]
case class Branch[+A](node: A, left: BinTree[A], right: BinTree[A]) extends BinTree[A]

def buildTree[A](list: List[A]): BinTree[A] = list match {
 case Nil => Leaf
 case x :: xs =>
    val (left, right) = xs.splitAt(xs.length/2)

    Branch(x, buildTree(left), buildTree(right))
}

But you really need to get familiar with some of the basics of Scala before trying more complex stuff like this.

1

solved binary tree creation in SCALA