2012-11-26 49 views
2

我正在嘗試編寫一個遞歸函數以預先輸出值。但是,出於某種原因,它會保持與我的inOrder函數相同的打印效果。 postOrder函數可以正常工作,但是我不得不對這個函數稍微做一些遞歸函數。你們可以看看我的代碼,讓我知道有什麼問題嗎?我真的很感激,因爲這一直給我帶來麻煩。在此先感謝編程二叉樹預訂功能

#include <stdio.h> 
#include <stdlib.h> 
#include <signal.h> 

#define TRUE 1 
#define FALSE 0 

typedef struct bnodestruct 
{ 
    int value; 
    struct bnodestruct *left; 
    struct bnodestruct *right; 
}bnode; 

typedef bnode *bnodePtr; 

void displayMenu(); 
void insert (bnodePtr *hd, int val); 
void inOrder(bnodePtr hd); 
void preOrder(bnodePtr hd); 
void postOrder(bnodePtr hd); 
void empty (bnodePtr *hd); 

int main (int argc, char **argv) 
{ 
    int val; 
    bnodePtr head; 
    head = NULL;  

    char option; 
    int result = 0; 
    while(1) 
    { 
    displayMenu(); 

    printf("Choose an option : "); 
    scanf("\n%c", &option); 

    /*printf("The option chosen is %c\n", option);*/ 

    switch (option) 
    { 

     case 'q': 
      printf("The program is exiting...\n"); 
      exit(-1); 
     case 'i': 
      printf("Enter an integer value to be inserted into the linked list : ");  
      scanf("%d", &val); 
      insert(&head, val); 
      break; 
     case 'o': 
      inOrder(head); 
      break; 
     case 'n': 
      preOrder(head); 
      break; 
     case 'p': 
      postOrder(head); 
      break; 
     case 'e': 
      empty(&head); 
      /*inOrder (head);*/ 
      break; 



    } 

    } 

} 

void displayMenu() 
{ 

    printf("\n   Menu   \n"); 
    printf("(q): quit the program\n"); 
    printf("(i): insert integer value into the binary tree\n"); 
    printf("(e): empty all values from the binary tree\n"); 
    printf("(o): list the items contained in the binary tree in order\n"); 
    printf("(n): list the items contained in the binary tree in pre order\n"); 
    printf("(p): list the items contained in the binary tree in post order\n"); 
} 

void insert (bnodePtr *hd, int val) 
{ 

    if (*hd == NULL) 
    { 
     *hd = (bnodePtr)malloc(sizeof(bnode)); 
     (*hd)->left = NULL; 
     (*hd)->value = val; 
     (*hd)->right = NULL; 
    } 
    else 
    { 
     if (val < ((*hd)->value)) 
      insert (&((*hd)->left), val); 
     else if (val > ((*hd)->value)) 
      insert (&((*hd)->right), val); 
    } 



} 


void empty (bnodePtr *hd) 
{ 

    bnodePtr temp1 = *hd; 
    bnodePtr temp2; 

    while (temp1 != NULL) 
    { 

     temp2 = temp1; 
     temp1 = temp1->left; 

     free (temp2); 

    } 

    *hd = NULL; 

} 


void inOrder (bnodePtr hd) 
{ 

    if (hd != NULL) 
    { 


     inOrder(hd->left); 
     printf("%d\n", hd->value); 
     inOrder(hd->right); 
    } 



} 

void preOrder (bnodePtr hd) 
{ 

    if (hd != NULL) 
    { 


     printf("%d\n", hd->value); 
     preOrder(hd->left); 
     preOrder(hd->right); 
    } 


} 

void postOrder (bnodePtr hd) 
{ 

    if (hd != NULL) 
    { 


     inOrder(hd->left); 
     inOrder(hd->right); 
     printf("%d\n", hd->value); 

    } 



} 
+0

有趣。看起來沒問題。 –

+0

你已經發布了*方式*太多的代碼。你的目標應該是創建一個*最小*示例程序來展示你的問題 - 而不僅僅是因爲這對潛在的回答者有禮貌。發現/創建這樣一個程序也是一個縮小問題範圍的好方法,以防萬一它不在你的想法之中。 (例如,你使用'scanf'來讀取單個字符是很麻煩的。) – ruakh

+0

好的,除了scanf部分,你會提供什麼幫助嗎?我發佈整個代碼的原因是因爲程序中的其他地方可能存在一個錯誤,即某人可能能夠檢測到使用preOrder函數來診斷我遇到的問題。而且,現在我也認爲我的postOrder函數也是搞砸了。 –

回答

3
void postOrder (bnodePtr hd) 
{ 
    if (hd != NULL) 
    { 
     inOrder(hd->left); // XXX 
     inOrder(hd->right); // XXX 
     printf("%d\n", hd->value); 
    } 
} 

那是你的問題就在這裏。你打電話給inOrder,你應該打電話給postOrder

+0

沒有那個問題,但實際上我不確定。因爲我最初試圖按照你所說的,通過遞歸調用postOrder函數中的postOrder,但由於某種原因,它不起作用。它只是按降序排列列表。所以我在網上搜索,一個代碼說要做的就是調用你在postOrder函數中按順序打印列表的函數。我的postOrder函數打印出來,但我不認爲它可能是正確的。 –

+0

我不知道你剛剛說了什麼。 – melpomene

+0

我只是說我在postOrder函數中調用inOrder的唯一原因是因爲某些原因,當我試圖在postOrder函數中遞歸調用postOrder時,什麼也沒有發生。但在網上查看後,我看到有人在他們的postOrder函數中調用了inOrder。即使我不認爲這是正確的,該計劃至少打印出一些東西。遞歸調用postOrder對我完全沒有任何幫助。 –