// Inserts a word into the trie. public void InsertWord(string word) { var children = _root.Children; for (int i = 0; i < word.Length; i++) { char c = word[i]; TriNode t; if (children.ContainsKey(c)) { t = children[c]; } else { t = new TriNode(c); children.Add(c, t); } children = t.Children; if (i == word.Length - 1) { t.IsLeaf = true; } } }
// Returns if the word is in the trie. public bool SearchWord(string word) { Dictionary <char, TriNode> children = _root.Children; TriNode triNode = null; foreach (var c in word) { if (children.ContainsKey(c)) { triNode = children[c]; children = triNode.Children; } else { triNode = null; break; } } return(triNode != null && triNode.IsLeaf); }
// Inserts a word into the trie. public void InsertWord(string word) { var children = _root.Children; for (int i = 0; i < word.Length; i++) { char c = word[i]; TriNode t; if (children.ContainsKey(c)) t = children[c]; else { t = new TriNode(c); children.Add(c,t); } children = t.Children; if (i == word.Length - 1) t.IsLeaf = true; } }
public Tri() { _root = new TriNode(); }