注册 登录
编程论坛 数据结构与算法

二叉树的基本操作和应用的问题

坚强的螺丝 发布于 2014-10-22 09:39, 551 次点击
题目1:建立任意二叉树的二叉链表存储,并对其进行先序、中序、后序遍历。
要求:采用二叉链表作为存储结构,以加入虚结点的先序序列输入该二叉树,并设置选单,依据选单项分别输出该二叉树的先序、中序、后序序列。二叉树子结点的数据域可采用字符类型。
测试数据:建立如图所示的二叉树,建立时的输入序列为:ABD000CE00F00
程序代码:
#include<stdio.h>
#include<malloc.h>

typedef struct node
{
    char data;
    struct node * LChild;
    struct node * RChild;
}biTnode,*biTree;

void preorder(biTree root)
{
    if(root!=NULL)
    {
        printf("%c",root->data);
        preorder(root->LChild);
        preorder(root->RChild);
    }
}

void inorder(biTree root)
{
    if(root!=NULL)
    {
        inorder(root->LChild);
        printf("%c",root->data);
        inorder(root->RChild);
    }
}

void postorder(biTree root)
{
    if(root!=NULL)
    {
        postorder(root->LChild);
        postorder(root->RChild);
        printf("%c",root->data);
    }
}

void GreatebiTree(biTree * bt)
{
    char ch;
    ch = getchar();
    if(ch == '#')
        *bt = NULL;
    else
    {
        *bt = (biTree)malloc(sizeof(biTnode));
        (*bt)->data = ch;
        GreatebiTree(&((*bt)->LChild));
        GreatebiTree(&((*bt)->RChild));
    }
}

int main()
{
    biTnode * bt;
    int num;
    printf("please enter this tree:\n");
    GreatebiTree(&bt);
    printf("┌────────────────────┐\n");
    printf("│   please choose one input the tree:    │\n");
    printf("│         1.preorder                     │\n");
    printf("│         2.inorder                      │\n");
    printf("│         3.postorder                    │\n");
    printf("└────────────────────┘\n");
    scanf("%d",&num);
    switch(num)
    {
        case 1:
            preorder(bt);
        case 2:
            inorder(bt);
        case 3:
            postorder(bt);
        default:
            break;
    }
    return 0;
}


编译也没有错,就是结果出不来,求教。

2 回复
#2
azzbcc2014-10-22 11:01

程序代码:
void GreatebiTree(biTree *bt)
{
    char ch;
    ch = getchar();
    if(ch == '0')     // 与输入有关
        *bt = NULL;
    else
    {
        *bt = (biTree)malloc(sizeof(biTnode));
        (*bt)->data = ch;
        GreatebiTree(&((*bt)->LChild));
        GreatebiTree(&((*bt)->RChild));
    }
}



   
程序代码:
// switch 要加 break
    switch (num)
    {
    case 1:
        preorder(bt);
        break;
    case 2:
        inorder(bt);
        break;
    case 3:
        postorder(bt);
        break;
    default:
        break;
    }
#3
坚强的螺丝2014-10-22 12:18
回复 2 楼 azzbcc
谢谢哈!!
1