依赖于类成员属性的类实例成员



我的教师为我提供了一个代表二叉树的类,我们必须用它来进行识别。 所以我正在做一个名为集群的类,事情是这样的:

BinTree(我只复制了我的东西,尽管这是最低限度的必要(:

template <typename T>
class BinTree {
public:
// Constructs an empty tree. Θ(1).
BinTree ()
:   p(nullptr)
{   }
// Constructs a tree with a value x and no subtrees. Θ(1).
explicit BinTree (const T& x);
// Constructs a tree with a value x and two subtrees left and right. Θ(1).
explicit BinTree (const T& x, const BinTree& left, const BinTree& right);

簇:

class Cluster {
private:
BinTree<std::pair<std::string, double>> _cluster;
public:
Cluster();
//etc....
}

由于我不能使用继承(我们还没有达到那部分(,我真的不知道 Cluster 的构造函数会如何。集群对象将是一个二叉树,但我从"叶子"开始(英语不是我的第一语言,所以我不知道如何称呼它(,因此我必须创建一个带有字符串的集群双精度= 0.0。

我假设集群构造函数是这样的:

Cluster(const std::string& id) : _cluster(make_pair(id, 0.0)){};

这是对的吗?

然后,有 2 个特定的集群,我会将它们合并为一个。这个新的集群,因为它的_cluster属性是一个二叉树,将是以前的父级,这里是我必须使用 BinTree 的第 3 个构造函数,但我不知道该怎么做。

根据我的理解,您希望合并 2 个集群,因此,您可以像在 BinTree 类中那样完成它

explicit Cluster ( Cluster& left, const Cluster& right){
_cluster = Cluster(left, right) ;
}

我不取消集群类的功能,直接使用 BinTree 可能更容易。

最新更新