Im trying to create a Binary search tree. I've used recursive procedures to insert nodes into the tree. The code is as follows.
void BST :: insertRoot(Node* node, int data) {
if (node == NULL)
this -> root = new Node(data);
else
insertOthers(node, data);
}
void BST :: insertOthers(Node* node, int data) {
if(node == NULL) {
node = new Node(data);
return;
}
if(data < node->getData())
insertOthers(node->getLeft(), data);
else
insertOthers(node->getRight(), data);
}
In this code only one node is inserted into the tree and then the connection is broken. However i when I change my Node*
to Node*&
it works perfectly. Still i cant understand whats the difference between these two. Can anyone explain the differentiation between these two with their memory mapping? Thank you