二叉树插入

时间:2011-10-15 17:28:52

标签: c

在下面的代码中,我收到编译错误:c2227:left of - > left必须指向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 

2 个答案:

答案 0 :(得分:0)

(&安培;根) - &GT;左 &amp; root不是BNODE *

答案 1 :(得分: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;


}
相关问题