2

给定 bin 树 defn :

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

我需要得到二叉树的顺序树遍历。例如:

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

为了这棵树 shd 是:3,5,1,9,15

我试过的代码:

 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)  
 }

但它不工作。有人可以帮我吗。

完整代码:

    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)  
 }

}
4

2 回答 2

4
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))
}
于 2019-01-31T10:52:19.703 回答
2

首先,我认为按顺序遍历会导致 3,1,5,9,15 而不是 OP 的 3,5,1,9,15。而且,对不起,我无法让您的代码按原样编译。此外,正如保罗所建议的,您混淆了“t”。

这是我对实现的看法:

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))  
  }
}
于 2012-10-08T18:42:35.870 回答