Last active
August 29, 2015 14:06
-
-
Save natemurthy/55adf39d7abd33f5551d to your computer and use it in GitHub Desktop.
Print elements of binary search tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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