构造二叉树的抽象数据类型对于给定的先序序列和中序序列,构造二叉树,并按层输出所有结点内容,要求每层结点输出一行按层输出上述二叉树所表示的森林的所有结点内容
来源:互联网 发布:python split空格 编辑:程序博客网 时间:2024/05/17 23:02
第一次发表文章
刚编好的程序,在WINTC环境下运行无错误,题目要求如题
(不是伪码,可以直接运行哦)
#include <stdio.h>
#include <conio.h>
#define MAXLENGTH 1000
struct BiTNode
{
char data;
struct BiTNode *lchild,*rchild;
};
struct SqQueue
{
struct BiTNode *data[MAXLENGTH];
int front;
int rear;
};
void EnQueue(struct SqQueue *Q,struct BiTNode *e) /*增加队列*/
{
Q->data[Q->rear]=e;
Q->rear=(Q->rear+1)%MAXLENGTH;
}
struct BiTNode *DeQueue(struct SqQueue *Q) /*删除队列*/
{
int now=Q->front;
Q->front=(Q->front+1)%MAXLENGTH;
return Q->data[now];
}
void FloorPrint(struct BiTNode *T) /*按层输出此二叉树*/
{
struct SqQueue *store;
struct BiTNode *output;
store=(struct SqQueue*)malloc(sizeof(struct SqQueue));
store->front=store->rear=0;
if(T!=NULL)
{
EnQueue(store,T);
while(store->front!=store->rear)
{
int control=store->rear-store->front;
int i;
for(i=0;i<control;i++)
{
output=(struct BiTNode*)malloc(sizeof(struct BiTNode));
output=DeQueue(store);
printf("%c",output->data);
if(output->lchild!=NULL)
{
EnQueue(store,output->lchild);
}
if(output->rchild!=NULL)
{
EnQueue(store,output->rchild);
}
}
printf("/n");
}
}
}
void FloorPrintTree(struct BiTNode *T) /*按层输出此二叉树所表示的森林*/
{
struct SqQueue *store;
struct BiTNode *output;
int level=1;
store=(struct SqQueue*)malloc(sizeof(struct SqQueue));
store->front=store->rear=0;
if(T!=NULL)
{
EnQueue(store,T);
while(T->rchild!=NULL)
{
T=T->rchild;
EnQueue(store,T);
}
while(store->front!=store->rear)
{
int control=store->rear-store->front;
int i;
for(i=0;i<control;i++)
{
output=(struct BiTNode*)malloc(sizeof(struct BiTNode));
output=DeQueue(store);
printf("%c",output->data);
if(output->lchild!=NULL)
{
output=output->lchild;
EnQueue(store,output);
while(output->rchild!=NULL)
{
output=output->rchild;
EnQueue(store,output);
}
}
}
printf("/n");
}
}
}
void createtree(struct BiTNode *T,int pre1,int pre2,int in1,int in2,char pre[],char in[]) /*按给定的先序序列和中序序列构造二叉树*/
{
int control=0;
int same;
same=in1;
if(pre[pre1]!=in[same])
{
while(pre[pre1]!=in[same])
{
same+=1;
control+=1;
}
}
if((control==0)&&(pre1==pre2))
{
T->data=pre[pre1];
T->lchild=NULL;
T->rchild=NULL;
}
if((control!=0)&&(control<(pre2-pre1)))
{
T->lchild=(struct BiTNode*)malloc(sizeof(struct BiTNode));
T->rchild=(struct BiTNode*)malloc(sizeof(struct BiTNode));
T->data=pre[pre1];
createtree(T->lchild,pre1+1,pre1+control,in1,in1+control-1,pre,in);
createtree(T->rchild,pre1+control+1,pre2,in1+control+1,in2,pre,in);
}
if((control!=0)&&(control==pre2-pre1))
{
T->lchild=(struct BiTNode*)malloc(sizeof(struct BiTNode));
T->data=pre[pre1];
createtree(T->lchild,pre1+1,pre2,in1,in1+control-1,pre,in);
T->rchild=NULL;
}
if((control==0)&&(pre1!=pre2))
{
T->rchild=(struct BiTNode*)malloc(sizeof(struct BiTNode));
T->data=pre[pre1];
T->lchild=NULL;
createtree(T->rchild,pre1+control+1,pre2,in1+control+1,in2,pre,in);
}
}
main()
{
struct BiTNode *t,*a,*b,*n;
char pre[8],in[8];
int i;
pre[0]='a';
pre[1]='b';
pre[2]='c';
pre[3]='d';
pre[4]='e';
pre[5]='g';
pre[6]='f';
pre[7]='h';
in[0]='c';
in[1]='b';
in[2]='e';
in[3]='g';
in[4]='d';
in[5]='f';
in[6]='a';
in[7]='h';
t=(struct BiTNode*)malloc(sizeof(struct BiTNode));
createtree(t,0,7,0,7,pre,in);
printf("Please enter you choice:/n1.print in floor of this tree/n2.print in floor of the forest/n");
scanf("%d",&i);
if(i==1)
{
FloorPrint(t);
}
if(i==2)
{
FloorPrintTree(t);
}
getch();
}
- 构造二叉树的抽象数据类型对于给定的先序序列和中序序列,构造二叉树,并按层输出所有结点内容,要求每层结点输出一行按层输出上述二叉树所表示的森林的所有结点内容
- 输出二叉树某一层的所有结点
- 从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。
- 从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。
- 打印二叉树第K层的所有结点
- 得到二叉树指定层的所有结点
- 输出二叉树中所有从根结点到叶子结点的路径
- 输出二叉树中所有从根结点到叶子结点的路径
- 输出二叉树中所有从根结点到叶子结点的路径
- 给定先序和后续,构造出一颗二叉树并输出中序序列
- 输出二叉树中中序遍历中给定结点的下一个结点
- 设二叉树结点的先根序列、中根序列和后根序列中,所有叶子结点的先后顺序____。
- 【数据结构作业四】以二叉链表作存储结构,建立一棵二叉树,并输出该二叉树的先序、中序、后序遍历序列、高度和其叶子结点数。
- 递归输出二叉树的每个结点
- 二叉树的构造与遍历—先根构造,一维数组存储按层构造,先根,中根,后根遍历输出
- 【数据结构】输入二叉树的前序序列,输出二叉树的不同度的结点个数
- 二叉树的创建与遍历&二叉树的高度&二叉树每层结点个数&复制二叉树
- c++输出二叉树叶子结点并输出叶子结点到根结点的路径长度
- 关于文本和字体
- 我在美国做程序员(2)
- 定时的重要性
- 如何在rad hat enterprise 5 中启动mysql服务
- 一段可能危险的asp.net代码
- 构造二叉树的抽象数据类型对于给定的先序序列和中序序列,构造二叉树,并按层输出所有结点内容,要求每层结点输出一行按层输出上述二叉树所表示的森林的所有结点内容
- tttttttttttttttttttttt
- The Video4Linux2 API series
- 开篇
- AJAX技术介绍
- 职场好人缘的26个细节
- AJAX框架简介
- 关于SQL SERVER建立索引需要注意的问题
- Ajax应用场景简析