c - 编程二叉树 preOrder 函数

标签 c tree binary-tree preorder

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

    }



}

最佳答案

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

这就是你的问题所在。您正在调用 inOrder,而您应该调用 postOrder

关于c - 编程二叉树 preOrder 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13557727/

相关文章:

c - 程序的汇编和执行 - 两遍汇编器

c++ - 寻找 OpenCV 教程

gwt - 以编程方式刷新 Gwt CellTree

mysql - innodb b树中的内部节点如何物理存储?

c -/n C 中的换行错误

c - 警告 : format argument is not a pointer (arg 2)?

data-structures - "Order"和 "Degree"在Tree数据结构上有什么区别

c++ - std::map 获取最低的 n 个元素时间

Python:TypeError 无法连接 'str and ' 列表'对象

java - 简单二叉树的 Add 方法出现莫名其妙的问题