简体   繁体   English

BST中大于给定KEY的节点数

[英]Number of nodes in a BST which are bigger than a given KEY

I wrote the code bellow to count number of nodes in a BST which are bigger than a given KEY: 我写了下面的代码来计算 BST中大于给定KEY的节点数:

int Tree::findNumbersThatBiggerThanKey(int key){
    return PrivateFindNumbersThatBiggerThanKey(key, this->rootNode);
}

int Tree::PrivateFindNumbersThatBiggerThanKey(int key, Node* start) {
    if (!start)
        return 0;

    if (start->key > key)
        return 1 + this->PrivateFindNumbersThatBiggerThanKey(key, start->leftNode) +
        this->PrivateFindNumbersThatBiggerThanKey(key, start->rightNode);

    else if (start->key < key)
        return this->PrivateFindNumbersThatBiggerThanKey(key, start->rightNode);

    else /*start->key > key*/
        return this->PrivateFindNumbersThatBiggerThanKey(key, start->leftNode);
}

this is my main: 这是我的主要:

int main() {

    int TreeKeys[16] = { 50, 76, 21, 4, 32, 64, 15, 52, 14, 100, 83, 2, 3, 70, 87, 80 };
    Tree myBst;

    cout << "Printing the three Inorder before adding numbers: \n";
    myBst.printInorder();


    for (int i = 0; i < 16; i++) {
        myBst.AddLeaf(TreeKeys[i]);
    }

    cout << "Printing the three Inorder after adding numbers: \n";
    myBst.printInorder();

    int key = 2;
    cout << "\n\nNumber of nodes that are bigger than "<<key<<" : " <<
         myBst.findNumbersThatBiggerThanKey(key);
    return 0;
}

The Inroder print works just fine: Inroder打印效果很好:

Printing the three Inorder after adding numbers:
2 3 4 14 15 21 32 50 52 64 70 76 80 83 87 100

but when I serach for number of nodes that are bigger than "2" I get 14 which is incorrect (instead of 15): 但是当我搜索大于“ 2”的节点数时,我得到14个错误的值(而不是15个):

Number of nodes that are bigger than 2 : 14

When I search for number of nodes that are bigger than "1" I get 16 which is correct result (I get a correct result for any other numbers too): 当我搜索大于“ 1”的节点数时,我得到了16个正确的结果(对于任何其他数字我也得到了正确的结果):

Number of nodes that are bigger than 1 : 16

I'm a student and newbie with recursion, I'll be glad for explanations why that's happened and how can I fix it. 我是递归的学生和新手,我很高兴为您解释为什么会发生这种情况以及如何解决它。

your else is not correct, it's condition is start->key == key which should be treated the same as start->key < key 您的else不正确,其条件是start->key == key ,应与start->key < key

so rewrite it to 所以改写成

else 
   return this->PrivateFindNumbersThatBiggerThanKey(key, start->rightNode);

or simply combine it with start->key < key 或简单地将其与start->key < key

Since you don't provide an MCVE, I cannot work on your code. 由于您不提供MCVE,因此我无法处理您的代码。 The general way of doing this, is like that: 这样做的一般方法是这样的:

int Tree::PrivateFindNumbersThatBiggerThanKey(int key, Node* node)
{
  if (node == null)
  {
    return 0;
  }
  int countLeft = PrivateFindNumbersThatBiggerThanKey(node->leftNode, key);
  int countRight = PrivateFindNumbersThatBiggerThanKey(node->rightNode, key);

  return (node->key > k ? 1 : 0) + countLeft + countRight;
}

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

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