示例#1
0
            public int CountAllStartsWith(string prefix = "")
            {
                int      index = 0;
                TrieNode node  = root;

                while (index < prefix.Length && node.ContainsKey(prefix[index]))
                {
                    node = (TrieNode)node[prefix[index++]];
                }
                if (index < prefix.Length)
                {
                    return(0);
                }
                return(node.Count);
            }
示例#2
0
文件: Trie.cs 项目: bitflame/Trie
        /** Inserts a word into the trie. */
        public void Insert(string word)
        {
            TrieNode node = root;

            for (int i = 0; i < word.Length; i++)
            {
                char currentChar = word.ElementAt(i);
                if (!node.ContainsKey(currentChar))
                {
                    node.Put(currentChar, new TrieNode());
                }
                node = node.Get(currentChar);
            }
            node.SetEnd();
        }
示例#3
0
文件: Trie.cs 项目: bitflame/Trie
        private TrieNode SearchPrefix(string word)
        {
            TrieNode node = root;

            for (int i = 0; i < word.Length; i++)
            {
                char curLetter = word.ElementAt(i);
                if (node.ContainsKey(curLetter))
                {
                    node = node.Get(curLetter);
                }
                else
                {
                    return(null);
                }
            }
            return(node);
        }
示例#4
0
            public void Add(string str)
            {
                int      index = 0;
                TrieNode node  = root;

                while (index < str.Length && node.ContainsKey(str[index]))
                {
                    node = (TrieNode)node[str[index++]];
                }
                if (index < str.Length)
                {
                    do
                    {
                        node.Add(str[index], new TrieNode());
                        node = (TrieNode)node[str[index++]];
                    }while(index < str.Length);
                    node.Add('\0', null);
                    IncreaseCount(str);
                }
            }