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); }
/** 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(); }
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); }
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); } }