首页 > 学院 > 开发设计 > 正文

数据结构实验之二叉树二:遍历二叉树

2019-11-11 04:16:15
字体:
来源:转载
供稿:网友

PRoblem Description

已知二叉树的一个按先序遍历输入的字符序列,如abc,,de,g,,f,,, (其中,表示空结点)。请建立二叉树并按中序和后序的方式遍历该二叉树。

Input

连续输入多组数据,每组数据输入一个长度小于50个字符的字符串。

Output

每组输入数据对应输出2行:第1行输出中序遍历序列;第2行输出后序遍历序列。

Example Input

abc,,de,g,,f,,,

Example Output

cbegdfacgefdba
 
#include<stdio.h>#include<string.h>#include<stdlib.h>typedef struct node{    char data ;    struct node * lc;    struct node * rc;}bitree;int i;bitree * pre_create(char str[51]){    bitree * t;    if(str[++i]!=',')    {        t=(bitree *)malloc(sizeof(bitree));        t->data=str[i];        t->lc=pre_create(str);        t->rc=pre_create(str);    }    else    {        t=NULL;    }    return t;}void inshow(bitree * tree){    bitree * t;    t=tree;    if(t!=NULL)    {        inshow(t->lc);        printf("%c",t->data);        inshow(t->rc);    }}void postshow(bitree * tree){    bitree * t;    t=tree;    if(t!=NULL)    {        postshow(t->lc);        postshow(t->rc);        printf("%c",t->data);    }}int main(){    int len;    char str[51];    bitree * tree;    while(scanf("%s",str)!=EOF)    {        i=-1;        len=strlen(str);        tree=pre_create(str);        inshow(tree);        printf("/n");        postshow(tree);        printf("/n");    }    return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表