I initialized a test case as a global variable, here:
void InsertNode(BSTNode* &t, const int &key) {
if (t == NULL) {
t = new BSTNode;
t->key = key;
t->left = t->right = NULL;
} else {
if (key != t->key) {
if (key < t->key)
InsertNode(t->left, key);
else
InsertNode(t->right, key);
}
}
}
BSTNode t1[] = {
{4, &t1[1], &t1[2]},
{2, &t1[3], &t1[4]},
{6, &t1[5], &t1[6]},
{1, NULL, NULL},
{3, NULL, NULL},
{5, NULL, NULL},
{7, NULL, NULL}
};
int main() {
InsertNode(t1, 0);
return 0;
}
However, when I try to modify t1, it gives me an error:
invalid initialization of non-const reference of type 'BSTNode*&' from a temporary of type 'BSTNode*'
Could someone explain this for me? Thank you!!