Skip to content

Instantly share code, notes, and snippets.

@adilakhter
Created May 10, 2013 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adilakhter/5555987 to your computer and use it in GitHub Desktop.
Save adilakhter/5555987 to your computer and use it in GitHub Desktop.
toTree
def toTree(ls: List[Int]): IntSet = {
def toTreeAux(ls: List[Int], n: Int): (List[Int], IntSet) = {
if (n <= 0)
(ls, Empty)
else {
val (lls, lt) = toTreeAux(ls, n / 2) // construct left sub-tree
val x :: xs = lls // extract root node
val (xr, rt) = toTreeAux(xs, n - n / 2 - 1) // construct right sub-tree
(xr, IntSet(x, lt, rt)) // construct tree
}
}
val (ls_1, tree) = toTreeAux(ls, List.length(ls))
tree
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment