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

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

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

2019-11-10 17:03:15
字體:
來源:轉載
供稿:網友

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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 浮梁县| 南平市| 勃利县| 突泉县| 河北省| 兴国县| 堆龙德庆县| 山丹县| 招远市| 扎囊县| 吴桥县| 蕉岭县| 五华县| 奈曼旗| 临夏市| 梧州市| 苗栗县| 黄平县| 卓尼县| 耿马| 长宁区| 邵东县| 都匀市| 弥勒县| 清原| 彰化市| 宽甸| 青阳县| 南阳市| 浮梁县| 嘉定区| 石棉县| 奈曼旗| 鄱阳县| 连云港市| 游戏| 石柱| 玉龙| 建水县| 福鼎市| 怀远县|