C++ 计算二叉搜索树C+中的平均值+;
我被困在如何在二进制整数搜索树中找到整数的平均值 如果树为空,则应返回0 到目前为止,我的代码是:C++ 计算二叉搜索树C+中的平均值+;,c++,binary-search-tree,average,C++,Binary Search Tree,Average,我被困在如何在二进制整数搜索树中找到整数的平均值 如果树为空,则应返回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)
//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
}
//节点类
类节点
{
公众:
私人:
int数据;
节点*左;
节点*右;
朋友班;
};
二叉搜索树类
BST级
{
公众:
节点*插入(int值,节点*根)
{
if(root==NULL)
{
根=新节点;
根->数据=值;
根->左=根->右=空;
}
else if(值<根->数据)
{
根->左=插入(值,根->左);
}
else if(值>根->数据)
{
根->右=插入(值,根->右);
}
返回根;
}
空白插入(整数x)
{
根=插入(x,根);
}
整数和(节点*根){
if(root==NULL)
{
返回0;
}
返回根->数据+求和(根->右)+求和(根->左);
}
整数计数(节点*根)
{
if(root==NULL)
{
返回0;
}
返回计数(根->右)+计数(根->左)+1;
}
双平均(节点*根){
返回(双)和(根)/计数(根);
}
私人:
节点*根;
};
int main()
{
BST树;
插入(20);
插入(25);
插入(15);
树。插入(10);
插入(30);
树。插入(0);
cout您的average
函数根本不需要参数。您可以简单地执行以下操作:
double average()
{
return static_cast<double>(sum(root)) / count(root); // uses the actual root
}
double average()
{
返回static_cast(sum(root))/count(root);//使用实际根
}
这样称呼它:
cout << tree.average() << endl;
cout我的方法是让包装器函数调用递归函数。递归函数可以有两个变量,通过引用传递总和和数量。这样,两个值只需遍历一次
以下是包装器函数:
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
}
然后使用tree.average()
从main
调用它,它将返回平均值。调用average
时会出现什么错误?“标识符根未定义”为什么average
接受参数?这对于count
和sum
是有意义的,因为它们是递归的,但是average
不是。似乎我仍然有一个错误。退出时代码为-1073741819。这似乎是一个运行时错误。您需要调试代码以查看它在哪里崩溃,以及为什么崩溃。