网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

二叉树的创建和遍历

#include
#include
typedef struct Tree{
    char data;
    struct Tree *lchild;
    struct Tree *rchild;
}BTree;
void createTree(BTree **t)
{
    char c;
    scanf("%c",&c);
    if(c==' ')
      *t=NULL;
    else if(c!='\0'&&c!='\x0a'){
        if(!(*t=(BTree *)malloc(sizeof(BTree))))
          exit(0);
        (*t)->data=c;
        createTree(&((*t)->lchild));
        createTree(&((*t)->rchild));
    }
}
//先序遍历
void preorderTraverse(BTree *t)
{
    if(t!=NULL){
        printf("%c",t->data);
        if(t->lchild!=NULL)
          preorderTraverse(t->lchild);
        else
          printf("*");
        if(t->rchild!=NULL)
          preorderTraverse(t->rchild);
        else
          printf("*");
    }else
      printf("Thia tree is null\n");
}
//中序遍历
void inorderTraverse(BTree *t)
{
    if(t!=NULL){
        if(t->lchild!=NULL)
        inorderTraverse(t->lchild);
        else
          printf("*");
        printf("%c",t->data);
        if(t->rchild!=NULL)
          inorderTraverse(t->rchild);
        else
          printf("*");
    }else
      printf("Thia tree is null\n");
}
//后序遍历
void postorderTraverse(BTree *t)
{
    if(t!=NULL){
        if(t->lchild!=NULL)
          postorderTraverse(t->lchild);
        else
          printf("*");
        if(t->rchild!=NULL)
          postorderTraverse(t->rchild);
        else
          printf("*");
        printf("%c",t->data);
    }else
      printf("Thia tree is null\n");
}
int main()
{
    BTree *t=NULL;
    createTree(&t);
    printf("start treverse\n");
    preorderTraverse(t);
    printf("\n");
    inorderTraverse(t);
    printf("\n");
    postorderTraverse(t);
    printf("\n");
    return 0;
}

网页标题:二叉树的创建和遍历
链接分享:http://bjjierui.cn/article/gjjpoi.html

其他资讯