I am trying to overload the assignment operator for my Binary Search Tree.
Example: tree1 = tree2
I want to delete all the nodes in tree1 and make deep copy of all nodes in tree.
I already have function:
Node* deepCopyTree(const Node *source)
which works perfectly well. I created also this function:
void deleteTree(Node* root)
{
if (root)
{
deleteTree(root->left);
deleteTree(root->right);
delete root;
}
}
which as I see my debugging it works. The operator overload function :
BST& BST::operator=(const BST &rhs)
{
DestroyRecursive(root);
deepCopyTree(rhs.root);
return *this;
}
And that throws me an error when copying.. I am working from 10 hours on that and this is the smallest thing I have left with and I want to finish it. Please help :) .
That is my deep copy constructor:
BST::BST(const bST&rhs)
:root(deepCopyTree(rhs.root))
{
}
deepCopyTree returns Node*
struct Node
{
std::string value = "";
Node *left = nullptr;
Node *right = nullptr;
};
Deconstructor:
BST::~BST()
{
DeleteTree(this->root);
}
void DeleteTree(Node* root)
{
if (root)
{
DeleteTree(root->left);
DeleteTree(root->right);
delete root;
}
}