Skip to main content
added 125 characters in body
Source Link
unique_ptr<Node>& right = node->right;
node = move(node->left); // it will destroy the previous node and right nodes
if (right) // right will be nullptr as the right nodes would have been destructed in the previous line
{...}

In your Delete() function, this logic seems to be flawed. When you move your node->left into node, then all the existing right nodes will be destroyed because you just created a reference of unique_ptr which doesn't stop the following move operation from deleting these nodes.

if (right) {...}

So, in effect the above condition will never be true and also, you will lose the right nodes.

The correct way to do this will be to move the right nodes into a local variable something like this :

auto right = std::move(node->right);
node = move(node->left);
if (right)  // Now, right will contain all the right nodes
{...}
unique_ptr<Node>& right = node->right;
node = move(node->left);
if (right) // right will be nullptr as the right nodes would have been destructed
{...}

In your Delete() function, this logic seems to be flawed. When you move your node->left into node, then all the existing right nodes will be destroyed.

if (right) {...}

So, in effect the above condition will never be true and also, you will lose the right nodes.

The correct way to do this will be to move the right nodes into a local variable something like this :

auto right = std::move(node->right);
node = move(node->left);
if (right)  // Now, right will contain all the right nodes
{...}
unique_ptr<Node>& right = node->right;
node = move(node->left); // it will destroy the previous node and right nodes
if (right) // right will be nullptr as the right nodes would have been destructed in the previous line
{...}

In your Delete() function, this logic seems to be flawed. When you move your node->left into node, then all the existing right nodes will be destroyed because you just created a reference of unique_ptr which doesn't stop the following move operation from deleting these nodes.

if (right) {...}

So, in effect the above condition will never be true and also, you will lose the right nodes.

The correct way to do this will be to move the right nodes into a local variable something like this :

auto right = std::move(node->right);
node = move(node->left);
if (right)  // Now, right will contain all the right nodes
{...}
Source Link

unique_ptr<Node>& right = node->right;
node = move(node->left);
if (right) // right will be nullptr as the right nodes would have been destructed
{...}

In your Delete() function, this logic seems to be flawed. When you move your node->left into node, then all the existing right nodes will be destroyed.

if (right) {...}

So, in effect the above condition will never be true and also, you will lose the right nodes.

The correct way to do this will be to move the right nodes into a local variable something like this :

auto right = std::move(node->right);
node = move(node->left);
if (right)  // Now, right will contain all the right nodes
{...}