public void Process() { PrefixSumTrie pst = new PrefixSumTrie(); pst.AddWord("foo", 5); pst.AddWord("foobar", 15); string prefix = "foo"; Console.WriteLine("Cummulative for {0}: {1}", prefix, pst.Cummulative(prefix)); pst.AddWord("foo", 15); Console.WriteLine("Cummulative for {0}: {1}", prefix, pst.Cummulative(prefix)); pst.AddWord("foofighters", 100); Console.WriteLine("Cummulative for {0}: {1}", prefix, pst.Cummulative(prefix)); }
private void AddWord(string word, int val, ref int previousVal) { cummulative += val; if (String.IsNullOrEmpty(word)) { if (isWord) { previousVal = wordVal; } isWord = true; wordVal = val; } else { if (!children.ContainsKey(word[0])) { children.Add(word[0], new PrefixSumTrie()); } PrefixSumTrie child = (PrefixSumTrie)children[word[0]]; child.AddWord(word.Substring(1), val, ref previousVal); } }