Hello, I'm kind of new in C++ programming and I'm starting with data structures (in this case binary trees). But when I read information from console I keep getting an error and I don't really know why. I'd apreciate your help, thank you very much.
void insert_node(struct node **root, int n)
{
int h;
int i = 0;
node *q;
//q = new node; -> you don't need this
(*root) -> value = n;
(*root)->right = 0;
(*root)->left = 0;
//q = *root; -> q must start from root every insertion
cout<<"Insert number of nodes: "<<endl;
cin>>n;
//cout<<"Insert number: "<<endl;
//cin>>h;
while(i < n){
cout<<"Insert number: "<<endl;
cin>>h;
// here's q
q = *root;
do{
// beware the NULL pointer !
if(q->value>h)
if(q->left == NULL) break;
else q = q->left;
elseif(q->value<h)
if(q->right == NULL) break;
else q = q->right;
}while(q!=0 && q->value!=h);
if(q->value == h)
cout<<"Repeated number"<<endl;
elseif(h < q->value)
ins_left(q,h);
else
ins_right(q,h);
i++; //How you forget this!
}
}
In fact, you don't have to use void insert_node(struct node **root, int n).
Just make it more complicated. You can use just void insert_node(struct node *root, int n), and in main just insert_node(root, n);
Btw, nothing wrong with cin. It's all because if your null pointer is processed.