以下のようなノード構造があります。このノードには、子として文字列データとノードのリストがあります。このツリーでデータを検索したかったので、再帰関数 FindNode(Node intree,string target) を書きました
public class Node
{
public string Data;
public List<Node> Children = new List<Node>();
//some code
public Node(string r)
{
this.Data = r;
}
public string getData()
{
return this.Data;
}
//some code
public List<Node> getchildren()
{
return this.Children;
}
//some code
}
target は検索したい文字列で、intree はツリーの先頭 (ROOT) です while ループの後に問題があります その後、何を返せばよいですか? 間違っていたらどう書けばいいですか?
public Node FindNode(Node intree,string target)
{
if(intree.getData()==target)
return intree;
else
{
while(intree.getchildren()!=null)
{
foreach(Node n in intree.getchildren())
{
FindNode(n,target);
}
}
}
}