gpt4 book ai didi

c++ - 我的 operator= 和复制构造函数在我的类(class)中无法正常工作

转载 作者:太空狗 更新时间:2023-10-29 21:25:50 25 4
gpt4 key购买 nike

主要内容:

#include <iostream>
#include <cstdlib>
#include "avl_tree.h"


using namespace std;


int main()
{
AVLTree<int> av1;
int testarray [10] = { 16, 2, 77, 40, 54 , 1 , 100, 39, 73, 35 };
AVLTree<int> av3;

for( unsigned int i = 0; i < 10; i++ )
{
av1.insert( testarray[i] );
}

AVLTree<int> av2 = av1; //test copy constructor
av3 = av1; //test operator=
av2.printTree();
av1.printTree();
av3.printTree();

exit( 0 );
}

标题:

#ifndef AVL
#define AVL

#include <iostream>
using namespace std;
/**
* An AVL tree class adapted from Weiss.
* Does NOT allow duplicate elements.
*/
template <typename Comparable>
class AVLTree
{
public:
AVLTree( ) : root ( )
{
//nothing goes in the main constructor
}

AVLTree( const AVLTree & rhs ) : root ()
{
copyNodes( rhs.root , root );
}

~AVLTree( )
{
makeEmpty( root );
delete root;
}

const AVLTree & operator=( const AVLTree & rhs )
{

makeEmpty( root );
copyNodes( rhs.root , root );
}

void printTree( ) const
{
printTree( root, 0 );
}

void makeEmpty( )
{
makeEmpty( root );
}

void insert( const Comparable & x )
{
insert( x , root );
}
// void remove( const Comparable & x );

private:

struct AVLNode
{
Comparable element;
AVLNode *left;
AVLNode *right;
int height;

AVLNode( const Comparable & element,
AVLNode *left,
AVLNode *right,
int height = 0 )
: element( element ), left( left ), right( right ), height( height ) { }
}; // end of AVLNode

AVLNode * root;

void insert( const Comparable & x, AVLNode * & t )
{

if( t == NULL )
{
//cout << "tnull" <<endl;
t = new AVLNode( x, NULL, NULL );
}
else if( x < t->element )
{
//cout << "c1" <<endl;
insert( x, t->left );
if( height( t->left ) - height( t->right ) == 2 )
if( x < t->left->element )
rotateWithLeftChild( t );
else
doubleWithLeftChild( t );
}
else if( t->element < x )
{
// cout << "c2 " << t->element << " " << x <<endl;
insert( x, t->right );
if( height( t->right ) - height( t->left ) == 2 )
if( t->right->element < x )
rotateWithRightChild( t );
else
doubleWithRightChild( t );
}
//cout << "end" << endl;
// else duplicate; do nothing
t->height = max( height( t->left ), height( t->right ) ) + 1;
}

void makeEmpty( AVLNode * & t )
{
if ( t != NULL )
{
makeEmpty ( t -> left ) ;
makeEmpty ( t -> right ) ;
}
delete t;
t = NULL;
}

void copyNodes( AVLNode * t , AVLNode * r )
{
if ( t != NULL )
{
copyNodes( t->left , r );
copyNodes( t->right, r );
insert(t->element, r );
cout << t->element << r->element << endl; //these always print as the same
}
}
#endif

恐怕我的复制构造函数和 operator= 无法正常工作,因为它们不会导致 av2 或 av3 作为 av1 的拷贝。我知道 copyNodes() 工作正常,因为第 122 行的 cout 反射(reflect) t->element 和 r->element 是相同的。为什么测试程序的第 22 行和第 24 行没有输出?

如有任何帮助,我们将不胜感激。

注意:printTree() 被省略了,因为我确定这不是问题所在,而且它是一个大函数。

其他注意事项:我已经逐步浏览了代码,并检查了其他类的其他几个复制构造函数/运算符=函数。当我逐步跟踪时,我发现它可以工作,但是当我实际编译它时却没有。

最佳答案

您可以通过为 copy_nodes 添加第二个参数来修复您的代码一个引用。在您调用 insert 时的代码中来自内部 copy_nodes您没有传递对树的根节点的引用,而是传递对 r 的引用copy_node 的参数.

但我认为有一种更简单(也更有效,不需要重新平衡)的方法来做到这一点。重写 copy_nodes作为返回复制节点的静态方法。

static AVLNode * copyNodes( AVLNode * t)
{
if ( t != NULL )
{
AVLNode* left = copyNodes( t->left );
AVLNode* right = copyNodes( t->right );
return new AVLNode(t->element, left, right, t->height);
}
else
{
return NULL;
}
}

然后你可以像这样在你的复制构造函数中使用这个方法

AVLTree( const AVLTree & rhs )
{
root = copyNodes( rhs.root );
}

赋值运算符也是如此。

关于c++ - 我的 operator= 和复制构造函数在我的类(class)中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13323575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com