private void GetWords(Node node, char[] charstack, int stackdepth, List<string> result) { if (node == null) { return; } if (node.IsEnd) { result.Add(new string(charstack, 0, stackdepth)); } foreach (Node child in node.Children.Values) { charstack[stackdepth] = child.Letter; GetWords(child, charstack, stackdepth + 1, result); } }
public Trie() { Root = new Node((char)0); }
public void Insert(String word) { Node current = Root; int wdepth = 1; for (int level = 0; level < word.Length; level++) { Dictionary<char, Node> children = current.Children; char ch = word[level]; if (children.ContainsKey(ch)) current = children[ch]; else { Node temp = new Node(ch); children.Add(ch, temp); current = temp; } wdepth++; } if (wdepth > Depth) { Depth = wdepth; } current.IsEnd = true; }