private void ReplaceIfSuccessor(string word, TrieNodeWithWord root) { bool isRootWordFound = false; TrieNodeWithWord current = root; for (int i = 0; i < word.Length; i++) { char ch = word[i]; if (!string.IsNullOrEmpty(current.Word)) { isRootWordFound = true; break; } if (current.trieNodes[ch - 'a'] == null) { break; } current = current.Next(ch); } if (isRootWordFound) { res.Append(" " + current.Word); } else { res.Append(" " + word); } }
/** Inserts a word into the trie. */ public void Insert(string word) { TrieNodeWithWord current = root; for (int i = 0; i < word.Length; i++) { if (current.trieNodes[word[i] - 'a'] == null) { current.trieNodes[word[i] - 'a'] = new TrieNodeWithWord(); } current = current.Next(word[i]); } current.Word = word; }
public TrieWithWord() { this.root = new TrieNodeWithWord(); }