简体   繁体   English

了解二进制搜索树计数

[英]Understanding Binary Search Tree Counting

I am having trouble understanding how this Binary Search Tree method is counting nodes, I have looked at many examples online, however I cannot find one which will explain exactly what is happening. 我很难理解这种二叉搜索树方法是如何计算节点的,我在网上查看了许多示例,但是找不到一个可以确切说明正在发生什么的示例。

Here is an example: 这是一个例子:

public int nodes() {
    int leftNodes = 0;

    if (left != null) {
        leftNodes = left.nodes();
    }
    int rightNodes = 0;

    if (right != null) {
        rightNodes = right.nodes();
    }
    return leftNodes + rightNodes + 1;
}

This is how I am understanding the process of this method, and maybe someone can help me understand where I'm going wrong. 这就是我了解此方法过程的方式,也许有人可以帮助我了解我要去哪里。

  1. The method is called from outside of itself from a BTS Object; 该方法是从BTS对象自身之外调用的; "tree.nodes() etc". “ tree.nodes()等”。
  2. int leftNodes is declared and set to 0. 声明了int leftNodes并将其设置为0。
  3. If there is a left node (assume there is), then the value of leftNodes will be assigned to the return value of the call to nodes(); 如果有一个左节点(假设有),则将leftNodes的值分配给对nodes()的调用的返回值;
  4. The recursive call will go through the nodes method again, assigning leftNodes to zero again. 递归调用将再次通过nodes方法,再次将leftNodes分配为零。

So what I don't understand is where is the leftNodes variable being incremented? 所以我不明白的是,leftNodes变量在哪里递增? It seems like it's just recursing through the method again but the value doesn't change, and from how I see leftNodes and rightNodes will always be 0. 似乎只是再次遍历该方法,但该值没有改变,从我的角度来看,leftNodes和rightNodes始终为0。

I found another example of BTS counting, this one using C++ 我发现了另一个BTS计数的例子,这个例子使用C ++

int CountNodes(node*root)
{
if(root==NULL)
    return 0;
if(root->left!=NULL)
{
    n=n+1;
    n=CountNodes(root->left);
}
if(root->right!=NULL)
{
    n=n+1;
    n=CountNodes(root->right);
}
return n;
}

I find this method much easier to follow, as n is clearly being incremented everytime a node is found. 我发现此方法更容易执行,因为每次找到一个节点时n都会明显增加。

My question is how is the leftNodes/rightNodes value being incremented in the recursive call? 我的问题是在递归调用中如何增加leftNodes / rightNodes的值?

You should think about the end of the recursion. 您应该考虑递归的结束。

Suppose you have a single node with no children. 假设您有一个没有子节点的单个节点。

Both left and right would be null , so you will make no recursive calls. 无论leftrightnull ,那么你将没有递归调用。

You'll return 你会回来

leftNodes + rightNodes + 1; // 0 + 0 + 1 == 1

Now, suppose you have a simple tree that consists of a root, a left child and a right child. 现在,假设您有一棵简单的树,该树由一个根,一个左子代和一个右子代组成。

When you call nodes() for the root of that tree, both left and right are not null, so we'll call both left.nodes() and right.nodes() . 当您为该树的根调用nodes()leftright都不为null,因此我们将同时调用left.nodes()right.nodes() Since both the left and right children are leaf nodes (ie they have no children), the recursive calls for both will return 1, as explained above. 由于左子节点和右子节点都是叶节点(即,它们没有子节点),因此对它们的递归调用将返回1,如上所述。

Therefore, when the recursive calls return, we'll return 因此,当递归调用返回时,我们将返回

leftNodes + rightNodes + 1; // 1 + 1 + 1 == 3

which is the number of nodes in our tree. 这是我们树中的节点数。

The variables leftNodes and rightNodes are local to the method nodes() which means that there is a different instance of these variables for each call of the method. 变量leftNodesrightNodes在方法nodes()本地,这意味着对于方法的每次调用,这些变量都有不同的实例。

So when you call recursively the method (with left.nodes() for instance), the value of leftNodes is the same before and after the recursive call because it (the call) will have it's one instance of leftNodes (and rightNodes ). 因此,当您以递归方式调用该方法(例如,使用left.nodes() )时,在递归调用之前和之后, leftNodes的值是相同的,因为它(该调用)将具有它是leftNodes (和rightNodes )的一个实例。

This is a basic implementation of inorder traversal . 这是inorder traversal的基本实现。 For each node it goes to left child until there is no left child remaining (Because of recursion, think like in each visit of a node it is pushed to a stack). 对于每个节点,它将转到左子节点,直到没有剩余的左子节点为止(由于递归,请像在每次访问节点时那样将其推入堆栈)。 Then it repeats the same procedure for the top of the stack until there is no element left in the stack (Again note that, Stack is used to make things simpler compared to recursion). 然后,它对堆栈的顶部重复相同的过程,直到堆栈中没有剩余元素为止(再次注意,与递归相比,使用Stack可以使事情更简单)。 While doing so, it basically increments the total sum by one for each node it visits. 这样做时,它基本上将访问的每个节点的总和增加一。

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

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