二进制搜索树深层复制和解除引用

时间:2018-04-12 12:28:06

标签: c++ binary-search-tree deep-copy

我正在尝试为二叉搜索树设置深层复制构造函数,但似乎无法弄清楚如何处理指针的解除引用。我对C ++很陌生,并开始掌握它是如何工作的,但这让我感到震惊。

代码如下:

<!doctype html>
<html lang='en'>

<head>
  <meta charset='utf-8'>
  <title>Test</title>
  <link rel='stylesheet' href='style.css'>
</head>

<body>
  <header>
    Page header
  </header>
  <section id='slides'>
    <article class='slide'>
      <img src='//thimbleprojects.org/nclm/457411/image_1.png' alt='Description'>
    </article>
    <article class='slide'>
      <img src='//thimbleprojects.org/nclm/457411/image_2.png' alt='Description'>
    </article>
    <article class='slide'>
      <img src='//thimbleprojects.org/nclm/457411/image_1.png' alt='Description'>
    </article>
    <article class='slide'>
      <img src='//thimbleprojects.org/nclm/457411/image_2.png' alt='Description'>
    </article>
    <article class='slide'>
      <img src='//thimbleprojects.org/nclm/457411/image_1.png' alt='Description'>
    </article>
    <article class='slide'>
      <img src='//thimbleprojects.org/nclm/457411/image_2.png' alt='Description'>
    </article>
  </section>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/scrollify/1.0.19/jquery.scrollify.min.js"></script>
</body>

</html>

.h文件中二进制搜索树的实现如下所示。

void copyTree_helper(Node **destination,const Node *source)
{
    {
        if(source == NULL)
        {
            (*destination) = NULL;
        }
        else
        {
            (*destination) = new Node;
            (*destination)->data = source->data;
            copyTree_helper(&(*destination)->left, source->left);
            copyTree_helper(&(*destination)->right, source->right);
        }
    }
}

// Creates a binary tree by copying an existing tree
BinarySearchTree::BinarySearchTree(const BinarySearchTree &rhs)
{

    if(&rhs == nullptr)
        root = nullptr;
    else
        copyTree_helper(&(*root), &rhs);

    /*////////////////////////////////////////////
                 Needs implementation
    ////////////////////////////////////////////*/
}

现在它不会使用以下错误消息进行编译:

struct Node
{
    // Data stored in this node of the tree
    std::string data;
    // The left branch of the tree
    Node *left = nullptr;
    // The right branch of the tree
    Node *right = nullptr;
};

非常感谢任何有助于我全力以赴的帮助或解释。 干杯!

1 个答案:

答案 0 :(得分:0)

问题是您应该通过&root,而不是&*root - *rootNode,而不是Node*

这在功能上写得更好,但是:

Node* copyTree_helper(const Node *source)
{
    if(source == nullptr)
    {
        return nullptr;
    }
    Node* result = new Node;
    result->data = source->data;
    result->left = copyTree_helper(source->left);
    result->right = copyTree_helper(source->right);
    return result;
}

BinarySearchTree::BinarySearchTree(const BinarySearchTree &rhs)
    : root(copyTree_helper(rhs.root))
{
}

或者,如果您向Node添加合适的构造函数:

Node* copyTree_helper(const Node *source)
{
    return source == nullptr
          ? nullptr
          : new Node(source->data, 
                     copyTree_helper(source->left), 
                     copyTree_helper(source->right));
}