【问题标题】:Programming binary tree preOrder function编程二叉树 preOrder 函数
【发布时间】:2012-11-26 02:06:07
【问题描述】:

我正在尝试编写一个递归函数以预先打印出值。但是,由于某种原因,它一直打印出与我的 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);

    }



}

【问题讨论】:

  • 有趣。我觉得没问题。
  • 你发布了太多方式的代码。您的目标应该是创建一个 minimal 示例程序来演示您的问题——而不仅仅是因为这对潜在的回答者有礼貌。发现/创建这样的程序也是缩小问题所在的好方法,以防它不是你想的。 (例如,您使用scanf 读取单个字符是自找麻烦。)
  • 好的,但是除了 scanf 部分,你会提供任何帮助吗?我发布整个代码的原因是因为程序中的其他地方可能存在错误,有人可能能够检测到以诊断我在 preOrder 函数中遇到的问题。而且,现在我也认为我的 postOrder 函数也搞砸了
  • 为了记录,我从preOrderinOrder 得到不同的输出。当然,对于某些退化的树,两者都会打印相同的内容。您是否可能按升序插入值?那会产生这样一棵退化的树。

标签: c tree binary-tree preorder


【解决方案1】:
void postOrder (bnodePtr hd)
{
    if (hd != NULL)
    {
        inOrder(hd->left);  // XXX
        inOrder(hd->right); // XXX
        printf("%d\n", hd->value);
    }
}

那是你的问题。你打电话给inOrder,你应该打电话给postOrder

【讨论】:

  • 知道这实际上不是问题,实际上我不确定。因为我最初尝试按照您所说的那样通过在 postOrder 函数中递归调用 postOrder 来执行此操作,但由于某种原因,它不起作用。它只是按降序打印列表。所以我在网上搜索,一个代码要做的是调用你用来在你的 postOrder 函数中“按顺序”打印列表的函数。我的 postOrder 函数打印出来了,但我认为它也可能不正确。
  • 我不知道你刚才说了什么。
  • 我只是说我在 postOrder 函数中调用 inOrder 的唯一原因是因为某种原因,当我尝试在 postOrder 函数中递归调用 postOrder 时,什么也没发生。但是在网上看了之后,我看到有人在他们的 postOrder 函数中调用了 inOrder。即使我认为它不正确,该程序至少会打印出一些东西。递归调用 postOrder 对我没有任何作用。
猜你喜欢
  • 2011-09-08
  • 1970-01-01
  • 1970-01-01
  • 2012-09-17
  • 1970-01-01
  • 2020-02-20
  • 2015-10-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多