// 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); }
// 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; }
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; }
public Trie() { root = new TrieNode(); }
public WordDictionary() { root = new TrieNode(); }
public Trie() { root = new TrieNode(); next = new Dictionary <char, TrieNode>(); }