Reputation: 661
I'm writing a spell checker program and one of the functions I have to write must find suggestions for the misspelled word. Thus, I'm using recursion to traverse down my radix tree and finding all the suggestions. However, when my recursion executes, one of the counters i use keeps increasing, then it decreases, then increases again, when it should actually just increment. Here is the code for the function.
public void findSuggest(Edge tmp, String suffix, int app)
{
int used = 0;
if(tmp.tNode.children > 10)
{
used = 10;
}
else
{
used = tmp.tNode.children;
}
for(int j = 0; j < used; j++){
if(app <= 9)
{
if((!"#".equals(tmp.prefix))&&(!"#".equals(tmp.tNode.edges[j].prefix))){
suggest[app].append(tmp.prefix);
suggest[app].append(tmp.tNode.edges[j].prefix);
System.out.println("tmp.prefix: " + tmp.prefix);
System.out.println("tmp.prefix............: " + tmp.tNode.edges[j].prefix);
app++;
if(tmp.tNode.edges[j].tNode != null)
{
suggest[app].append(tmp.prefix);
System.out.println("App: " + app);
findSuggest(tmp.tNode.edges[j], suffix, app++);
}
}
}
}
}
and this is the output i'm getting: App is the counter, tmp.prefix is the parent node's prefix and tmp.prefix....... is the childs prefix.
App: 0
tmp.prefix: t
tmp.prefix............: e
App: 1
tmp.prefix: e
tmp.prefix............: s
App: 2
tmp.prefix: t
tmp.prefix............: i
App: 3
tmp.prefix: i
tmp.prefix............: c
App: 4
tmp.prefix: c
tmp.prefix............: al
App: 5
tmp.prefix: i
tmp.prefix............: se
App: 6
tmp.prefix: se
tmp.prefix............: d
App: 7
tmp.prefix: se
tmp.prefix............: s
App: 7
tmp.prefix: i
tmp.prefix............: ze
App: 8
tmp.prefix: ze
tmp.prefix............: d
App: 9
tmp.prefix: ze
tmp.prefix............: s
App: 4
tmp.prefix: t
tmp.prefix............: ure
App: 0
tmp.prefix: x
tmp.prefix............: e
App: 1
tmp.prefix: e
tmp.prefix............: d
App: 2
tmp.prefix: e
tmp.prefix............: s
App: 2
tmp.prefix: x
tmp.prefix............: ing
(This is the end result of the built string array of all word suggestions)
climatexe
climatesxed
climatiesxing
climatic
climaicalture
climaise
climaised
climasesize
climaized
climazes
Upvotes: 2
Views: 123
Reputation: 326
Use a class level variable and initialize it to zero before calling this method. You may or may not choose to use static depending on your needs.
Upvotes: 0
Reputation: 792
Since app
is an int
, its passed by value, so any changes you make on it on the recursive calls won't change it on the previous call. Suggestion: alter it to a static or to a field of your class (depending of the context you'll need it, probably will be a fild), this way you'll have the same instance of the app in all of the method calls
Upvotes: 1