Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在二叉搜索树节点中查找次元素的平均值_C++_Data Structures_Recursion_Traversal_Binary Search Tree - Fatal编程技术网

C++ 如何在二叉搜索树节点中查找次元素的平均值

C++ 如何在二叉搜索树节点中查找次元素的平均值,c++,data-structures,recursion,traversal,binary-search-tree,C++,Data Structures,Recursion,Traversal,Binary Search Tree,我试图创建一个函数,用于查找树节点中某些数据的平均值。问题是,每个节点都包含两段数据,与其他BST不同,构建它的主要数据是一个字符串。在树中查找基于数字的元素的平均值对我来说不是问题,但由于每个节点都包含一个字符串(一个人的名字)和一个看似随机的数字(此人的重量),因此树实际上是完全混乱的,我不知道如何处理它。 这是我的节点,所以你明白我的意思: struct Node { string name; double weight; Node* leftChild;

我试图创建一个函数,用于查找树节点中某些数据的平均值。问题是,每个节点都包含两段数据,与其他BST不同,构建它的主要数据是一个字符串。在树中查找基于数字的元素的平均值对我来说不是问题,但由于每个节点都包含一个字符串(一个人的名字)和一个看似随机的数字(此人的重量),因此树实际上是完全混乱的,我不知道如何处理它。 这是我的节点,所以你明白我的意思:

struct Node {
    string name;
    double weight;
    Node* leftChild;
    Node* rightChild;
};
Node* root;
以下是其众多阶段之一的功能:

 // This isn't what I'm actually using so don't jump to conclusions
 double nameTree::averageWeight(double total, double total, int count) const
 {
     if (parent != NULL)
     {      //nonsense, nonsense
        averageWeight(parent->leftChild, total, count);
        averageWeight(parent->rightChild, total, count);
        count++;                                 
        total = total + parent->weight;      
        return total;
     }
     return (total / count);
}
在遍历树的过程中,我尝试了一些递归,但每次我设法对所有内容进行计数和合计时,都会出现一些问题,结果每次都返回(total/count)。我还尝试了一个数组实现,通过遍历树并向数组添加权重,但这不起作用,因为返回和递归受到干扰,或者其他什么


就因为我知道有人会问,是的,这是学校作业。然而,这是一个类中18个函数中的一个,所以我并没有要求任何人为我做这件事。我已经做了几个小时的这个功能了,我已经熬夜了,我的大脑很痛,所以任何帮助都将不胜感激

要计算平均值,需要两个数字:总值和集合中的元素数。您需要提供一个函数(递归可能是最简单的),该函数将遍历树并返回带有这些值的
,或者修改作为引用传递的某个参数以存储这两个值

对于您的代码,
averageWeight
返回一个
double
,但是当您递归调用它时,您将忽略(丢弃)结果。
count
参数通过copy传递,这意味着调用方将看不到递归调用中应用的修改(调用方不知道
parent->weight
对结果的权重应该是多少)


这应该足以让您开始学习。

您可以尝试以下方法:

    //total number of tree nodes
static int count=0;


 // Calculate the total sum of the weights in the tree 
double nameTree::calculateWeight(Node *parent)   
{   
    double total=0;

    if (parent != NULL)       
    {      
        //nonsense, nonsense    
        //Calculate total weight for left sub-tree
        total+=calculateWeight(parent->leftChild); 
        //Calculate weight for right sub-tree
        total+=calculateWeight(parent->rightChild);  
        //add current node weight
        total+=parent->weight;                                                       
    } 
    count++;   
    //if it is a leaf it will return 0
    return total;   
}  

double averageWeight()
{
    double weightSum;

    weightSum=calculateWeight();

    if(count!=0)
        return (weightSum/count);
    else
    {
        cout<<"The tree is empty";
        return 0;
    }
}
//树节点的总数
静态整数计数=0;
//计算树中权重的总和
双名称树::CalculateView(节点*父节点)
{   
双倍合计=0;
如果(父项!=NULL)
{      
//胡说,胡说
//计算左子树的总权重
合计+=计算视图(父项->左子项);
//计算右子树的权重
合计+=计算视图(父项->右子项);
//添加当前节点权重
总+=父级->重量;
} 
计数++;
//如果是叶子,则返回0
返回总数;
}  
双平均重量()
{
双重加权和;
权重总和=计算视图();
如果(计数!=0)
返回(加权和/计数);
其他的
{

您是否必须先遍历树,然后检查
count
。实际上
count
最初为0,并且始终为0,因为不会调用
calculatewight