数据结构实验之二叉树五:层序遍历
来源:互联网 发布:matlab量化投资源码 编辑:程序博客网 时间:2024/06/05 17:33
题目描述
已知一个按先序输入的字符序列,如abd,,eg,,,cf,,,(其中,表示空结点)。请建立二叉树并求二叉树的层次遍历序列。
输入
输入数据有多行,第一行是一个整数t (t<1000),代表有t行测试数据。每行是一个长度小于50个字符的字符串。
输出
输出二叉树的层次遍历序列。
示例输入
2abd,,eg,,,cf,,,xnl,,i,,u,,
示例输出
abcdefgxnuli
算法先把根节点入队然后对不为空就输出对头元素,然后把该点的左右子树入队,如此往复,直到对位空
源代码在c++环境下可正常运行
方法一
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<queue>using namespace std;typedef char ET;char str[100];int i,n;/*���*/typedef struct BiTNode{ ET data; struct BiTNode *lchild,*rchild;} BiTNode,*BiTree;/*建��*/int CreateBiTree(BiTree &T){ if(i<n) { if(str[i++]==',') T=NULL; else { T=(BiTNode *)malloc(sizeof(BiTNode)); T->lchild=NULL; T->rchild=NULL; if(!T)exit(-1); T->data=str[i-1]; CreateBiTree(T->lchild); CreateBiTree(T->rchild); } } return 1;}/* C++*/void Inord(BiTree T){ BiTree p; queue<BiTree>q; if(T) { q.push(T); while(!q.empty()) { p=q.front(); printf("%c",p->data); q.pop(); if(p->lchild) { q.push(p->lchild); } if(p->rchild) { q.push(p->rchild); } } }}int main(){ int t; scanf("%d",&t); while(t) { BiTree T; i=0; scanf("%s",str); n=strlen(str); CreateBiTree(T); Inord(T); printf("\n"); t--; } return 0;}
方法二#include<stdio.h>#include<stdlib.h>#include<string.h>#include<queue>using namespace std;typedef char ET;char str[100];struct node{ struct BiTNode *elem; struct node *next;};int i,n;/*å®�ä¹�æ �*/typedef struct BiTNode{ ET data; struct BiTNode *lchild,*rchild;} BiTNode,*BiTree;/*建ç«�æ �*/int CreateBiTree(BiTree &T){ if(i<n) { if(str[i++]==',') T=NULL; else { T=(BiTNode *)malloc(sizeof(BiTNode)); T->lchild=NULL; T->rchild=NULL; if(!T)exit(-1); T->data=str[i-1]; CreateBiTree(T->lchild); CreateBiTree(T->rchild); } } return 1;}/*å±�æ©�Î¥Î�*/struct node *create(BiTree T){ struct node *head,*tail,*p; head=(struct node*)malloc(sizeof(struct node)); head->next=NULL; tail=head; p=(struct node*)malloc(sizeof(struct node)); if(T)/*亲大爷*//*没有这个条件就是Re*/ { p->elem=T; p->next=NULL; tail->next=p; tail=p; } return (head);};void Inord(BiTree T,struct node *h){ struct node *head,*tail,*w,*e; head=h->next; tail=head; while(head) { printf("%c",head->elem->data); if(head->elem->lchild) { w=(struct node*)malloc(sizeof(struct node)); w->next=NULL; w->elem=head->elem->lchild; tail->next=w; tail=w; } if(head->elem->rchild) { e=(struct node*)malloc(sizeof(struct node)); e->next=NULL; e->elem=head->elem->rchild; tail->next=e; tail=e; } head=head->next; }}int main(){ struct node *q; int t; scanf("%d",&t); while(t) { BiTree T; i=0; scanf("%s",str); n=strlen(str); CreateBiTree(T); q=create(T); Inord(T,q); printf("\n"); t--; } return 0;}
0 0
- 数据结构实验之二叉树五:层序遍历
- 数据结构实验之二叉树五:层序遍历
- 数据结构实验之二叉树五:层序遍历
- 数据结构实验之二叉树五:层序遍历
- 数据结构实验之二叉树五:层序遍历
- 数据结构实验之二叉树五:层序遍历
- 数据结构实验之二叉树五:层序遍历
- 数据结构实验之二叉树五:层序遍历
- 数据结构实验之二叉树五:层序遍历
- 数据结构实验之二叉树五:层序遍历
- 数据结构实验之二叉树五:层序遍历
- 数据结构实验之二叉树五:层序遍历
- 数据结构实验之二叉树五:层序遍历
- 数据结构实验之二叉树五:层序遍历
- 数据结构实验之二叉树五:层序遍历
- 数据结构实验之二叉树五:层序遍历
- 数据结构实验之二叉树五:层序遍历
- 数据结构实验之二叉树五:层序遍历
- Java_数组续集
- Android自定义适配器父类继承BaseAdapter,自定义生成对话框工具类
- 【win32】day05-鼠标消息/定时器消息/菜单
- hdu 5773 (The All-purpose Zero)
- 文章推荐
- 数据结构实验之二叉树五:层序遍历
- 上证指数大盘分析
- cmd 执行oracle sql脚本
- I/O(input/output)是什么意思?
- 求二叉树的深度
- 51nod oj 1094 和为k的连续区间【纯暴力--二分求解】
- sleep()和wait()的区别
- 斯坦福第三章:logistic回归中的权系数迭代公式
- 大数相加算法