Created
          April 19, 2012 16:18 
        
      - 
      
- 
        Save honnix/2422080 to your computer and use it in GitHub Desktop. 
    build and walk through the tree
  
        
  
    
      This file contains hidden or 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 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