/// <summary> /// Reads the fileName input file of allowable words and adds them to trie. /// </summary> /// <param name="fileName">Name of file to be read.</param> /// <returns>Whether the file was successfully read.</returns> public bool ReadDictionary(string fileName) { try { using (StreamReader sr = new StreamReader(fileName)) { ITrie temp = new TrieWithNoChildren(); while (!sr.EndOfStream) { string word = sr.ReadLine().Trim().ToLower(); temp = temp.Add(word); } _words = temp; } return(true); } catch { return(false); } }
/// <summary> /// Adds a word to this trie (no change is made if the word is already there) /// </summary> /// <param name="s">The word to add</param> /// <returns>The updated trie</returns> public ITrie Add(string s) { if (s == "") { _isWord = true; } else if (s[0] < 'a' || s[0] > 'z') { throw new ArgumentException(); } else { int loc = s[0] - 'a'; if (_children[loc] == null) { _children[loc] = new TrieWithNoChildren(); } _children[loc] = _children[loc].Add(s.Substring(1)); } return(this); }