【问题标题】:Compute average in Binary Search Tree C++在二叉搜索树 C++ 中计算平均值
【发布时间】:2020-06-10 03:06:57
【问题描述】:

我不知道如何在整数的二叉搜索树中找到整数的平均值。

如果树是空的,它应该返回 0。

到目前为止我的代码是:

//Node class
class Node
{
public:
private:
    int data;
    Node* left;
    Node* right;
friend class BST;
};

Binary Search Tree class
class BST
{
public:
    Node* insert(int value, Node* root)
    {
        if (root == NULL)
        {
            root = new Node;
            root->data = value;
            root->left = root->right = NULL;
        }
        else if (value < root->data)
        {
            root->left = insert(value, root->left);
        }
        else if (value > root->data)
        {
            root->right = insert(value, root->right);
        }
        return root;
    }
    void insert(int x)
    {
        root = insert(x, root);
    }
    int sum(Node* root) {
        if (root == NULL)
        {
            return 0;
        }
        return root->data + sum(root->right) + sum(root->left);
    }
    int count(Node* root)
    {
        if (root == NULL)
        {
            return 0;
        }
        return count(root->right) + count(root->left) + 1;
    }
    double average(Node* root) {
        return (double)sum(root) / count(root);
    }

private:
    Node* root;
};

int main()
{
    BST tree;
    tree.insert(20);
    tree.insert(25);
    tree.insert(15);
    tree.insert(10);
    tree.insert(30);
    tree.insert(0);

    cout << tree.average(root) << endl; // this gives an error

}

我添加了一些辅助函数,但如果其中有任何错误,请告诉我..

当我调用 average() 函数时,它给了我一个错误。我想我需要 sum() 函数和 count() 函数。如果 count() 为 0,则平均值为 0。然后 average() 函数将只是将总和除以计数。

【问题讨论】:

  • 调用average时遇到什么错误?
  • "标识符根未定义"
  • average 为什么要争论? countsum 是有意义的,因为它们是递归的,但 average 不是。

标签: c++ binary-search-tree average


【解决方案1】:

您的average 函数根本不需要参数。你可以这样做:

double average() 
{
  return static_cast<double>(sum(root)) / count(root);  // uses the actual root
}

然后这样称呼它:

cout << tree.average() << endl;

【讨论】:

  • 看来还是有错误。退出代码 -1073741819。
  • 这似乎是一个运行时错误。您需要调试代码以查看崩溃的位置以及原因。
【解决方案2】:

我的做法是让包装函数调用递归函数。递归函数可以有两个变量,它们通过引用传递给总和和数量。这样,您只需要为这两个值遍历一次。

这里是包装函数:

double average(node * root) {
    if(!root) {
        return 0; //base case if the tree is empty
    }

    int sum = 0; //variable for holding the sum
    int amount = 0; //variable for holding the amount of data

    averageRecursive(root, sum, amount); //calling the recursive function

    return (double)sum/amount;
}

还有被调用的递归函数:

void averageRecursive(node * root, int &sum, int &amount) {
    if(!root)
        return; //base case when we reach a node thats empty

    countAverage(root->left, sum, amount); //going to the left

    sum += root->data; //adding currents invocations root data to the sum
    ++amount; //adding one to the amount counter

    countAverage(root->right, sum, amount); //going to the right
}

然后您可以从maintree.average() 调用它,它会返回平均值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-23
    • 2023-03-30
    • 1970-01-01
    相关资源
    最近更新 更多