Last active
September 22, 2015 13:57
-
-
Save rbobillot/223ab0e78522629cd7e6 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
import scala.language.implicitConversions | |
case class Node(nodeVal:Any, left:Option[Node] = None, right:Option[Node] = None) | |
{ | |
def preorder(f:Node => Unit) { | |
f( this ) | |
left.map( _ preorder f ) | |
right.map( _ preorder f ) | |
} | |
} | |
implicit def nodeToSomeNode( n:Node ) = Some[Node](n) | |
val tree = ( | |
Node( "HEAD", | |
Node( " LEFT_CHILD", | |
Node( " LEFT_CHILD_LEFT"), | |
Node( " LEFT_CHILD_RIGHT", | |
Node( " LCR_L"), | |
Node( " LCR_R") | |
) | |
), | |
Node( " RIGHT_CHILD", | |
Node( " RIGHT_CHILD_LEFT"), | |
Node( " RIGHT_CHILD_RIGHT") | |
) | |
) | |
) | |
println( tree + "\n" ) | |
tree.preorder( e => println(e.nodeVal) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment