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.