public Trie(String characters) { map = new Dictionary<char, int>(TrieNode.Size); root = new TrieNode(); // no default constructor;therefore, so null argument is not possible. No need to test if (characters.Length == 0 || characters.Length > TrieNode.Size) { throw new ArgumentOutOfRangeException(); } int x = 0; foreach (var c in characters) { if (!map.ContainsKey(Char.ToUpper(c))) { map[Char.ToUpper(c)] = x; ++x; } } }
public void Add(String s) { TrieNode node = root; foreach (var c in s) { // Char cc = Char.ToUpper(c); int index = MapCharacter(c); Debug.Assert(index != -1); Debug.Assert(node != null); if (node.SubTrieNode[index] == null) { // we don't have the letter, so create a new node and assign the letter var newNode = new TrieNode { Character = c }; node.SubTrieNode[index] = newNode; } node = node.SubTrieNode[index]; } node.IsWord = true; Count++; }
private void EnumerateAllWords(TrieNode n, String runningString, List<String> runningList) { for (var i = 0; i < TrieNode.Size; i++) { if (n.SubTrieNode[i] == null) { continue; } if (n.SubTrieNode[i].IsWord) { // concat the letter and add it to the running list runningList.Add(runningString + n.SubTrieNode[i].Character); } EnumerateAllWords(n.SubTrieNode[i], runningString + n.SubTrieNode[i].Character, runningList); } }
public void Empty() { root = null; Count = 0; }