从现有的左右树中创建一棵新树
我的代码类似于此线程中给出的代码。
template<class T>
class BinarySearchTree
{
private:
struct tree_node
{
tree_node* left;
tree_node* right;
T data;
tree_node( const T & thedata, tree_node * l = NULL, tree_node * r = NULL )
: data( thedata ), left( l ), right( r ) { }
};
tree_node* root;
public:
BinarySearchTree()
{
root = NULL;
}
}
在我的主程序中,需要这样做:
我有两棵树:
BinarySearchTree<T> tree1;
BinarySearchTree<T> tree2;
我需要创建一棵新树:
根作为 T 的对象,左=树1,右=树2;
为此,我尝试添加此构造函数:
BinarySearchTree(const T& x, tree_node* l, tree_node* r);
并尝试从 main 调用:
BinarySearchTree<T> newTree(T object,tree1,tree2);
我知道这不起作用,但我应该做什么?
编译错误:
错误 C2664:'BinarySearchTree::BinarySearchTree(const T &,BinarySearchTree::tree_node *,BinarySearchTree::tree_node *)':无法将参数 2 从 'BinarySearchTree *' 转换为 'BinarySearchTree ::树节点*'
My code is similar to the one given in this thread.
template<class T>
class BinarySearchTree
{
private:
struct tree_node
{
tree_node* left;
tree_node* right;
T data;
tree_node( const T & thedata, tree_node * l = NULL, tree_node * r = NULL )
: data( thedata ), left( l ), right( r ) { }
};
tree_node* root;
public:
BinarySearchTree()
{
root = NULL;
}
}
In my main program, there a need for this:
I have two trees:
BinarySearchTree<T> tree1;
BinarySearchTree<T> tree2;
I need to create a new tree with:
root as an object of T and left = tree1 and right = tree2;
To do this I tried to add this constructor:
BinarySearchTree(const T& x, tree_node* l, tree_node* r);
and trying to call from main:
BinarySearchTree<T> newTree(T object,tree1,tree2);
I understand this won't work but what should I do?
Compile Error:
error C2664: 'BinarySearchTree::BinarySearchTree(const T &,BinarySearchTree::tree_node *,BinarySearchTree::tree_node *)' : cannot convert parameter 2 from 'BinarySearchTree *' to 'BinarySearchTree::tree_node *'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先:您对构造函数的调用不正确,应该是这样的:
我建议实现一个所谓的复制构造函数,一个构造函数,采用同一类的实例作为参数:
这将让您创建一个来自子节点的新树。
希望我已经回答了您的问题,如果还有什么不清楚的地方,请随时提问! :)
First of all: your call of the constructor is not correct, it should be like this:
I would suggest, to implement a so called copy constructor, a constructor, taking an instance of the same class as argument:
this would let you create a new tree from a child node.
I hope I have answered your question, feel free to ask if anything is not clear enough! :)
在实现您想要实现的目标后,您会遇到许多问题。首先,在加入树之后,您想要在根节点存储什么的方式是最重要的,在许多情况下,生成的树不会是二叉搜索树。您只需通过传递对指针的引用或对树根节点指针的指针即可解决此编译器问题。
There are many problems which you will surface after implementing what you are trying to achieve here. First of all after joining the trees the way you want what are you storing at the root node is most important and in many cases the resultant tree will not be a binary search tree. you can solve this compiler issue just by passing reference to pointer or pointer to pointer of root node of the trees.
如果您使用 * 定义函数参数,则表示编译器需要一个指向对象的指针。如果这样做,您必须给出对象的地址,而不是对象本身,如下所示:
您可以更改调用方法的方式,也可以更改方法定义以接受引用,就像使用 const T& 一样。
If you define your function arguments with a * this says the compiler they expect a pointer to an object. If you do this you must give the address of the object, not the object itself like:
You can either change how you call the method or you can change the methods definition to accept a reference as you do with const T&.