树形结构及其应用.docx

上传人:b****6 文档编号:4774418 上传时间:2022-12-08 格式:DOCX 页数:4 大小:17.26KB
下载 相关 举报
树形结构及其应用.docx_第1页
第1页 / 共4页
树形结构及其应用.docx_第2页
第2页 / 共4页
树形结构及其应用.docx_第3页
第3页 / 共4页
树形结构及其应用.docx_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

树形结构及其应用.docx

《树形结构及其应用.docx》由会员分享,可在线阅读,更多相关《树形结构及其应用.docx(4页珍藏版)》请在冰豆网上搜索。

树形结构及其应用.docx

树形结构及其应用

树形结构及其应用

树形结构及其应用实验题目:

树型结构的建立与遍历设计成绩报告成绩指导老师

一、实验目的1、学会二叉树这一数据结构的用法,掌握二叉树的存储结构,包括二叉树顺序存储结构和链式存储结构。

2、熟练掌握二叉树与广义表之间的相互转换方法。

3、熟练掌握二叉树的先序、中序、后序,递归与非递归遍历算法。

4、学会二叉树线索化方法,并掌握线索二叉树的存储结构。

5、熟练掌握线索二叉树的先序、中序、后序的遍历算法。

二、实验要求及实验环境1、实验要求:

(1)

至少用两种方法,编写建立二叉树的二叉链表存储结构的程序,并用广义表的形式显示并保存二叉树;

(2)

采用二叉树的二叉链表存储结构,编写程序实现二叉树的先序、中序和后序遍历的递归和非递归算法以及层序遍历算法,并显示二叉树和相应的遍历序列;(3)

在二叉树的二叉链表存储结构基础上,编写程序实现二叉树的先序、中序和后序线索链表存储结构建立的算法,并用广义表的形式显示和保存二叉树的线索链表;(4)

在二叉树的线索链表存储结构上,编写程序分别实现求一个结点的先序(中序、后序)的后继结点的算法;(5)

在(4)

基础上,编写程序实现对线索二叉树进行先序、中序和后序遍历的非递归算法,并显示线索二叉树和相应的遍历序列。

2、实验环境:

Windows下的dev-c++、

三、设计思想(本程序中的用到的所有数据类型的定义,主程序的流程图及各程序模块之间的调用关系)Createabit-tree1.逻辑设计主程序流程递归中序线索化线索化递归中序线索化递归先序线索化以广义表格式输出树递归非递归先序遍历递归非递归中序遍历递归非递归后序遍历输出节点的后继节点遍历后序线索化遍历中序线索化遍历先序线索化线索二叉树链式结构定义typedefstructbitnode{chardata;structbitnode*lchild,*rchild;boolLTag,RTag;}bnode,*btree;2.物理设计Btreecreate()创建一棵树并赋值应用数组栈btreea[MAX]VoidprintBTree(btreeT)以广义表形式打印树Voidpreorder(btreet)先序递归遍历Voidinorder(btreet)中序递归遍历Voidpostorder(btreet)后序递归遍历btreecopy(btreet)复制二叉树Intmain()主函数Voidpreorder2(btreet)先序非递归遍历Voidinorder2(btreet)中序非递归遍历Voidpostorder2(btreet)后序非递归遍历层序遍历voidlevelorder(btreet)voidprethreading(btreep)先序线索化Btreepreorderthreading(btreeprehead,btrreeT);先序线索化voidprethreading(btreep)先序线索化Btreeinorderthreading(btreeprehead,btrreeT);中序线索化voidprethreading(btreep)先序线索化Btreepostorderthreading(btreeprehead,btrreeT);后序线索化寻找字符X处于线索树中位置btreelocate(chara,btreet)中序线索化后继节点Btreeinnext(btreep);输出后继节点先序线索化后继节点Btreeprenext(btreep)Voidprintlist(btreet,boola)广义表形式输出线索化后的二叉树

四、测试结果

五、系统不足与经验体会不能连续输入多个广义表。

函数设计不够简洁。

没有实现非递归线索化加注释后修改代码方便六、附录:

源代码(带注释)#include#include#defineMAX100typedefstructbitnode{chardata;structbitnode*lchild,*rchild;boolLTag,RTag;}bnode,*btree;//建立树线索化结构体structbitnode*s[MAX];structbitnode*pre=NULL;btreecreate(){charch;scanf("%c",&ch);if(ch==#)

returnNULL;else{btreebt=(btree)malloc(sizeof(bnode));if(bt==NULL)

returnNULL;else{bt->data=ch;bt->lchild=create();bt->rchild=create();returnbt;}}}btreecreate2(){inti,j;btreebt,t;charch;printf("输入一个整数和一个字符\n");scanf("%d%c",&i,&ch);while(i!

=0&&ch!

=0)

{bt=(btree)malloc(sizeof(bnode));if(bt==NULL)

returnNULL;else{bt->data=ch;bt->lchild=NULL;bt->rchild=NULL;s[i]=bt;if(i==1)

t=bt;else{j=i/2;if(i%2==0)

s[j]->lchild=bt;elses[j]->rchild=bt;}}printf("输入一个整数和一个字符\n");scanf("%d%c",&i,&ch);}returnt;}//复制一棵二叉树btreecopy(btreeBT){btreetree;if(BT==NULL)

tree=NULL;else{tree=(btree)malloc(sizeof(bnode));tree->data=BT->data;tree->lchild=copy(BT->lchild);tree->rchild=copy(BT->rchild);}returntree;}//层序遍历并输出voidlevelorder(btreebt){btreet[MAX];//建立队列intfront=0,rear=1;btreep;t[0]=bt;while(front

//如果队列不为空的话,访问结点值再出队列{p=t[front];printf("%c",p->data);if(p->lchild)

//左孩子进队列(如果有的话),右孩子进队列(如果有的话)t[rear++]=t[front]->lchild;if(p->rchild)

t[rear++]=t[front]->rchild;++front;}}//先序递归遍历并输出voidpreorder(btreebt)

{if(NULL!

=bt)

{printf("%c",bt->data);preorder(bt->lchild);preorder(bt->rchild);}}//先序非递归遍历并输出voidpreorder2(btreebt){btreet[MAX];//建立数组栈btreep;p=bt;inti=0;while(p||i>0)

{while(p)

/*根结点先进栈,访问结点值,左孩子依次进栈,直到最左的孩子进栈*/{printf("%c",p->data);++i;t[i]=p;p=p->lchild;}if(i>0)

/*出栈*/{p=t[i];i;p=p->rchild;}}//栈顶指针出栈,退至上一层,然后访问右子树,右子树访问完成之后,退至上一层,直至栈空,节点访问完成}//中序递归遍历并输出voidinorder(btreebt){if(NULL!

=bt)

{inorder(bt->lchild);printf("%c",bt->data);inorder(bt->rchild);}}//中序非递归遍历并输出voidinorder2(btreebt){btreet[MAX];//建立数组栈btreep;p=bt;inti=0;while(p||i>0)

{while(p)

/*根结点先进栈,左孩子依次进栈,直到最左的孩子进栈*/{++i;t[i]=p;p=p->lchild;}if(i>0)

/*出栈*/{p=t[i];i;printf("%c",p->data);p=p->rchild;}}//栈顶指针出栈,退至上一层,访问结点值,然后访问右子树,右子树访问完成之后,退至上一层,直至栈空,节点访问完成}//后序递归遍历并输出voidpostorder(btreebt){if(NULL!

=bt)

{postorder(bt->lchild);postorder(bt->rchild);printf("%c",bt->data);}}//后序非递归并输出voidpostorder2(btreebt)

{btreet[MAX];//建立数组栈btreep,q;inti=0;p=bt;q=NULL;while(p||i>0)

{while(p)

{++i;t[i]=p;p=p->lchild;}if(i>0)

{p=t[i];/*取栈顶元素*/if(!

p->rchild||p->rchild==q)

/*如果p没有右孩子,或右孩子已经访问过*/{printf("%c",p->data);q=p;p=NULL;i;/*退栈*/}elsep=p->rchild;}}}/*树以广义表输出*/voidprintBTree(btreebt)

{/*树为空时结束递归,否则执行如下操作*/if(bt!

=NULL){printf("%c",bt->data);/*输出根结点的值*/if(bt->lchild!

=NULL||bt->rchild!

=NULL){printf("(");printBTree(bt->lchild);if(bt->rchild!

=NULL){printf(",");}printBTree(bt->rchild);printf(")");}}}voidprintlist(btreep,boola){//把线索二叉树用广义表显示if(a==0){printf("%c",p->data);if(p>RTag==0){printf("(");printlist(p->lchild,p->LTag);if(p->RTag==0){printf(",");}printlist(p>RTag);printf(")");}}}voidprethreading(btreep){//先序线索化二叉树if(p)

{if(!

p->lchild)

{p->lchild=pre;p->LTag=1;}//前驱线索else{p->LTag=0;}if(!

pre->rchild){pre->rchild=p;pre->RTag=1;}//后继线索else{pre->RTag=0;}pre=p;//保持pre指向p前驱if(p->LTag==0)prethreading(p->lchild);//左子树线索化if(p>rchild);//右子树线索化}}voidpreorderthreading(btree&prehead,btreeT){//先序线索化二叉树(加表头)prehead=newbitnode;//建立头结点prehead->data=0;prehead->LTag=0;prehead->RTag=0;prehead->rchild=prehead;//右指针回指if(!

T)

//为空时左指针回指{prehead->lchild=prehead;prehead->LTag=1;}else{prehead->lchild=T;pre=prehead;prethreading(T);//先序遍历先序线索化pre->rchild=prehead;//最后一个节点线索化pre->RTag=1;}pre=NULL;}voidinthreading(btreep){//中序线索化二叉树if(p)

{if(p->LTag==0)inthreading(p->lchild);//左子树线索化if(!

p->lchild){//前驱线索p->lchild=pre;p->LTag=1;}else{p->LTag=0;}if(!

pre->rchild){//后继线索pre->rchild=p;pre->RTag=1;}else{pre->RTag=0;}pre=p;if(p->RTag==0)inthreading(p->rchild);//右子树线索化}}voidinorderthreading(btree&inhead,btreeT){//中序线索化二叉树(加表头)inhead=newbitnode;//创建头结点inhead->data=0;inhead->LTag=0;inhead->RTag=0;inhead->rchild=inhead;if(!

T){inhead->lchild=inhead;//空二叉树inhead->LTag=1;}else{inhead->lchild=T;pre=inhead;//pre:

刚刚访问过的结点;inthreading(T);pre->rchild=inhead;pre->RTag=1;}pre=NULL;}voidpostthreading(btreep){//后序线索化二叉树if(p){if(p->LTag==0){postthreading(p>RTag==0){postthreading(p>lchild){p->LTag=1;p->lchild=pre;}//前驱线索else{p->LTag=0;}if(!

pre->rchild){pre->RTag=1;pre->rchild=p;}//后继线索else{p->RTag=0;}pre=p;}//后序搜索化节点的建立}voidpostorderthreading(btree&posthead,btreeT){//后序线索化二叉树(加表头)posthead=newbitnode;posthead->data=0;posthead->LTag=0;posthead->RTag=0;//建头结点posthead->rchild=posthead;//右指针回指if(!

T){posthead->lchild=posthead;//若二叉树空,则左指针回指posthead->LTag=1;}else{posthead->lchild=T;pre=posthead;postthreading(T);//后序遍历进行后序线索化}//最后一个节点处理pre=NULL;}//求线索二叉树先序后续节点btreeprenext(btreep){//求线索二叉树先序后继节点btreeQ;if(p>lchild;else{Q=p;while(Q>rchild;Q=Q->rchild;};return(Q);}//求线索二叉树中序后继节点btreeinnext(btreep){btreeQ;Q=p->rchild;if(p->RTag==0)while(Q->LTag==0)Q=Q->lchild;return(Q);}//返回元素x的位置btreelocate(charx,btreeinhead){btreetmp;tmp=inhead;do{tmp=prenext(tmp);if(tmp!

=inhead){if(tmp->data==x)

//若节点对应数据是X返回该节点位置{returntmp;}}}while(tmp!

=inhead);return0;}intmain(){btreeT,T1,T2,q,p;btreeprehead;btreeinhead,posthead;inta;chars;T=(btree)malloc(sizeof(bnode));printf("先序输入二叉树(#表示空):

");T=create();T1=(btree)malloc(sizeof(bnode));T2=(btree)malloc(sizeof(bnode));p=(btree)malloc(sizeof(bnode));q=(btree)malloc(sizeof(bnode));T1=copy(T);T2=copy(T);//复制树T到T1T2,以便线索化使用printf("以广义表形式输出二叉树:

");printBTree(T);//二叉树遍历printf("\n先序递归遍历为:

");preorder(T);printf("\n先序非递归遍历为:

");preorder2(T);printf("\n中序递归遍历为:

");inorder(T);printf("\n中序非递归遍历为:

");inorder2(T);printf("\n后序递归遍历为:

");postorder(T);printf("\n后序非递归遍历为:

");postorder2(T);//二叉树线索化printf("\n二叉树的线索化!

");printf("\n先序线索化:

");preorderthreading(prehead,T);//先序线索化printlist(prehead->lchild,prehead->LTag);//以广义表形式打印线索树printf("\n中序线索化:

");preorderthreading(inhead,T1);//中序线索化printlist(inhead->lchild,inhead->LTag);//以广义表形式打印线索树printf("\n后序线索化:

");preorderthreading(posthead,T2);//后序线索化printlist(posthead->lchild,posthead->LTag);//以广义表形式打印线索树//二叉树后继节点printf("\n线索二叉树后继节点!

输入一字符:

");getchar();scanf("%c",&s);p=locate(s,prehead);q=innext(p);printf("中序后继节点为%c",q->data);getchar();getchar();return0;}//输入样例dba##c##fe##g##

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高中教育 > 高考

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1