//get all words that start with the prefix "s" public List <string> get_matches(string s) { Search_Node curr_node = head; foreach (char c in s) { curr_node = curr_node[c]; if (null == curr_node) { return(null); } } List <string> res = new List <string>(); foreach (int i in curr_node.pre_words) { res.Add(words[i]); } return(res); }
public void Add(string s) { char last_letter = ' '; Search_Node curr_node = head; Search_Node last_node = head; foreach (char c in s) { last_node = curr_node; if (null == curr_node[c]) { curr_node[c] = new Search_Node(); } curr_node = curr_node[c]; curr_node.pre_words.Add(counter); last_letter = c; } if (!last_node[last_letter].is_a_word) { last_node[last_letter].is_a_word = true; words.Add(counter++, s); } }
public Search() { words = new Dictionary <int, string>(); counter = 0; head = new Search_Node(); }