//recursively searches through the child nodes of a place holder and adds the word to total list public void DepthSearch(TrieNode placeholder, char[] chars) { var list = placeholder.childNodes.Keys.ToList(); list.Sort(); if (list.Contains(' ')) { list.Remove(' '); list.Add(' '); } foreach (var key in list) { if (results.Count != 10) { string temp = new string(chars); temp += key.ToString(); chars = temp.ToCharArray(); if (placeholder.childNodes[key].word) { results.Add(temp); } DepthSearch(placeholder.childNodes[key], chars); temp = new string(chars); temp = temp.Remove(temp.Length - 1); chars = temp.ToCharArray(); } } }
//adds a word to the trie, non recursively public void AddTitle(string title) { int count = 0; TrieNode placeholder = root; foreach (char ch in title.ToLower()) { if (!placeholder.childNodes.ContainsKey(ch)) { TrieNode newNode = new TrieNode(); placeholder.childNodes.Add(ch, newNode); } placeholder = placeholder.childNodes[ch]; if (count == title.Length - 1) { placeholder.word = true; } count++; } }
public void AddWord(String s) { // One char string passed if (s.Length == 1) { endpoint = true; rank = 1; return; } // Empty child list if (!HasChildren()) { children = new List<TrieNode>(); } TrieNode temp = null; // Find correct child for (int i = 0; i < children.Count; i++) { if (children[i].c == s[1]) { temp = children[i]; } } // Can't find the child if (temp == null) { temp = new TrieNode(s[1], this); children.Add(temp); } // Recurse temp.AddWord(s.Substring(1)); rank++; }
public Trie() { root = new TrieNode('*', null); }
public Trie() { root = new TrieNode(); }
public TrieNode(char c, TrieNode parent) { this.c = c; this.parent = parent; }