Simpson
Simpson

Reputation: 292

Binary tree traversal in scala

given bin tree defn :

// a binary tree node
 case class Node( var data:(Int), 
     left:Option[Node],
     right:Option[Node] 
       )

I need to get the in order tree traversal of the binary tree. for eg:

val testtree = Node( (3),
                     None,
                     Some(Node( (5),
                                Some(Node( (1),
                                     None,
                                     None )),
                             Some(Node( (9),
                                      None,
                                      Some(Node( (15), 
                                                 None,
                                                 None )) ))  ))  )

inorder for this tree shd be : 3,5,1,9,15

Code which I tried:

 def inOrder(t: Node): Unit ={  
   def print(data:Int,t:Option[Node]):Unit={
      if(t!=None)
                    {
                        print(data,t.left)
            Console.print(data)
            print(data,t.right)
        }      
   }
   print(t.data,t)  
 }

But its not working out. Can someone help me out.

Full code :

    case class Node( var data:(Int), 
         left:Option[Node],
         right:Option[Node] 
           )

object Ch15 {

  def main( args:Array[String] ) = {
   val tree =Node( (3), None,Some(Node( (5), Some(Node( (1), None, None )), Some(Node( (9), None,Some(Node( (15), None, None )) )) )) )    
   inOrder( tree ) 
  }

  def inOrder(t: Node): Unit ={  
   def print(data:Int,t:Option[Node]):Unit={
      if(t!=None)
      {
            print(data,t.left)
            Console.print(data)
            print(data,t.right)
        }      
   }
   print(t.data,t)  
 }

}

Upvotes: 2

Views: 7111

Answers (2)

Jet
Jet

Reputation: 3288

case class Branch(node: Int, left: Option[Branch] = None, right: Option[Branch] = None)

object TreeTraverse extends App {
  val branch = Branch(1, Some(Branch(2, Some(Branch(4)), Some(Branch(5)))), Some(Branch(3, Some(Branch(6)), Some(Branch(7)))))

  def preOrder(branch: Branch): Unit = {
    print(branch.node)
    if (branch.left.isDefined) preOrder(branch.left.get)
    if (branch.right.isDefined) preOrder(branch.right.get)
  }

  def inOrder(branch: Branch): Unit = {
    if (branch.left.isDefined) inOrder(branch.left.get)
    print(branch.node)
    if (branch.right.isDefined) inOrder(branch.right.get)
  }

  def postOrder(branch: Branch): Unit = {
    if (branch.left.isDefined) postOrder(branch.left.get)
    if (branch.right.isDefined) postOrder(branch.right.get)
    print(branch.node)
  }

  println("  -> PreOrder" + preOrder(branch))
  println("  -> InOrder " + inOrder(branch))
  println("  -> PostOrder " + postOrder(branch))
}

Upvotes: 4

vijay
vijay

Reputation: 21

First of all, I think the in-order traversal would result in 3,1,5,9,15 instead of OP's 3,5,1,9,15. And, sorry I could not get your code to compile as is. Also, as Paul suggested, you are confusing the "t"s.

Here is my take on the implementation:

object Ch15 {
  def main( args:Array[String] ) = {
   val tree =Node( (3), None,Some(Node( (5), Some(Node( (1), None, None )), Some(Node( (9), None,Some(Node( (15), None, None )) )) )) )    
   inOrder( tree ) 
  }

  def inOrder(t: Node): Unit ={  
    def printNode(node:Option[Node]):Unit={
      node match {
        case Some(aliveNode) => {
          printNode(aliveNode.left)
          Console.print(aliveNode.data + ", ")
          printNode(aliveNode.right)
        }
        case None => {}
      }
    }
    printNode(Option(t))  
  }
}

Upvotes: 2

Related Questions