Created
December 19, 2013 16:30
-
-
Save lnramirez/8042097 to your computer and use it in GitHub Desktop.
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
package bajoneando | |
trait Position | |
object Left extends Position | |
object Right extends Position | |
class Node[T](parent : Node[T], value : T) { | |
var map : Map[Position,Node[T]] = Map.empty | |
def getValue = value | |
def get(position : Position) : Option[Node[T]] = { | |
map.get(position) | |
} | |
def insert(value : T, position : Position) : Unit = { | |
map = map.updated(position,new Node(this,value)) | |
} | |
} | |
object TreeTraverse extends App { | |
// import bajoneando._ | |
val root = new Node(null,0) | |
root.insert(2,Left) | |
root.insert(3,Right) | |
val nodeTwo = root.get(Left).get | |
nodeTwo.insert(4,Right) | |
val nodeFour = nodeTwo.get(Right).get | |
nodeFour.insert(3,Left) | |
def traverse(node : Node[Int]) : Unit = { | |
val childs = List(node.get(Left),node.get(Right)) | |
childs.flatMap { | |
_ match { | |
case Some(node) => List(node) | |
case None => None | |
} | |
}.foreach(traverse(_)) | |
println(node.getValue) | |
} | |
traverse(root) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment