Skip to content

Instantly share code, notes, and snippets.

@natemurthy
Last active August 29, 2015 14:06
Show Gist options
  • Save natemurthy/55adf39d7abd33f5551d to your computer and use it in GitHub Desktop.
Save natemurthy/55adf39d7abd33f5551d to your computer and use it in GitHub Desktop.
Print elements of binary search tree
case class BSTNode(payload: Int, left: Option[BSTNode] = None, right: Option[BSTNode] = None)
object Treeutils {
def inOrder(root: Option[BSTNode]): List[Int] = root match {
case None => List()
case Some(BSTNode(payload, left, right)) => inOrder(left) ::: List(payload) ::: inOrder(right)
}
}
scala> val tree = BSTNode(10,Some(BSTNode(8,Some(BSTNode(7,Some(BSTNode(6,None,None)),Some(BSTNode(7,None,None)))),Some(BSTNode(9,None,None)))),Some(BSTNode(11,None,None)))
scala> Treeutils.inOrder(Option(tree))
res0: List[Int] = List(6, 7, 7, 8, 9, 10, 11)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment