数据结构实验之二叉树五:层序遍历
生活随笔
收集整理的這篇文章主要介紹了
数据结构实验之二叉树五:层序遍历
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目描述
已知一個(gè)按先序輸入的字符序列,如abd,,eg,,,cf,,,(其中,表示空結(jié)點(diǎn))。請(qǐng)建立二叉樹并求二叉樹的層次遍歷序列。
輸入
?輸入數(shù)據(jù)有多行,第一行是一個(gè)整數(shù)t?(t<1000),代表有t行測試數(shù)據(jù)。每行是一個(gè)長度小于50個(gè)字符的字符串。輸出
?輸出二叉樹的層次遍歷序列。示例輸入
2 abd,,eg,,,cf,,, xnl,,i,,u,,示例輸出
abcdefg xnuli #include <stdio.h> #include <stdlib.h> #include <string.h> typedef char telemtype; typedef char status; typedef struct BiTNode {telemtype data;struct BiTNode *lchild, *rchild; }*BiTree; typedef BiTree QElemType;//注意此類型應(yīng)定義為BiTreetypedef char Status;typedef struct QNode;{QElemType data;QNode *next;} QNode, *Queueptr; typedef struct//定義隊(duì)列{Queueptr ?front;Queueptr ?rear;} LinkQueue; char str[55]; int i; status create(BiTree &T)//生成樹; {if(str[i++]==',') T=NULL;else{T = (BiTNode *) malloc (sizeof(BiTNode));if(!T) exit(0);T->data = str[i-1];create(T->lchild);create(T->rchild);}return 1; } Status InitQueue (LinkQueue &Q)//隊(duì)的初始化;‘{Q.front = Q.rear = (Queueptr)malloc(sizeof(QNode));if (!Q.front) exit (0);Q.front->next = NULL;return 1;}Status EnQueue (LinkQueue &Q, QElemType e)//進(jìn)隊(duì);{Queueptr p;p = (Queueptr) malloc (sizeof (QNode));if (!p) ?exit (0);p->data = e;p->next = NULL;Q.rear->next = p;Q.rear = p;return 1;}Status DeQueue (LinkQueue &Q, ? QElemType &e)//出隊(duì);{Queueptr p;if (Q.front == Q.rear)return 0;p = Q.front->next;e = p->data;Q.front->next = p->next;if (Q.rear == p)Q.rear = Q.front;free (p);return 1;}Status QueueEmpty(LinkQueue Q)//判斷是否為空隊(duì);{if(Q.front==Q.rear)return 1;elsereturn 0;}void Traverse(BiTree T)//二叉樹的層次遍歷序列; {LinkQueue Q;//BiTree p;//p=T;InitQueue(Q);if(T)EnQueue(Q, T);while(!QueueEmpty(Q)){DeQueue(Q,T);printf("%c", T->data);if(T->lchild)EnQueue(Q, T->lchild);if(T->rchild)EnQueue(Q, T->rchild);}} int main() {BiTree T;int t;scanf("%d",&t);for(int j=0;j<t;j++){scanf("%s",str);i=0;create(T);//樹的建立;Traverse(T);//二叉樹的層次遍歷序列;printf("\n");} } #include<iostream> #include<cstring> #include<queue> #include<cstdio> #include<algorithm> using namespace std; typedef struct Bnode {char data;Bnode *rchild,*lchild; }*BiTree,Bnode; char str[55]; int i; void create(BiTree &T) {if(str[i++]==',') T=NULL;else{T=new Bnode;if(!T) exit(0);T->data=str[i-1];create(T->lchild);create(T->rchild);} } void ?cengci(BiTree &T) {int out=0,in=0;BiTree q[100];//存樹的隊(duì)列;if(T)q[in++]=T;while(in>out){if(q[out]){printf("%c",q[out]->data);q[in++]=q[out]->lchild;q[in++]=q[out]->rchild;}out++;} } void Traverse(BiTree T) {queue<BiTree> q;BiTree p=NULL;if(T){q.push(T);}while(!q.empty()){p=q.front();q.pop();cout<<p->data;if(p->lchild)q.push(p->lchild);if(p->rchild)q.push(p->rchild);} } int main() {BiTree T;int t;while(cin>>t){for(int j=0;j<t;j++){scanf("%s",str);T=NULL;i=0;create(T);//cengci(T);Traverse(T);cout<<endl;}}return 0; } 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的数据结构实验之二叉树五:层序遍历的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: new/delete和malloc/fr
- 下一篇: 小鑫去爬山