Skip to content

Instantly share code, notes, and snippets.

@rbobillot
Last active September 22, 2015 13:57
Show Gist options
  • Save rbobillot/223ab0e78522629cd7e6 to your computer and use it in GitHub Desktop.
Save rbobillot/223ab0e78522629cd7e6 to your computer and use it in GitHub Desktop.
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