简体   繁体   English

二进制搜索树分配运算符(递归)

[英]binary search tree assignment operator (recursion)

I'm having huge troubles understanding how to implement this. 我在理解如何实现这一点方面遇到了很大的麻烦。

Here is the prototype and implementation I've tried thus far (please note that two of the attempts are commented out - labeled (1) and (2) 这是到目前为止我尝试过的原型和实现(请注意,其中两次尝试已被注释掉-标记为(1)和(2)

//--- ADD PROTOTYPE OF ASSIGNMENT OPERATOR HERE
 // (1) void operator=(const BST<DataType> & origList);
 // (2) BST<DataType>& BST<DataType>::operator=(const BST& origList)
BST::operator=(const BST& origList);

Currently each one results with the following error: 当前,每个结果都会出现以下错误:

home\visual studio 2010\projects\bst.h(139): error C4430: missing type specifier - int assumed.

Here is the implementation of the assignment operator: 这是赋值运算符的实现:

//--- Definition of operator=
template <typename DataType>
BST<DataType>& BST<DataType>::operator=(const BST& origList)
{
 if (this != &origList)
 {
    copyTree(origList.myRoot, myRoot);
    destroy(myRoot);
 }
 return *this;
}

Here is the copyTree recursive function(s): 这是copyTree递归函数:

//--- Definition of copy constructor()
template <typename DataType>
BST<DataType>::BST(const BST<DataType> & origList)
{
 copyTree(origList.myRoot, myRoot);
}

//--- Definition of copyTree()
template <typename DataType>
void BST<DataType>::copyTree(BinNodePointer origRoot, BinNodePointer & subtreeRoot)
{
 if (origRoot == 0)
    subtreeRoot = NULL;
 else
 {
    subtreeRoot = new BinNode(origRoot->data);
    copyTree(origRoot->left, subtreeRoot->left);
    copyTree(origRoot->right, subtreeRoot->right);

    //origRoot = new BinNode(subtreeRoot->data);
    //copyTree(subtreeRoot->left, origRoot->left);
    //copyTree(subtreeRoot->right, origRoot->right);
 }
}

The copy constructor works beautifully, however the assignment operator I've failed to grasp what is needed here. 复制构造函数的工作很漂亮,但是赋值运算符我未能掌握这里需要什么。 Any help is greatly appreciated! 任何帮助是极大的赞赏!

PS You may have noticed I have "origList", it should be named "origTree" however I'm borrowing this from my previously created constructors for LinkedLists. PS:您可能已经注意到我有“ origList”,应该将其命名为“ origTree”,但是我是从以前为LinkedLists创建的构造函数中借用的。

This 这个

BST<DataType>& BST<DataType>::operator=(const BST& origList)

should be 应该

BST<DataType>& BST<DataType>::operator=(const BST<DataType>& origList)

with following declaration in the class 在班上有以下声明

BST<DataType>& operator=(const BST<DataType>& origList)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM