intellij idea - Scala Binary tree, setting next "node" when navigating -


i have binary tree. ask user question, if says yes, give him left branch, otherwise give him right branch. (method: get_answer2)

but when this:

var current_node = node("test") current_node.set_left = node("end node") current_node = current_node.get_answer2("y") 

i

expression of type tree[nothing] not conform to expected type tree[nothing] (from intellij)

sealed abstract class tree[+t] { } case object end extends tree[nothing] {   var left = "blah"   var right = "blah"    override def tostring = "." }  case class node[t](var question: string,  var left: tree[t] = end, var right: tree[t] = end) extends tree[t] {    def get_answer2(answer: string) ={     if (answer == "y"){       right     }else{       left     }   }    def change_left_tree( new_tree : tree[t]) = {left = new_tree}   def change_right_tree( new_tree : tree[t]) = {right = new_tree} } 

sugested solution:

var current_node = node("test") current_node.left = node("end node") var answer = current_node.get_answer2("n") answer match {     case end => {         println("empty")         }     case x: node[_] => {         println("node")         current_node = x         println(current_node)     } } 

problem code variable 'left' declared tree. , tree broader type node. compiler doesn't matter object assigned variable 'left' of type 'node'.

in solution var 'answer' of type 'tree'. cast type 'node' used 'match' construction 2 cases (first empty nodes , second not-empty)


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -