I'm using binary search tree to save strings input by user. I wanted to get the total number of strings that matches the range of strings given. However, my current code is unable to add the number of strings correctly.
I'm using recursion to help me count the number of strings that is within the range start and end. For example, it passes through count twice, but my final output is 1 instead of 2. Here is my code:
private int matchQuery(BST T, String START, String END, int count) {
if (T == null) return count;
// Go left of tree
matchQuery(T.left, START, END, count);
if(T.key != null && withinStartRange(T.key, START)) {
count++;
}
// Go right of tree
return matchQuery(T.right, START, END, count);
}
matchQuerydo ? Your method is not recursive unlessmatchQueryis recursive or callsnumberOfStrings... but since yo don't provide its code