static void Main()
        {
            Stopwatch sw = new Stopwatch();
            TrieNode start = new TrieNode();
            Dictionary<string, int> wordsInDictionary = new Dictionary<string, int>();
            var allWords = SetInputText();

            List<string> wordsToSearch = new List<string>();
            // change number to increase searched words count
            for (int i = 0; i < 50; i++)
            {
                wordsToSearch.Add(allWords[i].ToString());
            }

            AddWordsForSearchInDictionary(sw, wordsToSearch, wordsInDictionary);
            AddWordsForSearchInTrie(sw, start, wordsToSearch);

            IncrementOccuranceCountTrie(sw, start, allWords);
            IncrementOccuranceCountDictionary(sw, wordsInDictionary, allWords);

            Console.WriteLine("Searched words count trie: ");
            foreach (var word in wordsToSearch)
            {
                Console.WriteLine("{0}: {1}", word, start.CountWords(start, word));
            }

            Console.WriteLine("\nSearched words count dictionary: ");
            foreach (var item in wordsInDictionary)
            {
                Console.WriteLine("{0}: {1}", item.Key, item.Value);
            }
        }
 private static void AddWordsForSearchInTrie(Stopwatch sw, TrieNode start, List<string> words)
 {
     sw.Start();
     foreach (var item in words)
     {
         start.AddWord(start, item.ToString());
     }
     sw.Stop();
     Console.WriteLine("Time to populate the trie: {0}\n", sw.Elapsed);
 }
        private static void IncrementOccuranceCountTrie(Stopwatch sw, TrieNode start, MatchCollection allWords)
        {
            sw.Restart();
            foreach (var word in allWords)
            {
                start.AddOccuranceIfExists(start, word.ToString());
            }

            sw.Stop();
            Console.WriteLine("Adding searched words count trie for: {0}", sw.Elapsed);
        }
Beispiel #4
0
        private void AddOccuranceIfExists(TrieNode node, string word, int indexInWord)
        {
            if (word.Length == indexInWord)
            {
                node.Words += 1;
            }
            else
            {
                int nextCharIndex = word[indexInWord] - 'a';
                indexInWord++;
                node.Prefixes += 1;

                if (node.edges[nextCharIndex] == null)
                {
                    return;
                }
                else
                {
                    AddOccuranceIfExists(node.edges[nextCharIndex], word, indexInWord);
                }
            }
        }
Beispiel #5
0
        private TrieNode AddWord(TrieNode node, string word, int indexInWord)
        {
            if (word.Length != indexInWord)
            {
                node.Prefixes += 1;

                int index = word[indexInWord] - 'a';
                indexInWord++;

                if (node.edges[index] == null)
                {
                    node.edges[index] = new TrieNode();
                }

                node.edges[index] = AddWord(node.edges[index], word, indexInWord);
            }

            return node;
        }
Beispiel #6
0
 public int CountWords(TrieNode node, string word)
 {
     return this.CountWords(node, word, 0);
 }
Beispiel #7
0
 public int CountPrefix(TrieNode node, string word)
 {
     return this.CountPrefix(node, word, 0);
 }
Beispiel #8
0
 public TrieNode AddWord(TrieNode node, string word)
 {
     return this.AddWord(node, word, 0);
 }
Beispiel #9
0
 public void AddOccuranceIfExists(TrieNode node, string word)
 {
     AddOccuranceIfExists(node, word, 0);
 }
Beispiel #10
0
 private int CountWords(TrieNode node, string word, int indexInWord)
 {
     if (word.Length == indexInWord)
     {
         return node.words;
     }
     else
     {
         int nextCharIndex = word[indexInWord] - 'a';
         indexInWord++;
         if (node.edges[nextCharIndex] == null)
         {
             return 0;
         }
         else
         {
             return CountWords(node.edges[nextCharIndex], word, indexInWord);
         }
     }
 }