I'm working on a function that inserts a value in a binary search tree, I wrote this code:
// this is my decalarations if its relevent
typedef struct element* tree;
typedef struct element
{
type data;
tree right;
tree left;
} noed;
tree rightSon(tree head)
{
return (head->right);
}
void insert(tree* a,int val)
{
if(!empty(*a)) {
if((*a)->data>val) {
if(!empty(leftSon(*a)))
insert(&leftSon(*a),val); // error here leftson return a tree
else {
(*a)->left=newNoed(val);
}
}
else if((*a)->data<val) {
if(!empty(rightSon(*a)))
insert(&rightSon(*a),val); //same error here
else
(*a)->right=newNoed(val);
}
else printf("value already exist!\n");
}
else {
*a=newNoed(val);
}
}
I solved the problem by declaring local variables of type tree then assigning the values to them like this:
tree lson;
lson=leftson(*a);
insert(&lson,val);
But I still don't get what was the problem in the first place.