I am currently implementing a BK-Tree to make a spell checker. The dictionary I am working with is very large (millions of words), which is why I cannot afford any inefficiencies at all. However, I know that the lookup function that I wrote (arguably the most important part of the entire program) can be made better. I was hoping to find some help regarding the same. Here's the lookup that I wrote:
public int get(String query, int maxDistance)
{
calculateLevenshteinDistance cld = new calculateLevenshteinDistance();
int d = cld.calculate(root, query);
int tempDistance=0;
if(d==0)
return 0;
if(maxDistance==Integer.MAX_VALUE)
maxDistance=d;
int i = Math.max(d-maxDistance, 1);
BKTree temp=null;
for(;i<=maxDistance+d;i++)
{
temp=children.get(i);
if(temp!=null)
{
tempDistance=temp.get(query, maxDistance);
}
if(maxDistance<tempDistance)
maxDistance=tempDistance;
}
return maxDistance;
}
I know that I am running the loop an unnecessarily large number of times and that we can trim the search space to make the lookup faster. I'm just not sure how to best do that.