简体   繁体   English

递归计数二进制搜索树中的特定节点

[英]Recursively counting specific nodes in a binary search tree

I'm trying to create a method in Java to count Nodes in a Binary Search Tree that contain a certain value and print out their contents. 我正在尝试用Java创建一种方法来计算二进制搜索树中包含特定值的节点并打印出其内容。 Each Node contains an Article object which has a title, and I want to find and print Articles in the tree containing a keyword in the title. 每个节点都包含一个带有标题的Article对象,我想在标题中包含关键字的树中查找并打印Articles。 However, I don't want the program to print out more than 10 of these (a 1-letter keyword could cause the program to stall or crash, for example). 但是,我不希望程序打印出10个以上的内容(例如,一个1个字母的关键字可能会导致程序停止或崩溃)。 The code is here: 代码在这里:

    public int traverse(String key) {
    if (root == null) {
        System.out.println("Empty Tree!");
        return 0;
    } else {
        int n = traverseHelper(root, key, 0);
        return n;
    }
}

public int traverseHelper(Node t, String key, int n) {
    if (t == null) {
        return n;
    } else {
        if (t.data.getTitle().indexOf(key) >= 0 && n <= 10) {
            System.out.println(t.data);
            n++;
        }
        return traverseHelper(t.left, key, n) + traverseHelper(t.right, key, n);
    }
}

I'm trying to keep a running count of how many times the program has printed the data, but I'm not entirely sure how. 我试图保持运行状态,以计算程序已打印数据的次数,但我不完全确定该怎么做。 Currently the program prints all occurrences, or very close to it. 当前,该程序将打印所有出现的事件,或非常接近的事件。 I know something is wrong with my recursive approach (I'm never any good at recursion anyway), so a good explanation of what I'm doing wrong would be greatly appreciated. 我知道我的递归方法出了点问题(无论如何我都不擅长递归),因此对我做错的事情的一个很好的解释将不胜感激。 This is homework, though, so I don't expect an explicit solution. 不过,这是家庭作业,因此我不希望有明确的解决方案。 A couple other things: the traverse function's purpose is to print out the Articles, so I will most likely change it to a void method later. 还有两件事:遍历函数的目的是打印出Articles,所以我很可能稍后将其更改为void方法。 It currently should return the final count of articles printed. 当前应返回已打印文章的最终数量。 Also, the tree is set up like any other BST, but I'll give any clarification of my code if necessary. 而且,树的设置与其他BST一样,但是如有必要,我将对代码进行任何说明。

Thanks! 谢谢!

The issue I see is with the n variable. 我看到的问题是与n变量。

You do 你做

return traverseHelper(t.left, key, n) + traverseHelper(t.right, key, n);

So if n was 5 to begin with, your method would return 10 (5 from each branch) even if no items were found . 因此,如果n从5开始, 即使未找到任何项 ,您的方法也会返回10(每个分支为5)

Make the function return only the number of items found in the subtree. 使该函数仅返回在子树中找到的项目数。

Since you count with n you can only pass n to one of the leafs or you can nest them like this: 由于您使用n进行计数,因此只能将n传递给其中一个叶子,也可以像这样嵌套它们:

public int traverseHelper(Node t, String key, int n) {
    if (t == null) {
        return n;
    } else {
        if (t.data.getTitle().indexOf(key) >= 0 && n <= 10) {
            System.out.println(t.data);
            n++;
        }
        return traverseHelper(t.left, key, traverseHelper(t.right, key, n));
    }
}

Nesting like this reduces stack usage slightly since you don't have the extra adding and the left becomes a tail call. 这样的嵌套会稍微减少堆栈使用量,因为您没有多余的添加,并且左侧变成了尾部调用。 Java don't have tail call optimization but it doesn't hurt to write as if it had. Java没有尾部调用优化,但是写起来好像没有问题。

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

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