public int Insert(string s, int pos, int freq = 1) { if (string.IsNullOrEmpty(s) || pos >= s.Length) { return 0; } if (Children == null) { Children = new Dictionary<char, TrieNode>(); } var c = s[pos]; if (!Children.ContainsKey(c)) { Children[c] = new TrieNode(c); } var curNode = Children[c]; if (pos == s.Length - 1) { curNode.Frequency += freq; return curNode.Frequency; } return curNode.Insert(s, pos + 1, freq); }
public Trie() { Root = new TrieNode(RootChar); Count = 0; }