简体   繁体   English

如何在Java中递归添加计数?

[英]How to recursively add count in java?

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 . 我正在使用递归来帮助我计算在startend范围内的字符串数。 For example, it passes through count twice, but my final output is 1 instead of 2. Here is my code: 例如,它两次通过count,但是我的最终输出是1而不是2。这是我的代码:

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); 
    }

I think the problem might be that you only return the right side of the recursion tree, so if count was increased on the left, it is forgotten. 我认为问题可能在于您仅返回了递归树的右侧,因此如果在左侧增加计数,则将其遗忘。 Instead, you could try changing your return statement the following way: 相反,您可以尝试通过以下方式更改return语句:

private int matchQuery(BST T, String start, String end, int count) {
    if (T == null) return count;

    if(T.key != null && withinStartRange(T.key, start)) {
        count++;                       
    }

    // Go right of tree
    int rightCount =  matchQuery(T.right, start, end, count);
    // Go left of tree
    int leftCount = matchQuery(T.left, start, end, count); 

    return rightCount + leftCount - count;

}

This should count all increases in "Count". 这应该在“计数”中计算所有增加。 Hope this helps. 希望这可以帮助。

Edit: I also subtracted count from the returned amount as the current call's count is counted twice. 编辑:我还从返回的金额中减去了计数,因为当前调用的计数被计数了两次。

Edit2: My suggestion still holds - OP doesn't return the left side of the tree. Edit2:我的建议仍然成立-OP不会返回树的左侧。 Changed my code slightly. 略微更改了我的代码。

I think the count parameter is useless with Bloodworth approach since he cancels it out every time... 我认为count参数对于Bloodworth方法是无用的,因为他每次都会取消它。

I would go for 我会去

private int matchQuery(BST bst, String start, String end) {
    if (bst == null) return 0;

    int count = 0;
    if (bst.key != null && withinRange(bst.key, start, end)) count++; 

    count +=  matchQuery(bst.right, start, end);
    count += matchQuery(bst.left, start, end); 

    return count;    
}

I also fixed a number of details (naming convention etc). 我还修复了许多细节(命名约定等)。 This works, however this does not take into account the properties of the data structure. 这是可行的,但是并没有考虑数据结构的属性。 Indeed, when you are on a particular node, you know that all the nodes at its left are lower than it, and all the nodes at its right are higher. 确实,当您在特定节点上时,您知道其左侧的所有节点都比其低,而其右侧的所有节点都比其高。 Therefore, you can sometimes prevent yourself from exploring some nodes when you know they are out of the range. 因此,有时您可以在发现某些节点不在范围内时阻止自己探索它们。 I assume in he following code that we always have node.left.key < node.key < node.right.key . 我假设他在下面的代码中,我们总是有node.left.key < node.key < node.right.key I also assume that the range is inclusive on both ends 我还假设范围是两端都包括在内

// I assume start <= end has already been checked
private int matchQuery(BST bst, String start, String end) {
    if (bst == null) return 0;
    if (bst.key == null) return matchQuery(bst.left, start, end) + matchQuery(bst.right, start, end);

    int count = 0;

    int compareToStart = bst.key.compareTo(start);
    int compareToEnd = bst.key.compareTo(end);

    if (compareToStart > 0) count += matchQuery(bst.left, start, end);
    if (compareToEnd < 0) count +=  matchQuery(bst.right, start, end);   
    if (compareToStart >= 0 && compareToEnd <= 0) count++;
    return count;    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM