创建一个二叉树,并对其进行先序,中序,后序遍历

#include <stdio.h>

#include <stdlib.h>

#include <malloc.h>

typedef struct BiTNode

{

    char data;

    struct BiTNode *lchild, *rchild;

} *BiTree;

typedef struct QNode

{

    BiTree data;

    struct QNode *next;

} QNode, *QueuePtr;

typedef struct

{

    QueuePtr front;

    QueuePtr rear;

} LinkQueue;

void InitQueue(LinkQueue *Q)

{

    Q->front = (QueuePtr)malloc(sizeof(QNode));

    if (Q->front == NULL)

        exit(EXIT_FAILURE);

    Q->rear = Q->front;

    Q->front->next = NULL;

} // 初始化队列

void EnQueue(LinkQueue *Q, BiTree T)

{

    QueuePtr p = (QueuePtr)malloc(sizeof(QNode));

    p->data = T;

    p->next = NULL;

    Q->rear->next = p;

    Q->rear = p;

} // 入队列

BiTree DeQueue(LinkQueue *Q)

{

    if (Q->front == Q->rear)

        return 0;

    QueuePtr p = Q->front->next;

    Q->front->next = p->next;

    BiTree e = p->data;

    if (Q->rear == p)

        Q->rear = Q->front;

    free(p);

    return e;

} // 出队列

BiTree creat(BiTree p)

{

    int i, m;

    BiTree root, q, T;

    LinkQueue *Q;

    Q=(LinkQueue*)malloc(sizeof(LinkQueue));

    InitQueue(Q);

    if (p == NULL)

    {

        return NULL;

    }

    else

    {

        root = (BiTree)malloc(sizeof(struct BiTNode));

        T = root;

        T->lchild = NULL;

        T->rchild = NULL;

        printf("请输入根节点的data域");

        scanf(" %c", &(root->data)); // 添加空格以消耗可能的换行符

        printf("Please input the number of BiTnodes for the binary tree \n m=");

        scanf("%d", &m);

        for (i = 0; i < m - 1; i++)

        {

            q = (BiTree)malloc(sizeof(struct BiTNode));

            getchar(); // 消耗换行符或其他无用字符

            printf("请输入第 %d 个节点的data域", i + 2);

            scanf(" %c", &(q->data)); // 添加空格以消耗可能的换行符

            EnQueue(Q, q);

            q->lchild = NULL;

            q->rchild = NULL;

            if (T->lchild == NULL)

            {

                T->lchild = q;

            }

            else if (T->rchild == NULL)

            {

                T->rchild = q;

                T = DeQueue(Q); // 移动到下一层

            }

        }

    }

    p = root;

    return p;

}

// 创造一个二叉树

void PreOrder(BiTree T)

{

    if (T != NULL)

    {

        printf("%c", T->data);

        PreOrder(T->lchild);

        PreOrder(T->rchild);

    }

} // 先序遍历

void InOrder(BiTree T)

{

    if (T != NULL)

    {

        InOrder(T->lchild);

        printf("%c", T->data);

        InOrder(T->rchild);

    }

} // 中序遍历

void PostOrder(BiTree T)

{

    if (T != NULL)

    {

        PostOrder(T->lchild);

        PostOrder(T->rchild);

        printf("%c", T->data);

    }

} // 后序遍历

int main()

{

    BiTree root, q;

    struct BiTNode n;

    LinkQueue *Q;

    BiTree creat(BiTree p);

    void PreOrder(BiTree T);

    void InOrder(BiTree T);

    void PostOrder(BiTree T);

    void InitQueue(LinkQueue * Q);

    void EnQueue(LinkQueue * Q, BiTree T);

    BiTree DeQueue(LinkQueue * Q);

    int t;

    q = &n;

    root = creat(q);

    printf("At the first,we creat a tree\n");

    if (root == NULL)

        printf("It is an empty tree!\n");

    else

    {

        printf("\n1.The preordertraverse\n");

        printf("\n2.The inordertraverse\n");

        printf("\n3.The postordertraverse\n");

        printf("please choose a kind of order\n");

        scanf("%d", &t);

        switch (t)

        {

        case 1:

            PreOrder(root);

            break;

        case 2:

            InOrder(root);

            break;

        case 3:

            PostOrder(root);

            break;

        default:

            printf("The error!");

        }

    }

    return 0;

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值