2011-10-15 4 views
0

Dans le code suivant, j'obtiens une erreur de compilation: c2227: left of -> left doit pointer vers class/struct/union/type générique. toute aide pour résoudre ce problème; J'essaie d'insérer dans un arbre binaire.insertion d'un arbre binaire

typedef struct bnode{ 
     int key; 
     struct bnode* left; 
     struct bnode* right; 
     }BNODE; 

void printKeysReverse(BNODE* current); 
void inorder(BNODE* current); 
void insert(BNODE **root,int key); 
int main(void){ 
    BNODE* root=NULL; 

    insert(&root,27); 
    insert(&root,59); 
    insert(&root,21); 
    insert(&root,38); 
    insert(&root,54); 
    insert(&root,63); 
    insert(&root,8); 
    insert(&root,70); 
    insert(&root,15); 

} 
void insert(BNODE **root, int val){ 
    BNODE *newnode; 

    newnode=(BNODE*)malloc(sizeof(BNODE));   
    newnode->right=NULL; 
    newnode->left=NULL; 

    if ((*root)==NULL){ 
     *root=newnode; 
     (*root)->key=val;    
     return; 
     } 
    if (val<(*root)->key) insert((&root)->left,val); 
    else insert((&root)->right,val);  
}//end 
+0

Vous fuite de mémoire. Si la fonction choisit de se répéter, elle a déjà attribué un nœud qui ne sera jamais pointé. – wildplasser

Répondre

0

(& racine) -> gauche & racine est pas un bnode *

0
void insert(BNODE **root, int val){ 
BNODE *newnode; 

newnode= malloc(sizeof *newnode);   
newnode->right=NULL; 
newnode->left=NULL; 
newnode->key=val; 

while (*root){ 
    if (val < (*root)->key) root = &(*root)->left; 
    else root = &(*root)->right;  
    } 
*root=newnode; 

return; 


}