Ejemplo n.º 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);
            }
Ejemplo n.º 2
0
        /** 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();
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 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);
                }
            }