简体   繁体   English

BST中的节点计数

[英]Counting nodes in BST

I keep getting java.lang.StackOverflowError at this line: 我在这一行不断收到java.lang.StackOverflowError

count += countNodes(current.leftChild);

when I try to count the total number of the nodes in BST. 当我尝试计算BST中节点的总数时。 Could somebody please tell me why I'm getting that error? 有人可以告诉我为什么会出现这个错误吗? Thanks in advance! 提前致谢!

public int countNodes(Node node){
        if(root == null){
            System.out.println("The tree is empty!");
            return -1;
        }
        else{
            int count = 1;
            Node current = root;
            while(current.leftChild != null){
                count += countNodes(current.leftChild);
            }
            while(current.rightChild != null){
                count += countNodes(current.rightChild);
            }
            return count;
        }
    }

try this I think it should work 试试这个,我认为它应该工作

public int countNodes(Node root){
    if(root == null){
        System.out.println("The tree is empty!");
        return 0;
    } 
    else{
        Node current = root;
        int count = 0 
        if(current.leftChild != null){
            count +=  countNodes(current.leftChild)+1;
        }
        if(current.rightChild != null){
            count += countNodes(current.rightChild)+1;
        }
        return count;
    }
}

You go in recursion from the root to the leaves. 您从根到叶进行递归。 Look at your code you go with while loop. 查看您在while循环中使用的代码。 The problem that in this recursion you go to the depth with the recursion and not with the while loop. 问题在于,在此递归中,您需要使用递归而不是while循环来深入了解。

you take the result from the operation of the left tree and add it to the operation on the right. 您将从左侧树的运算中获取结果,并将其添加到右侧的运算中。 The stopping condition of the recursion is when the root dont have childs. 递归的停止条件是当根没有孩子时。

When 什么时候

  current.rightChild == null && current.leftChild == null

Maybe I confused with the init of count. 也许我对count的初始化感到困惑。 check it.. 核实..

I see two problems with your original code. 我发现您的原始代码有两个问题。

The first is that you use while statements when looking at the left and right subtrees. 第一个是在查看左右子树时使用while语句。 Since you never change the value of current , these loops will run forever. 由于您永远不会更改current的值,因此这些循环将永远运行。

The second problem is that you take a parameter called node , but never use it. 第二个问题是您采用了一个名为node的参数,但是从不使用它。 Instead, within the method body, you refer to another variable root . 而是在方法体内,您引用​​了另一个变量root This is what is causing your StackOverflowError. 这就是导致您的StackOverflowError的原因。

Changing the while s to if s should help with the infinite looping, and renaming some variables so that you use the value of parameter to the function should fix the stack overflow problem. while s更改为if s应该有助于无限循环,并重命名一些变量,以便对函数使用parameter值可以解决堆栈溢出问题。

As a side note, you may want to return zero when the node is null instead of -1. 附带说明,当节点为null时,您可能希望返回零而不是-1。 That way, you can make recursive calls without having to check the null-ness of the children, and, more importantly, when the tree has zero nodes in it, the function says so. 这样,您可以进行递归调用,而不必检查子级的空值,更重要的是,当树中的节点数为零时,该函数会这样说。 Right now, a tree with zero nodes looks like it has -1 nodes. 现在,具有零个节点的树看起来像有-1个节点。

//^_^ // ^ _ ^

public int getCount(){
    return getCount(root);
}

public int getCount(BSTNode<T> p){
    if(p == null){
        return 0;
    }
    else{
        int count = 1;
        BSTNode<T> current = p;
        while(current.left != null){
            count += getCount(current.left);
        }
        while(current.right != null){
            count += getCount(current.right);
        }
        return count;
    }
}

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

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