0

I identify a binary tree like this:

class Btree
{
public:
    BtreeNode <T>* root;
    ...
}

Then I write an insert function in the binary tree class like this:

void Btree<T>::InsertNode2(T data, BtreeNode <T>* &root)
{
    if (root==NULL)
    {
        root = new BtreeNode <T> (data);
        //cout << root->data << endl;
        return ;
    }
    if (data <= root->data)
        InsertNode2(data, root->leftchild);
    else
        InsertNode2(data, root->rightchild);
}

When I call this function:

Btree<int> *tree=new Btree<int>();
tree->InsertNode2(1, tree->root);

It's alright. Everything is OK.

But if I write another function to get root:

BtreeNode <T>* GetRoot(){ return this->root; }

When I call InsertNode2:

Btree<int> *tree=new Btree<int>();
tree->InsertNode2(1, tree->GetRoot());

There is an error: initial value of reference to non-const must be an lvalue. What's the difference between the two methods? How to modify it? I want root to be private.

  • The difference is that `tree->root` is the member variable, but `tree->GetRoot()` is a copy of that member variable. – molbdnilo Apr 13 '17 at 12:45

2 Answers2

1

Take your BtreeNode pointer by value not by reference. For example

void Btree<T>::InsertNode2(T data, BtreeNode <T>* root)

This should solve your problem.

PVRT
  • 480
  • 4
  • 13
  • It doesn't work. You can see this:http://stackoverflow.com/questions/43373161/how-to-insert-element-in-binary-tree-by-recursion – user7857293 Apr 13 '17 at 14:36
  • Take a look at this article. It has wonderful explanation on how to build a binary tree [http://www.cprogramming.com/tutorial/lesson18.html] – PVRT Apr 13 '17 at 15:52
  • The return type of `tree->GetRoot()` is an rvalue (in simple words a constant) and it cannot be passed to a function that is taking a reference. – PVRT Apr 13 '17 at 16:11
0

Your InsertNode2 function is a private detail of your tree. Your tree should have a public insertion interface as follows:

class Btree
{
    BtreeNode <T>* root;

public:
    void Insert(const T& data) { InsertNode2(data, root); }

    // ...
};

The tree root should of course also be a private detail of your tree.

Your GetRoot function returns a copy of the root pointer, and of course it makes no sense to try and modify the copy. You probably shouldn't even have a GetRoot function, because see above.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • InsertNode2 function is also public. Everything in the binary tree class is public but the error still occurs. – user7857293 Apr 13 '17 at 12:50
  • @user7857293: careful with the details. I didn't say that `InsertNode2` "has `private` access", I said it "is a private detail". I was talking about the design, not the code details. The code details resulting from the design are that the function *should* have `private` access only, but that's a secondary concern. Understand the design first before you implement. – Kerrek SB Apr 13 '17 at 13:00
  • I have to think for a while-_-!. Thank you for answering. – user7857293 Apr 13 '17 at 13:07