2016-07-14 80 views
1

我寫了一個C程序來輸入二叉搜索樹的元素並顯示其InOrder,PostOrder和PreOrder遍歷。Inorder,Preorder和Postorder遍歷

#include<stdio.h> 
#include<stdlib.h> 
struct tnode 
{ 
    int data; 
    struct tnode *leftc; 
    struct tnode *rightc; 
}; 
int main() 
{ 
    char ans='N'; 
    struct tnode *new_node,*root; 
    // struct tnode *get_node(); 
    root=NULL; 
    do{ 
     // new_node=get_node(); 
     printf("\nEnter the Element"); 
     scanf("%d",&new_node->data); 
     if(root==NULL) 
      root=new_node; 
     else 
      insert(root,new_node); 
     printf("\nDo you want to enter a new element?(y/n)"); 
     scanf("%c",&ans); 
    }while(ans == 'y'); 
    printf("Inorder traversal:the elements in the tree are"); 
    inorder(root); 
    printf("\nPreorder traversal:the elements in the tree are"); 
    preorder(root); 
    printf("Postorder traversal:the elements in the tree are"); 
    postorder(root); 
    return 0; 
} 
void insert(struct tnode ** tree,int num) 
{ 
    struct tnode *temp = NULL; 
    if(!(*tree)) 
    { 
     temp=(struct tnode *)malloc(sizeof (struct tnode)); 
     temp->leftc=temp->rightc=NULL; 
     temp->data=num; 
     *tree=temp; 
     return; 
    } 
    if(num < (*tree)->data) 
    { 
     insert(&(*tree)->leftc,num); 
    } 
    else if(num > (*tree)->data) 
    { 
     insert(&(*tree)->rightc,num); 
    } 
} 
void preorder(struct tnode * s) 
{ 
    if(s) 
    { 
     printf("%d\n",s->data); 
     preorder(s->leftc); 
     preorder(s->rightc); 
    } 
} 
void inorder(struct tnode * s) 
{ 
    if(s) 
    { 
     inorder(s->leftc); 
     printf("%d\n",s->data); 
     inorder(s->rightc); 
    } 
} 
void postorder(struct tnode * s) 
{ 
    if(s) 
    { 
     postorder(s->leftc); 
     postorder(s->rightc); 
     printf("%d\n",s->data); 
    } 
} 

我得到這些警告消息:

warning: implicit declaration of functionS, 
conflicting types OF FUNCTIONS, 
new_node’ may be used uninitialized in this function 

我無法理解的errors.Can請你幫我解決這些?

+3

請縮進代碼,否則我不看它。 –

+2

沒有看代碼,但關於第一個警告:在C中,您必須在使用therm之前聲明函數。事實上,你必須在使用之前聲明* everything *。至於另一個,你是否在使用它之前初始化'new_node'變量? –

回答

3

在C才能使用的功能,你需要main函數之前聲明EM像你的情況,你應該寫:

void insert(struct tnode ** tree,int num); 
//all declarations of other functions here . 

//順便說一句,你可以聲明不EM變量像這樣的名字:

void insert(struct tnode ** , int); 

也只是嘗試谷歌二進制搜索樹在C。 有許多網站顯示你正在尋找的答案和大量的網站,得到的教程,解釋周圍的一切。

P.S 如果您不想在main函數之前聲明函數,那麼您可以將main函數的上層函數和main函數的上層函數放在最後。

2
  • 您必須聲明或定義函數中使用您使用它們之前。
  • 您對insert()的使用是錯誤的。
  • 使用具有自動存儲持續時間的未初始化變量的值不確定,將調用未定義的行爲。在這種情況下,您不必使用結構來讀取新節點的數據。
  • 你不指望可能會被讀入ans。在格式說明符%c之前添加一個空格,使scanf()在讀取字符前跳過空格字符。
  • 您應該使用main()的標準簽名之一。在C中,int main()int main(void)have different meanings
  • 你應該正確地格式化你的代碼。
  • 他們說you shouldn't cast the result of malloc() in C

試試這個:

#include<stdio.h> 
#include<stdlib.h> 
struct tnode 
{ 
    int data; 
    struct tnode *leftc; 
    struct tnode *rightc; 
}; 
/* declare functions */ 
void insert(struct tnode ** tree,int num); 
void preorder(struct tnode * s); 
void inorder(struct tnode * s); 
void postorder(struct tnode * s); 
/* use one of the standard forms of main() */ 
int main(void) 
{ 
    char ans='N'; 
    struct tnode *root; 
    int new_node_data; 
    // struct tnode *get_node(); 
    root=NULL; 
    do{ 
     // new_node=get_node(); 
     printf("\nEnter the Element"); 
     scanf("%d",&new_node_data); /* do not dereference indeterminate pointer */ 
     insert(&root,new_node_data); /* pass correct data */ 
     printf("\nDo you want to enter a new element?(y/n)"); 
     scanf(" %c",&ans); /* add a space before %c to have scanf() skip whitespace characters */ 
    }while(ans == 'y'); 
    printf("Inorder traversal:the elements in the tree are"); 
    inorder(root); 
    printf("\nPreorder traversal:the elements in the tree are"); 
    preorder(root); 
    printf("Postorder traversal:the elements in the tree are"); 
    postorder(root); 
    return 0; 
} 
void insert(struct tnode ** tree,int num) 
{ 
    struct tnode *temp = NULL; 
    if(!(*tree)) 
    { 
     temp=malloc(sizeof (struct tnode)); 
     temp->leftc=temp->rightc=NULL; 
     temp->data=num; 
     *tree=temp; 
     return; 
    } 
    if(num < (*tree)->data) 
    { 
     insert(&(*tree)->leftc,num); 
    } 
    else if(num > (*tree)->data) 
    { 
     insert(&(*tree)->rightc,num); 
    } 
} 
void preorder(struct tnode * s) 
{ 
    if(s) 
    { 
     printf("%d\n",s->data); 
     preorder(s->leftc); 
     preorder(s->rightc); 
    } 
} 
void inorder(struct tnode * s) 
{ 
    if(s) 
    { 
     inorder(s->leftc); 
     printf("%d\n",s->data); 
     inorder(s->rightc); 
    } 
} 
void postorder(struct tnode * s) 
{ 
    if(s) 
    { 
     postorder(s->leftc); 
     postorder(s->rightc); 
     printf("%d\n",s->data); 
    } 
} 
+0

感謝您的幫助..但在輸出中只有第一個元素打印所有3遍歷 – joejoe

+0

@joejoe [無法重現](http://melpon.org/wandbox/permlink/DeQ813IWJy5Y6Bib)。 – MikeCAT

相關問題