简单二叉树操作

二叉树的先序、中序、后序操作

#include <stdio.h>
#include <malloc.h>
typedef struct Tree
{
    char a;
    struct Tree * LeftTree;
    struct Tree * RigthTree;
}T;
T * Creat();   //生成一个二叉树
void PreShowTree(T * pT);       //先序访问
void InShowTree(T * pT);        //中序访问
void PostShowTree(T * pT);      //后序访问
int main()
{
    T * pT = Creat();
    
    printf("先序遍历:\n");
    PreShowTree(pT);
    printf("中序遍历:\n");
    InShowTree(pT);
    printf("后序遍历:\n");
    PostShowTree(pT);
    
    return 0;
}
void PreShowTree(T * pT)
{
    if( NULL != pT )
    {
        printf("%c\n", pT->a);
        if( NULL != pT->LeftTree )
            PreShowTree( pT->LeftTree );
        if( NULL != pT->RigthTree )
            PreShowTree( pT->RigthTree );
    }
}
void InShowTree(T * pT)
{
    if( NULL != pT )
    {
        if( NULL != pT->LeftTree)
            InShowTree( pT->LeftTree );
        printf("%c\n", pT->a);
        if( NULL != pT->RigthTree)
            InShowTree( pT->RigthTree);
    }
}
void PostShowTree(T * pT)
{
    if( NULL != pT )
    {
        if( NULL != pT->LeftTree)
            PostShowTree( pT->LeftTree );
        if( NULL != pT->RigthTree)
            PostShowTree( pT->RigthTree);
        printf("%c\n", pT->a);
    }
}


T * Creat()
{
    T * pA = (T *)malloc(sizeof(T));
    T * pB = (T *)malloc(sizeof(T));
    T * pC = (T *)malloc(sizeof(T));
    T * pD = (T *)malloc(sizeof(T));
    T * pE = (T *)malloc(sizeof(T));
    
    pA->a = 'A';
    pB->a = 'B';
    pC->a = 'C';
    pD->a = 'D';
    pE->a = 'E';
    
    pA->LeftTree = pB;
    pA->RigthTree = pC;
    pB->LeftTree = pB->RigthTree = NULL;
    pC->LeftTree = pD;
    pC->RigthTree = NULL;
    pD->LeftTree = NULL;
    pD->RigthTree = pE;
    pE->LeftTree = pE->RigthTree = NULL;
    
    return pA;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值