Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取小于/大于BST中某个值的节点总数_Java_Binary Search Tree - Fatal编程技术网

Java 获取小于/大于BST中某个值的节点总数

Java 获取小于/大于BST中某个值的节点总数,java,binary-search-tree,Java,Binary Search Tree,我的代码有问题。它显示: 无法将BSTNode“”转换为BinarySearchTree“” 请给我一些提示来修复我的代码 public int countLowerScores(BinarySearchTree<Golfer> tree, int maxValue) { BSTNode<Golfer> node = tree.root; if (node == null) { return 0; } int count

我的代码有问题。它显示:

无法将BSTNode“”转换为BinarySearchTree“”

请给我一些提示来修复我的代码

public int countLowerScores(BinarySearchTree<Golfer> tree, int maxValue) {

    BSTNode<Golfer> node = tree.root;
    if (node == null) {
        return 0;
    }

    int countLeft = countLowerScores(node.getLeft(), maxValue);
    int countRight = countLowerScores(node.getRight(), maxValue);

    return (node.getInfo() > maxValue ? 1 : 0) + countLeft + countRight;  

    }
public int countLowerScores(二进制搜索树,int-maxValue){
BSTNode=tree.root;
if(node==null){
返回0;
}
int countLeft=countLowerScores(node.getLeft(),maxValue);
int countRight=countLowerScores(node.getRight(),maxValue);
返回(node.getInfo()>maxValue?1:0)+countLeft+countRight;
}

我不知道
BinarySearchTree
BSTNode
有什么关系,但必须有相同类型的节点、左子节点和右子节点

这就是树的全部思想,它是一个递归数据结构,这意味着根和子元素可以用同样的方式处理。下面是一个例子:

class TreeNode {
  int val;
  TreeNode left;
  TreeNode right;
  TreeNode(int x) { val = x; }
}
方法是:

public int countLowerScores(TreeNode root, int maxValue) {
    if (root == null) {
        return 0;
    }

    int countLeft = countLowerScores(root.left, maxValue);
    int countRight = countLowerScores(root.right, maxValue);

    return (root.val > maxValue ? 1 : 0) + countLeft + countRight;

}

使用泛型会使事情过于复杂。

我不知道
BinarySearchTree
BSTNode
有什么问题,但必须具有相同类型的节点、左子节点和右子节点

这就是树的全部思想,它是一个递归数据结构,这意味着根和子元素可以用同样的方式处理。下面是一个例子:

class TreeNode {
  int val;
  TreeNode left;
  TreeNode right;
  TreeNode(int x) { val = x; }
}
方法是:

public int countLowerScores(TreeNode root, int maxValue) {
    if (root == null) {
        return 0;
    }

    int countLeft = countLowerScores(root.left, maxValue);
    int countRight = countLowerScores(root.right, maxValue);

    return (root.val > maxValue ? 1 : 0) + countLeft + countRight;

}

您使用泛型将事情过度复杂化。

您有
countLowerScores
方法,该方法将
作为参数。现在无法将节点传递给它。您可以通过每个节点使用BST遍历和集成,并比较值

    public int countLowerScores(BinarySearchTree<Golfer> tree, int maxValue) {

        Iterator<Golfer> iter; // Createts interator for comparision.
        int count = 0; // Counts the number of nodes.

        // Traverse the tree in inorder.
        iter = tree.getIterator(BSTInterface.Traversal.Inorder);

        // Get the nodes and compares to passed argument.
        while (iter.hasNext()) {
            if (iter.next().getScore() <= maxValue) {
                count++;
            };
        }
        return count;
    }
public int countLowerScores(二进制搜索树,int-maxValue){
迭代器iter;//创建用于比较的迭代器。
int count=0;//计算节点数。
//按顺序遍历树。
iter=tree.getIterator(BSTInterface.Traversal.Inorder);
//获取节点并与传递的参数进行比较。
while(iter.hasNext()){

if(iter.next().getScore()您有一个
countLowerScores
方法,该方法将
tree
作为一个参数。现在您不能将节点传递给它。您可以使用BST遍历并通过每个节点进行积分,并比较值

    public int countLowerScores(BinarySearchTree<Golfer> tree, int maxValue) {

        Iterator<Golfer> iter; // Createts interator for comparision.
        int count = 0; // Counts the number of nodes.

        // Traverse the tree in inorder.
        iter = tree.getIterator(BSTInterface.Traversal.Inorder);

        // Get the nodes and compares to passed argument.
        while (iter.hasNext()) {
            if (iter.next().getScore() <= maxValue) {
                count++;
            };
        }
        return count;
    }
public int countLowerScores(二进制搜索树,int-maxValue){
迭代器iter;//创建用于比较的迭代器。
int count=0;//计算节点数。
//按顺序遍历树。
iter=tree.getIterator(BSTInterface.Traversal.Inorder);
//获取节点并与传递的参数进行比较。
while(iter.hasNext()){

if(iter.next().getScore())错误来自哪一行?调用此函数时,似乎传递的是BSTNode,而不是BinarySearchTree@Sara请同时添加BinarySearchTree类实现。还有一点,如果您已经知道BST中的节点总数,则无需同时查找较小/较大的节点。您可以查找一个,然后获取另一个从BST拥有的节点总数中减去。错误来自哪一行?调用此函数时,似乎传递的是BST节点,而不是BinarySearchTree@Sara请添加BinarySearchTree类实现。还有一点,如果您已经知道BST中的节点总数,则无需查找r两者都较小/较大。您可以查找其中一个,然后从BST拥有的节点总数中减去它来获取另一个。谢谢!这很有帮助。谢谢!这很有帮助。