Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
 public Trie()
 {
     Root = new TrieNode(RootChar);
     Count = 0;
 }