Beispiel #1
0
            /** Inserts a word into the trie. */
            public void AddWord(string word)
            {
                var root = roots;

                for (int i = 0; i < word.Length; i++)
                {
                    var trieNode = new TrieNode1(word[i]);
                    if (i == word.Length - 1)
                    {
                        trieNode.word = word;
                    }

                    if (root.TryGetValue(trieNode, out var foundTrie))
                    {
                        if (trieNode.word != null && foundTrie.word == null)
                        {
                            foundTrie.word = trieNode.word;
                        }
                        root = foundTrie.children;
                    }
                    else
                    {
                        root.Add(trieNode);
                        root = trieNode.children;
                    }
                }
            }
Beispiel #2
0
            public bool Search(string word, int start, HashSet <TrieNode1> root)
            {
                if (root == null)
                {
                    return(false);
                }

                for (int i = start; i < word.Length; i++)
                {
                    var ch = word[i];
                    switch (ch)
                    {
                    case '.':
                        if (i == word.Length - 1 && root.Any(child => child.word != null))
                        {
                            return(true);
                        }
                        foreach (var node in root)
                        {
                            var found = Search(word, i + 1, node.children);
                            if (found)
                            {
                                return(true);
                            }
                        }
                        return(false);

                        break;

                    default:
                        var trieNode = new TrieNode1(ch);
                        if (root.TryGetValue(trieNode, out var foundTrie))
                        {
                            if (i == word.Length - 1 && foundTrie.word != null)
                            {
                                return(true);
                            }
                            root = foundTrie.children;
                        }
                        else
                        {
                            return(false);
                        }
                        break;
                    }
                }
                return(false);
            }
Beispiel #3
0
            /** Returns if there is any word in the trie that starts with the given prefix. */
            public bool StartsWith(string prefix)
            {
                var root = roots;

                for (int i = 0; i < prefix.Length; i++)
                {
                    var trieNode = new TrieNode1(prefix[i]);
                    if (root.TryGetValue(trieNode, out var foundTrie))
                    {
                        root = foundTrie.children;
                    }
                    else
                    {
                        return(false);
                    }
                }
                return(true);
            }