Created
May 10, 2013 17:29
-
-
Save adilakhter/5555987 to your computer and use it in GitHub Desktop.
toTree
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
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