Below is my code for BST insertion. I want to use void return type for insert, instead of the regular way of using 'struct node*' as its return type. I am unable to find an error or logical error in this. Can someone explain in detail as to why isn't my code working?
#include<iostream>
using namespace std;
struct node
{
int val;
node *left, *right;
};
void ins(int key, node *nroot)
{
if(nroot == NULL)
{
node *temp= new node;
temp->val=key;
temp->left=NULL;
temp->right=NULL;
nroot=temp;
}
else if(key > nroot->val)
{
ins(key, nroot->right);
}
else
{
ins(key, nroot->left);
}
}
void print(node *nroot)
{
if(nroot!=NULL)
{
print(nroot->left);
cout<<nroot->val;
print(nroot->right);
}
}
main()
{
int n;
cin>>n;
node *root= new node;
int x;
cin>>x;
root->left=NULL;
root->right=NULL;
root->val=x;
for(int i=1;i<n;i++)
{
cin>>x;
ins(x, root);
}
print(root);
}