【发布时间】:2023-03-27 16:42:01
【问题描述】:
我的教师为我提供了一个代表二叉树的类,我们必须将它用于分配。 所以我正在做一个名为 cluster 的课程,事情是这样的:
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 的构造函数是怎样的。集群对象将是二叉树,但我从“叶子”开始(英语不是我的第一语言,所以我不知道如何称呼它),因此我必须创建一个带有字符串的集群 a double = 0.0.
我假设 Cluster 构造函数是这样的:
Cluster(const std::string& id) : _cluster(make_pair(id, 0.0)){};
这是正确的吗?
然后,拥有 2 个特定的集群,我会将它们合并为一个。这个新的集群,因为它的 _cluster 属性是一个二叉树,将是以前的父级,这里是我必须使用 BinTree 的第三个构造函数,但我不知道该怎么做。
【问题讨论】: