Thursday, June 12, 2008

Trie Algorithm for Autocomplete and Suggest word

Currently I try using Trie for Suggest and Autocomplete algorithm.

public List<string> Suggest2(string str)
{
List<String> result = new List<String>();
TrieNode curr = root;
foreach (char c in str.Trim().ToLower().ToCharArray())
{
curr = curr.GetChild(c);
if (curr == null)
return result;
}

Suggest2(curr, new StringBuilder(str), result);
return result;

}

public void Suggest2(TrieNode curr,StringBuilder sbTemp, List<string> result)
{
if (curr == null) return;
if (curr.IsEnd) result.Add(sbTemp.ToString());

foreach (char c in curr.Childs.Keys)
{
sbTemp.Append(c);
Suggest2(curr.Childs[c], sbTemp, result);
sbTemp.Length--;
}
}



Any Suggestion to improve this algorithm except changing String to StringBuilder ?

No comments: