scala - Finding node in tree -
i have tree can have multiple root nodes. node defined as:
case class node[a] ( id: int, data: a, var children: option[seq[node[a]]], var parent: option[node[a]] )
where id identifies node, data information related node, children sequence of nodes, , parent points parent node.
the tree class follows:
case class tree[a] ( var nodes: option[seq[node[a]]] ) { def searchparent (searchid: int): option[node[a]] = { nodes match { case some(nodes) => { ( node <- nodes ) { val parent = searchparent2(node,searchid) parent match { case some(p) => { parent } case none => none } } none } case none => none } } def searchparent2 (node: node[a], searchid: int): option[node[a]] = { node.children match { case some(nodes2) => { ( node2 <- nodes2 ) { if (node2.id == searchid) some(node2) else searchparent2(node2,searchid) } none } case none => none } } }
problem when search node based on id, none. what's wrong code?
Comments
Post a Comment