public void AddToChildren(string prefix) { if (string.IsNullOrWhiteSpace(prefix)) { IsLeaf = true; return; } Node n = new Node(prefix[0]); n.IsLeaf = prefix.Length == 1; _children.Add(n); n.AddToChildren(prefix.Substring(1)); }
/** 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); }