Example #1
0
        /** Returns if there is any word in the trie that starts with the given prefix. */
        public bool StartsWith(string prefix)
        {
            ImplementTrieNode current = Root;

            for (int i = 0; i < prefix.Length; i++)
            {
                char c = prefix[i];
                if (!current.Next.ContainsKey(c))
                {
                    return(false);
                }

                current = current.Next[c];
            }

            return(true);
        }
Example #2
0
        /** Inserts a word into the trie. */
        public void Insert(string word)
        {
            ImplementTrieNode current = Root;

            for (int i = 0; i < word.Length; i++)
            {
                char c = word[i];
                if (!current.Next.ContainsKey(c))
                {
                    current.Next[c] = new ImplementTrieNode();
                }

                current = current.Next[c];
            }

            current.Final = true;
        }
Example #3
0
        /** Returns if the word is in the trie. */
        public bool Search(string word)
        {
            ImplementTrieNode current = Root;

            for (int i = 0; i < word.Length; i++)
            {
                char c = word[i];
                if (!current.Next.ContainsKey(c))
                {
                    return(false);
                }

                current = current.Next[c];
            }

            return(current.Final);
        }
Example #4
0
 public ImplementTrie()
 {
     Root = new ImplementTrieNode();
 }