2013-11-01 22 views
1
#include<stdio.h>  
#include<conio.h> 
#include<malloc.h> 
#include<string.h> 

struct node{ 
    char *name; 
    struct node *lchild; 
    struct node *rchild; 
}*root; 


void find(char *str,struct node **par,struct node **loc) 
{ 
    struct node *ptr,*ptrsave; 
    if(root==NULL) 
    { 
     *loc=NULL; 
     *par=NULL; 
     return; 
    } 
    if(!(strcmp(str,root->name))) 
    { 
     *loc=root; 
     *par=NULL; 
     return; 
    } 
    if(strcmp(str,root->name)<0) 
     ptr=root->lchild; 
    else 
     ptr=root->rchild; 
    ptrsave=root; 
    while(ptr!=NULL) 
    { 
     if(!(strcmp(str,ptr->name))) 
     { 
      *loc=ptr; 
      *par=ptrsave; 
      return; 
     } 
     ptrsave=ptr; 
     if(strcmp(str,ptr->name)<0) 
      ptr=ptr->lchild; 
     else 
      ptr=ptr->rchild; 
    } 
    *loc=NULL; 
    *par=ptrsave; 
} 


void insert(char *str) 
{ 
    struct node *parent,*location,*temp; 
    find(str,&parent,&location); 
    if(location!=NULL) 
    { 
     printf("Name already present\n"); 
     return; 
    } 
    temp=(struct node*)malloc(sizeof(struct node)); 
    temp->name=str; 
    temp->lchild=NULL; 
    temp->rchild=NULL; 
    if(parent==NULL) 
     root=temp; 
    else 
     if(strcmp(str,parent->name)<0) 
      parent->lchild=temp; 
     else 
      parent->rchild=temp; 
} 


void displayin(struct node *ptr) 
{ 
    if(root==NULL) 
    { 
     printf("Tree is empty"); 
     return; 
    } 
    if(ptr!=NULL) 
    { 
     displayin(ptr->lchild); 
     printf("%s -> ",ptr->name); 
     displayin(ptr->rchild); 
    } 
} 


int main() 
{ 
    root=NULL; 
    char str[20]; 
    while(1) 
    { 
     printf("Enter name: "); 
     fflush(stdin); 
     gets(str); 
     insert(str); 
     printf("Wants to insert more item: "); 
     if(getchar()=='y') 
     insert(str); 
     else 
     break; 
    } 
    displayin(root); 
    getch(); 
    getchar(); 
    return 0; 
} 

如果我運行這段代碼與下列輸入創建與串的二進制搜索樹

勒凱什 RAJESH BIMAL

然後,它被顯示輸出爲「bimal->」,其是錯的。我不知道邏輯出錯的地方。我進行了檢查,但無法找到錯誤。有人可以看看這個。

+3

如何正常閱讀您的來源與這種可怕的縮進? – P0W

+0

不要刷新'stdin',它是根據C標準的未定義行爲。 'gets'已被棄用(非常非常長),請使用'fgets'來代替。 –

回答

4

問題的一個:

在你insert()功能你正在做

temp=(struct node*)malloc(sizeof(struct node)); 
temp->name=str; //this is not correct, 

//do 
temp=malloc(sizeof(struct node)); // no type cast for malloc 
temp->name = strdup(str);   //allocate memory too 
//also check you NULL and free the allocated memory. 

你只是設置在你的字符串存儲創建的節點的指針位置,但它指向str陣列從main()。因此,所有節點都將指向相同的位置,並輸入最後的值。在你的情況下它的"bimal"

3

您的查找功能在任何情況下都很模糊。這是改進版本。

void find(char *str,struct node **par,struct node **loc) 
{ 
    *par = NULL; 
    *loc = NULL; 
    struct node *ptr,*ptrsave; 
    if(root==NULL) return; 
    if(!(strcmp(str,root->name))) 
    { 
     *loc=root; 
     return; 
    } 
    ptrsave = NULL; 
    ptr = root; 
    while(ptr!=NULL) { 
     if(!(strcmp(str,ptr->name))) break; 
     ptrsave = ptr; 
     if(strcmp(str,ptr->name)<0) 
      ptr=ptr->lchild; 
     else 
      ptr=ptr->rchild; 
    } 
    *loc=ptr; 
    *par=ptrsave; 
}