2011-10-15 96 views
0

在下面的代碼中,我得到編譯錯誤:c2227:左側 - >左側必須指向class/struct/union/generic類型。任何幫助如何解決這個問題;我試圖插入二叉樹。二叉樹插入

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

您正在泄漏內存。如果函數選擇遞歸,它已經分配了一個永遠不會指向的節點。 – wildplasser

回答

0

(&根) - >離開 &根本不是一個B節點*

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; 


}