Skip to content

Instantly share code, notes, and snippets.

@honnix
Created April 19, 2012 16:18
Show Gist options
  • Save honnix/2422080 to your computer and use it in GitHub Desktop.
Save honnix/2422080 to your computer and use it in GitHub Desktop.
build and walk through the tree
case class Tree(var left: Tree, var right: Tree, var value: Int)
def build(seq: Seq[Int]): Tree = seq match {
case Nil => null
case _ =>
import scala.util.Random
val pos = Random nextInt seq.length
val (left, right) = seq splitAt pos
Tree(build(left), build(right.tail), right.head)
}
def walk(tree: Tree) {
if (tree != null) {
walk(tree.left)
println(tree.value)
walk(tree.right)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment