示例#1
0
 // Inserts a word into the trie.
 public void Insert(String word)
 {
     TrieNode tn = root;
     foreach (char c in word)
     {
         if (tn.child.ContainsKey(c))
         {
             tn = tn.child[c];
         }
         else
         {
             var next = new TrieNode();
             tn.child.Add(c, next);
             tn = next;
         }
     }
     if (!tn.child.ContainsKey('\0'))
         tn.child.Add('\0', null);
 }
示例#2
0
 // Adds a word into the data structure.
 public void AddWord(String word)
 {
     TrieNode tn = root;
     foreach (char c in word)
     {
         int index = c - 'a';
         if (tn.child[index] != null)
         {
             tn = tn.child[index];
         }
         else
         {
             var next = new TrieNode();
             tn.child[index] = next;
             tn = next;
         }
     }
     tn.isword = true;
 }
示例#3
0
            private bool Search(string word, TrieNode node, int index, int len)
            {
                if (index == len)
                    return node.isword;

                char c = word[index];
                if (c == '.')
                {
                    for(int i=0; i< 26; i++)
                    {
                        if (node.child[i] != null && Search(word, node.child[i], index + 1, len))
                            return true;
                    }
                    return false;
                }
                else if (node.child[c - 'a'] != null)
                    return Search(word, node.child[c - 'a'], index + 1, len);
                else
                    return false;
            }
示例#4
0
 public Trie()
 {
     root = new TrieNode();
 }
示例#5
0
 public WordDictionary()
 {
     root = new TrieNode();
 }
示例#6
0
 public Trie()
 {
     root = new TrieNode();
     next = new Dictionary <char, TrieNode>();
 }