Hello,
We were instructed to do in our programming assignment to implement the copy constructor below. However I am struggling on it. Our instructor gave us the two if statements below, and this as well "
new N -> parent = this;
this -> left = new N;
"
Which I have no idea of how to use that to help me in my if statements.
Any help is appreciated.
Also this is for map.h, Binary tree.
/// @brief Copy constructor
/// @param n node to perform deep copy from
node(const node& n) : value(n.value), parent(nullptr), left(nullptr), right(nullptr), height(n.height) {
/// @todo Finish implementation of this copy constructor.
/// Hint: left and right are not copied correctly at the moment
new N -> parent = this;
this -> left = new N;
if (n.left)
{
}
if(n.right)
{
}
}
Can you explain what the copy should do at logical level?
You have a binary tree.
If the constructed node is exact copy of the template, then it has the same parent and same children as the template. That, obviously is not acceptable.
If the node is not made part of a tree, who will point to it? Will it be a root of a new tree?
Or perhaps the "copy" is actually a way to insert a new node into the tree at particular position? Would that not change the template node too? Alas, it is const!
Recursion occurs when a function calls itself directly or indirectly. We can use the same copy constructor recursively to create the copies of left and right subtrees of a node