Task

Implement a binary tree where each node carries an integer,   and implement:

   pre-order,
   in-order, 
   post-order,     and 
   level-order   traversal. 


Use those traversals to output the following tree:

         1
        / \
       /   \
      /     \
     2       3
    / \     /
   4   5   6
  /       / \
 7       8   9

The correct output should look like this:

preorder:    1 2 4 7 5 3 6 8 9
inorder:     7 4 2 5 1 8 6 9 3
postorder:   7 4 5 2 8 9 6 3 1
level-order: 1 2 3 4 5 6 7 8 9



See also

   Wikipedia article:   Tree traversal.


case class IntNode(value: Int, left: Option[IntNode] = None, right: Option[IntNode] = None) {
 
  def preorder(f: IntNode => Unit) {
    f(this)
    left.map(_.preorder(f)) // Same as: if(left.isDefined) left.get.preorder(f)
    right.map(_.preorder(f))
  }
 
  def postorder(f: IntNode => Unit) {
    left.map(_.postorder(f))
    right.map(_.postorder(f))
    f(this)
  }
 
  def inorder(f: IntNode => Unit) {
    left.map(_.inorder(f))
    f(this)
    right.map(_.inorder(f))
  }
 
  def levelorder(f: IntNode => Unit) {
 
    def loVisit(ls: List[IntNode]): Unit = ls match {
      case Nil => None
      case node :: rest => f(node); loVisit(rest ++ node.left ++ node.right)
    }
 
    loVisit(List(this))
  }
}
 
object TreeTraversal extends App {
  implicit def intNode2SomeIntNode(n: IntNode) = Some[IntNode](n)
 
  val tree = IntNode(1,
    IntNode(2,
      IntNode(4,
        IntNode(7)),
      IntNode(5)),
    IntNode(3,
      IntNode(6,
        IntNode(8),
        IntNode(9))))
 
  List(
    "  preorder: " -> tree.preorder _, // `_` denotes the function value of type `IntNode => Unit` (returning nothing)
    "   inorder: " -> tree.inorder _,
    " postorder: " -> tree.postorder _,
    "levelorder: " -> tree.levelorder _) foreach {
      case (name, func) =>
        val s = new StringBuilder(name)
        func(n => s ++= n.value.toString + " ")
        println(s)
    }
}