Let's take a look at the code representation for the previous diagram. First of all, we have to define the Node interface through a trait:
trait Node { def print(prefix: String): Unit}
After we have the interface, we can now define the implementation:
class Leaf(data: String) extends Node { override def print(prefix: String): Unit = System.out.println(s"${prefix}${data}")}class Tree extends Node { private val children = ListBuffer.empty[Node] override def print(prefix: String): Unit = { System.out.println(s"${prefix}(") children.foreach(_.print(s"${prefix}${prefix}")) System.out.println(s"${prefix})") } def add(child: Node): ...