Mr Asker
Mr Asker

Reputation: 2380

Return the incremented value after recursiv call of the method

I am trying to return the depth of the string in the radix tree after finding the string in the radix tree but I am always getting the value 2.

How can I keep storing the value of counter after incrementing it and get the recursiv method depth(nextNodeEdge, restString) called several time?

Code:

private int depth(TrieNode node, String s) {
    int count = 1;

    String communsubString = checkEdgeString(node.getNext(), s);
    String restString = s.substring(communsubString.length());
    if (node.getNext() != null && !node.getNext().isEmpty()) {
        for (TrieNode nextNodeEdge : node.getNext()) {
            if (nextNodeEdge.getEdge().equals(communsubString)) {
                count++;
                if (!restString.isEmpty()) {
                    count = depth(nextNodeEdge, restString);

                } else {
                 System.out.println("Found");
                }
            }
        }
    }


    return count;
}

Upvotes: 0

Views: 58

Answers (3)

Mr Asker
Mr Asker

Reputation: 2380

This solution works for me. I needed to add break after this line count += depth(nextNodeEdge, restString); and in the else block count++;

Code

private int depth(TrieNode node, String s) {

   int  count = 1;
    String communsubString = checkEdgeString(node.getNext(), s);
    String restString = s.substring(communsubString.length());
    if (node.getNext() != null && !node.getNext().isEmpty()) {
        for (TrieNode nextNodeEdge : node.getNext()) {
            if (nextNodeEdge.getEdge().equals(communsubString)) {
                if (!restString.isEmpty()) {
                    count += depth(nextNodeEdge, restString);
                    break;

                } else {
                 System.out.println("Found");
                 count++;
                }
            }
        }
    }       
    return count;
}

Upvotes: 0

OldCurmudgeon
OldCurmudgeon

Reputation: 65811

The other solution is to make your count Mutable.

Use a Mutable<Integer> as your count and pass it up the recusrion stack.

class Mutable<T> {
    T it;
    public Mutable(T it) {
        this.it = it;
    }
    public T getIt() {
        return it;
    }
    public void setIt(T it) {
        this.it = it;
    }
}
private int depth(TrieNode node, String s) {
    return depth(new Mutable<Integer>(0), node, s).getIt();
}
private  Mutable<Integer> depth(Mutable<Integer> count, TrieNode node, String s) {
    count.setIt(count.getIt() + 1);
    String communsubString = checkEdgeString(node.getNext(), s);
    String restString = s.substring(communsubString.length());
    if (node.getNext() != null && !node.getNext().isEmpty()) {
        for (TrieNode nextNodeEdge : node.getNext()) {
            if (nextNodeEdge.getEdge().equals(communsubString)) {
                count.setIt(count.getIt() + 1);
                if (!restString.isEmpty()) {
                    depth(count, nextNodeEdge, restString);
                } else {
                    System.out.println("Found");
                }
            }
        }
    }
    return count;
}

Upvotes: 0

OldCurmudgeon
OldCurmudgeon

Reputation: 65811

You could just change:

               count = depth(nextNodeEdge, restString);

to

               count += depth(nextNodeEdge, restString);

Upvotes: 2

Related Questions