Devo codificare alcuni metodi per un BST e ho alcuni problemi, lasciatemi spiegare.
Ho le seguenti strutture:
struct node {
struct node *lChild;
struct node *rChild;
int value;
};
e
struct tree {
struct node *root;
};
insieme alle seguenti funzioni:
struct tree* constructNewTree()
{
struct tree *T=malloc(sizeof(struct tree));
T->root=NULL;
return T;
}
e
struct node* constructNewNode(int i)
{
struct node *N=malloc(sizeof(struct node));
N->value=i;
N->lChild=NULL;
N->rChild=NULL;
return N;
}
E nel mio principale devo chiamare questo (per esempio):
int main()
{
struct tree *T;
T=constructNewTree();
insertKey(5,T);
insertKey(2,T);
insertKey(9,T);
return 0;
}
Quello che devo fare è quello di creare la funzione insertKey (INT, struct albero * T) usando la ricorsione.
Volevo fare qualcosa di simile
void insertKey(int i, struct tree *T)
{
if (T->root==NULL) {
T->root=constructNewNode(i);
return;
}
else {
if (i<=T->root->value) {
T->root->lChild=constructNewNode(i);
else if (i>T->root->value) {
T->root->rChild=constructNewNode(i);
}
}
}
Ma non molto lontano, utilizzando la ricorsione mi avrebbe permesso di chiamare di nuovo insertKey ma io non riesco a utilizzare un nodo e un albero allo stesso modo.
Qualcuno sa come potrei farlo senza alterare le strutture date?
Grazie mille.













