Example #1
0
        public void AddWordsTest1()
        {
            Trie t = new Trie();
            t.Add("test");
            t.Add("train");
            t.Add("abc");

            Assert.AreEqual('t', t.FindChild('t').FindChild('e').FindChild('s').FindChild('t').Data);
            Assert.AreEqual('c', t.FindChild('a').FindChild('b').FindChild('c').Data);
        }
Example #2
0
        public void Add(string word)
        {
            if (String.IsNullOrEmpty(word))
            {
                words++;
                return;
            }
            prefixes++;

            Trie child = FindChild(word[0]);
            if (child == null)
            {
                child = new Trie(word[0]);
                this.edges.Add(child);
            }
            if (word.Length > 1)
                child.Add(word.Substring(1));
        }
Example #3
0
        public void EnumerateWords()
        {
            Trie t = new Trie();
            t.Add("test");
            t.Add("train");
            t.Add("task");

            Trie te = t.FindPrefix("te");
            Assert.AreEqual('e', te.Data);

            Trie ta = t.FindPrefix("ta");
            Assert.AreEqual('a', ta.Data);

            List<string> words = t.EnumerateWords("t");
            Assert.AreEqual("test", words[0]);
            Assert.AreEqual("train", words[1]);
            Assert.AreEqual("task", words[2]);

            words = t.EnumerateWords("tr");
            Assert.AreEqual("train", words[0]);
        }
Example #4
0
 private List<string> EnumerateWordsRecur(List<string> result, string prefix, Trie t)
 {
     foreach (Trie child in t.edges)
     {
         if (child.edges.Count <= 0)
             result.Add(prefix + child.Data);
         EnumerateWordsRecur(result, prefix + child.Data, child);
     }
     return null;
 }