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

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

數據結構實驗之二叉樹五:層序遍歷

2019-11-10 20:24:53
字體:
來源:轉載
供稿:網友

PRoblem Description

已知一個按先序輸入的字符序列,如abd,,eg,,,cf,,,(其中,表示空結點)。請建立二叉樹并求二叉樹的層次遍歷序列。

Input

輸入數據有多行,第一行是一個整數t (t<1000),代表有t行測試數據。每行是一個長度小于50個字符的字符串。

Output

輸出二叉樹的層次遍歷序列。

Example Input

2abd,,eg,,,cf,,,xnl,,i,,u,,

Example Output

abcdefgxnuli
 
#include<stdio.h>#include<string.h>#include<stdlib.h>#define maxsize 50typedef struct node{    char data;    struct node *lc,*rc;}bitree;bitree *queue[51];int front=0,rear=0;int i=-1;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 enter_queue(bitree * t){    if((rear+1)%maxsize!=front)    {        rear=(rear+1)%maxsize;        queue[rear]=t;    }}bitree * delete_queue(bitree * t){    if(rear!=front)    {        front=(front+1)%maxsize;        return queue[front];    }}void level_order(bitree * t){    bitree * p;    if(t)    {        enter_queue(t);    }    while(front!=rear)    {        p=delete_queue(t);        printf("%c",p->data);        if(p->lc)        {            enter_queue(p->lc);        }        if(p->rc)        {            enter_queue(p->rc);        }    }}int main(){    int t;    char str[51];    bitree *tree;    scanf("%d",&t);    while(t--)    {        i=-1;        scanf("%s",str);        tree=pre_create(str);        level_order(tree);        printf("/n");    }    return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 淮北市| 忻城县| 崇文区| 榆树市| 新兴县| 威信县| 隆林| 扶风县| 留坝县| 上林县| 山丹县| 收藏| 依兰县| 清徐县| 集安市| 大兴区| 嘉鱼县| 嘉善县| 盘锦市| 北辰区| 和政县| 连江县| 盐池县| 辰溪县| 万全县| 浮梁县| 衡东县| 万山特区| 尤溪县| 海城市| 宝丰县| 河池市| 浦江县| 土默特左旗| 临夏市| 邮箱| 九江县| 汨罗市| 抚顺市| 宁海县| 高青县|