Example #1
0
        public void AddWord(string word)
        {
            // add the char at word 0
            if (string.IsNullOrEmpty(word))
            {
                return;
            }

            var firstChar = word[0];

            var remainingWord = word.Substring(1);

            if (Neighbors.ContainsKey(firstChar))
            {
                var nextNode = Neighbors[firstChar];
                nextNode.AddWord(remainingWord);
            }
            else
            {
                var newNode = new TrieNodeCCI(firstChar);

                if (remainingWord.Length == 0)
                {
                    newNode.Terminates = true;
                }

                Neighbors[firstChar] = newNode;

                newNode.AddWord(remainingWord);
            }
        }
Example #2
0
 // take a list of strings as an argument and constructs a trie that stores these strings
 public TrieCCI(string[] list)
 {
     _root = new TrieNodeCCI();
     foreach (var word in list)
     {
         _root.AddWord(word);
     }
 }