1

以下のようなノード構造があります。このノードには、子として文字列データとノードのリストがあります。このツリーでデータを検索したかったので、再帰関数 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);
              }
         }
     }
}
4

4 に答える 4

2

これを使用してください:

public Node FindNode(Node intree,string target)
{
    if(intree.getData()==target)
          return intree;
    else 
    {

              foreach(Node n in intree.getchildren())
              {                
                  Node node = FindNode(n,target) ; //CHECK FOR RETURN 

                  if(node != null) 
                      return node;            

              }

     }

     return null;
}

違いは、FindNodeメソッドの戻りをチェックし、そうでない場合はnull結果を返すことです。

ツリー内の重複したノード(同じ文字列を持つノード)の場合、最初のオキュアランスが返されることに注意してください。

于 2012-04-20T07:05:53.627 に答える
2

ツリーに複数の一致がある可能性があることを考えると、を返す方がよいでしょうIEnumerable<Node>getまた、それらの奇妙なメソッドをそこに入れる必要はありません。そして最後に、リーフノードのみを検索することを意味しましたか、それともすべてのノードを検索することを意味しましたか(elseステートメント)?

public IEnumerable<Node> FindNode(Node intree,string target)
{
    if(intree.Data ==target)
        yield return intree;

    foreach (var node in intree.Children.SelectMany(c => FindNode(c, target))
        yield return node;
}

最初に一致するノードが必要な場合はFirst()、結果を呼び出すだけです。1つだけであることを確認したい場合は、それを呼び出しますSingle()

于 2012-04-20T07:05:54.687 に答える
2

null を返し、このメソッドを呼び出している場所でチェックを適用することをお勧めします。null が返された場合は、ノードが見つからないことを意味します。コードは次のとおりです

    public static Node FindNode(Node intree, string target)
    {
        if (intree.getData() == target)
            return intree;

        foreach (Node node in intree.getchildren())
        {
            Node toReturn = FindNode(node, target);
            if (toReturn != null) return toReturn;
        }

        return null;
    }
于 2012-04-20T06:50:22.807 に答える
0
   public Node FindNodeRecursively(Node parentNode, string keyword)
        {
            if(parentNode.getData() == keyword)
            {
                return parentNode;
            }
            else
            {
                if(parentNode.Children != null)
                {
                    foreach(var node in parentNode.Children)
                    {
                        var temp = FindNodeRecursively(node, keyword);
                        if(temp != null)
                            return temp;
                    }
                }
                return null;
            }
        }
于 2012-04-20T06:58:49.240 に答える