5

我正在尝试在 C++ 中创建我的二叉树数据结构的深层副本。问题是我使用的代码似乎只给了我一个浅拷贝(这似乎导致我的解构器出现问题)。

下面的代码是我的二叉树复制构造函数:

BinaryTreeStorage::BinaryTreeStorage(const BinaryTreeStorage &copytree):root(NULL)
{
    root = copytree.root;
    copyTree(root);
}

BinaryTreeStorage::node* BinaryTreeStorage::copyTree(node* other)
{
    //if node is empty (at bottom of binary tree)
    /*
        This creates a shallow copy which in turn causes a problem 
        with the deconstructor, could not work out how to create a 
        deep copy.
    */
    if (other == NULL)
    {
        return NULL;
    }

    node* newNode = new node;

    if (other ->nodeValue == "")
    {
        newNode ->nodeValue = "";
    }

    newNode->left = copyTree(other->left);
    newNode->right = copyTree(other->right); 

    return newNode;
}

任何帮助,将不胜感激。谢谢

这是引发内存异常的解构函数(我相信这是因为我在上面做的浅拷贝)

BinaryTreeStorage::~BinaryTreeStorage(void)
{
    try
    {
        destroy_tree();//Call the destroy tree method
        delete root;//delete final node
    }
    catch(int &error)
    {
        cout << "Error Message : " << error << endl;
    }
}
void BinaryTreeStorage::destroy_tree()
{
    destroy_tree(root);
}
void BinaryTreeStorage::destroy_tree(node *leaf)
{
  if(leaf!=NULL)
  {
    //Recursively work way to bottom node 
    destroy_tree(leaf->left);
    destroy_tree(leaf->right);
    //delete node
    delete leaf;
 }
}
4

2 回答 2

5

您没有执行根节点的深层副本,仅执行其子节点。

不应该是:

root = copyTree(copytree.root);

?

编辑:此外,您破坏了root两次:

destroy_tree();//Call the destroy tree method

//once here
//remove this line
delete root;//delete final node

if(leaf!=NULL)
{
   //Recursively work way to bottom node 
   destroy_tree(leaf->left);
   destroy_tree(leaf->right);

   //one more time here
   delete leaf;
}

如果您只执行其中一项修复,问题将无法解决。

于 2012-05-07T12:22:21.177 回答
1

实际上,我认为我们可以直接使用复制构造函数递归地深度复制树。假设类定义如下:

class TreeNode {
public:
  TreeNode() : value(), count(0), left(nullptr), right(nullptr) {}
  TreeNode(const TreeNode &);

  ~TreeNode();

  TreeNode &operator=(const TreeNode &);

  // Other members...

private:
  std::string value;
  int count;
  TreeNode *left;
  TreeNode *right;
  // Note that the valid state for the `left` and `right` pointer is either
  // `nullptr` or a subtree node. So that we must check these pointers every
  // time before dereferencing them.
};

然后复制构造函数是

TreeNode::TreeNode(const TreeNode &n)
    : value(n.value), count(n.count), left(nullptr), right(nullptr) {
  if (n.left)
    left = new TreeNode(*n.left);  // recursively call copy constructor
  if (n.right)
    right = new TreeNode(*n.right);  // recursively call copy constructor
}

递归将在叶节点处停止,因为它的两个孩子都是nullptr.

析构函数也是如此。

TreeNode::~TreeNode() {
  delete left;  // recursively call destructor on left subtree node
  delete right;  // recursively call destructor on right subtree node
}

leftrightisnullptr时,delete什么都不做,这样递归就会停止。

你可以从这里看到完整的代码。

于 2016-02-05T10:56:40.890 回答