Assistance needed in implementation of a deep copy constructor.

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

      /// @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)
	  {
		 	
	  }			
	  
	  
	  
	  } 
Last edited on
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!
The copy constructor is recursively called. I need to perform a deep copy, but putting it in a code its quite complicated for me.
What do you mean by "recursively"?
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
Topic archived. No new replies allowed.