国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

數據結構實驗之二叉樹的建立與遍歷

2019-11-10 16:51:26
字體:
來源:轉載
供稿:網友

PRoblem Description

       已知一個按先序序列輸入的字符序列,如abc,,de,g,,f,,,(其中逗號表示空節點)。請建立二叉樹并按中序和后序方式遍歷二叉樹,最后求出葉子節點個數和二叉樹深度。

Input

輸入一個長度小于50個字符的字符串。

Output

輸出共有4行:第1行輸出中序遍歷序列;第2行輸出后序遍歷序列;第3行輸出葉子節點個數;第4行輸出二叉樹深度。

Example Input

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

Example Output

cbegdfacgefdba35
 
#include<stdio.h>#include<string.h>#include<stdlib.h>typedef struct node{    char data ;    struct node * lc;    struct node * rc;}bitree;int i;int ans;int max;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);    }}void pre_show(bitree * t){    if(t)    {        if(t->lc==NULL&&t->rc==NULL)        {            ans++;        }        pre_show(t->lc);        pre_show(t->rc);    }}void preshow(int count,bitree *t){    int k;    if(t)    {        if(count==0)            count=1;        k=count;        if(k>max)            max=k;        preshow(++count,t->lc);        preshow(++k,t->rc);    }}int main(){    int len;    char str[51];    bitree * tree;    while(scanf("%s",str)!=EOF)    {        i=-1;        ans=0;        max=0;        len=strlen(str);        tree=pre_create(str);        inshow(tree);        printf("/n");        postshow(tree);        printf("/n");        pre_show(tree);        printf("%d/n",ans);        preshow(0,tree);        printf("%d/n",max);    }    return 0;}
上一篇:【Bzoj2733】永無鄉

下一篇:UVA201正方形

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 星子县| 昌平区| 红河县| 文昌市| 札达县| 二手房| 于田县| 香河县| 乌海市| 丹寨县| 莲花县| 扶风县| 太保市| 原阳县| 福安市| 五河县| 新民市| 洛浦县| 泰和县| 辽宁省| 涿鹿县| 沾益县| 漳浦县| 龙井市| 崇阳县| 定西市| 和政县| 宾阳县| 大港区| 格尔木市| 肇州县| 咸宁市| 剑川县| 济宁市| 德安县| 丹巴县| 乌海市| 兰西县| 合江县| 阿拉善盟| 内乡县|