Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 搜索树/trie时是否返回多个节点?_C#_Tree_Suffix Tree - Fatal编程技术网

C# 搜索树/trie时是否返回多个节点?

C# 搜索树/trie时是否返回多个节点?,c#,tree,suffix-tree,C#,Tree,Suffix Tree,我构建了一个后缀trie,一个包含字符串所有后缀的树,其中每个节点只包含一个字符,每个路径的末尾都有一个后缀节点,其中包含字符串中后缀的位置 假设我的trie包含单词“Cat”、“Car”和“Can”,我想搜索“Ca”,结果应该返回3个后缀节点,因为搜索字符串位于3个不同的位置。我设法在树中搜索“Ca”,但一旦到达该点,我就不知道如何继续遍历“a”节点的子节点以查找所有后缀节点 我正在考虑使用某种集合向添加后缀节点,然后返回集合。这种方法有意义吗,还是有更好的解决方案 我已经解决了下面的搜索问题

我构建了一个后缀trie,一个包含字符串所有后缀的树,其中每个节点只包含一个字符,每个路径的末尾都有一个后缀节点,其中包含字符串中后缀的位置

假设我的trie包含单词“Cat”、“Car”和“Can”,我想搜索“Ca”,结果应该返回3个后缀节点,因为搜索字符串位于3个不同的位置。我设法在树中搜索“Ca”,但一旦到达该点,我就不知道如何继续遍历“a”节点的子节点以查找所有后缀节点

我正在考虑使用某种集合向添加后缀节点,然后返回集合。这种方法有意义吗,还是有更好的解决方案

我已经解决了下面的搜索问题。它没有返回任何节点的原因与树的创建以及节点之间的差异有关:

public void SearchTrie(Node parent, String s, List<SuffixNode> suffixes)
    {
        Node n = FindNode(parent, s);
        FindSuffixes(n, suffixes);
    }

    private void FindSuffixes(Node parent,List<SuffixNode> suffixes)
    {            
        if (parent is SuffixNode)
        {
            suffixes.Add((SuffixNode)parent);
        }
        else
        {
            foreach (KeyValuePair<char, Node> child in parent.children)
            {
                FindSuffixes(child.Value, suffixes);
            }
        }
    }

    private Node FindNode(Node parent, String s)
    {
        if ((s.Length == 1) && parent.children.ContainsKey(s[0]))
        {
            Node n = parent.children[s[0]];
            return n;
        }

        while (s[0].ToString() != "")
        {
            if (parent.children.ContainsKey(s[0]))
            {
                if ((s.Length > 1))
                {
                    return FindNode(parent.children[s[0]], s.Substring(1));
                }

            }
            else
            {
                break;
            }

        }

        return null;
    }
public void SearchTrie(节点父节点、字符串、列表后缀)
{
节点n=FindNode(父节点,s);
FindSuffix(n,后缀);
}
私有void FindSuffixes(节点父节点,列表后缀)
{            
if(父节点是SuffixNode)
{
添加((后缀节点)父节点);
}
其他的
{
foreach(parent.children中的KeyValuePair子级)
{
FindSuffix(child.Value,后缀);
}
}
}
私有节点FindNode(节点父节点,字符串s)
{
if((s.Length==1)和&parent.children.ContainsKey(s[0]))
{
节点n=parent.children[s[0]];
返回n;
}
而(s[0].ToString()!=“”)
{
if(parent.children.ContainsKey(s[0]))
{
如果((s.长度>1))
{
返回FindNode(父.子[s[0]],s.子字符串(1));
}
}
其他的
{
打破
}
}
返回null;
}
节点:

类节点
{
公共字符标签;
公共节点父节点;
公共字典儿童;
公共节点(节点NewParent,char NewLabel)
{
this.parent=NewParent;
this.label=NewLabel;
儿童=新字典();
}
}
我正在考虑使用某种集合向添加后缀节点,然后返回集合

这将是第一选择

…还是有更好的解决方案

还有其他解决方案,比如

  • 填写调用者提供的列表
  • 将迭代器块与
    一起使用以产生返回

但是,如果没有任何特殊要求,请填写
列表
,并将其作为
IEnumerable

返回。您可以发布一些代码/示例吗?这有助于理解这个问题。。。因为在我看来,您应该简单地遍历子树(在“CA”下面)……这就是我试图做的,但我发现这有点困难。我会发布我以前的搜索功能,还有哪些代码会有用?你能发布节点类吗?我用新的搜索方法和节点类更新了我的问题
class Node
{
    public char label;
    public Node parent;
    public Dictionary<char,Node> children;

    public Node(Node NewParent, char NewLabel)
    {
        this.parent = NewParent;
        this.label = NewLabel;
        children=new Dictionary<char,Node>();
    }
}