0

I am working with a binary search tree. Here I'm writing a function to delete an item from the tree. In the following code:

if(root = NULL)//if there is nothing in the tree
{
    cout<<"the Tree is empty"<<endl;//ouput to the screen
    return;//exit the function
}

bool isFound = false;//tells us if the item is found
Node* tmp = new Node();//declare a temp pointer
Node* tmp2 = new Node();;//declare a temp pointer
tmp* = *root;//assign the pointer to something

It is calling the copy constructor, but as I have it right now I'm just copying the values like this:

Node& Node::operator= (const Node& node)
{
    data = node.data;
    left = node.left;
    right = node.right;
    return *this;
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

2 Answers2

1

You're assigning pointers, to assign the objects you need

*tmp = *root;

tmp and root are of type Node*; *tmp and *root are of type Node.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • will i need to write a copy constructor then? –  Mar 13 '13 at 11:39
  • @TravisLeonSorensen are you managing resources? Is ownership of the resources defined? (It's not a questions I can answer only with that information) – Luchian Grigore Mar 13 '13 at 11:39
  • @TravisLeonSorensen better read - http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three – Luchian Grigore Mar 13 '13 at 11:41
  • okay so when i add that it goes to the copy constructor. will i need to just copy everything from the root to tmp? and what do you mean by resources? –  Mar 13 '13 at 11:42
  • @TravisLeonSorensen as I said, it's not something that can be answered in a comment box - read the question and answers in the link I provided & try to understand them. – Luchian Grigore Mar 13 '13 at 11:43
0
if(root = NULL)

change it to

if(root == NULL)

it is also wrong:

tmp* = *root;//assign the pointer to something
4pie0
  • 29,204
  • 9
  • 82
  • 118