/** Returns if the word is in the trie. */ public bool Search(string word) // should only be true if the found node is a leaf { Node curr = _root; for (int i = 0; i < word.Length; i++) { curr = curr.FindNode(word[i]); if (curr == null) { return(false); } } return(curr.IsLeaf); }
/** Returns if there is any word in the trie that starts with the given prefix. */ public bool StartsWith(string prefix) { Node curr = _root; foreach (char c in prefix) { curr = curr.FindNode(c); if (curr == null) { return(false); } } return(true); //return _root.GetChild(prefix) != null; // return true if any node found }
/** Inserts a word into the trie. */ public void Insert(string word) { Node curr = _root; string restOfWord = word; for (int i = 0; i < word.Length; i++) { Node prev = curr; curr = curr.FindNode(restOfWord[0]); if (curr == null) { curr = prev; break; } restOfWord = restOfWord.Substring(1); } curr.AddToChildren(restOfWord); }