1、数据结构实验3 二叉树层次遍历数据结构实验3实验报告实验项目3:二叉树层次遍历学号姓名课程号实验地点指导教师时间评语: 按时完成实验;实验内容和过程记录完整;回答问题完整、正确;实验报告的撰写认真、格式符合要求;无抄袭的行为。成绩教师签字二叉树从左至右,从上至下层次遍历1、预习要求:二叉树结构定义和层次遍历。2、实验目的: (1)了解二叉树结构层次遍历概念;(2)理解二叉树二种不同层次遍历过程;(3)掌握二叉树层次遍历算法程序。3、实验内容及要求:(1)建立包含10个结点的二叉树(树结构和数据元素的值由自己设定);(2)完成二叉树从左至右,从上至下层次遍历程序;(3)给出程序和遍历程序的结果。
2、4、实验设备(环境)及要求硬件:支持 Intel Pentium 及其以上 CPU ,内存 128MB 以上、硬盘 1GB 以上容量的微机。软件:配有 Windows98/2000/XP 操作系统,安装 Visual C+ 。5、实验时间:10学时6、该文档的文件名不要修改,存入 命名的文件夹中7、该表中的数据只需填空,已有内容不要修改实验结果(运行结果界面及源程序,运行结果界面放在前面):#include #include #include #include #define STUDENT ETypestruct STUDENT char number8; char name8; char
3、sex3; int age; char place20;struct BinaryTreeNode EType data; BinaryTreeNode *LChild; BinaryTreeNode *RChild;struct QType BinaryTreeNode *ptr;struct Queue QType *element; int front; int rear; int maxsize;struct Node_Ptr BinaryTreeNode *ptr; ;void CreatQueue(Queue &Q,int MaxQueueSize)/创建队列 Q.maxsize=
4、MaxQueueSize; Q.element=new QTypeQ.maxsize+1; Q.front=0; Q.rear=0;bool IsEmpty(Queue &Q)/判断队列是否为空 if(Q.front=Q.rear) return true; return false;bool IsFull(Queue &Q)/判断队列是否为满 if(Q.front=(Q.rear+1)%(Q.maxsize+1) return true; return false;bool GetFront(Queue &Q,QType &result)/取出队头元素 if(IsEmpty(Q) retur
5、n false; result=Q.element(Q.front+1)%(Q.maxsize+1); return true;bool GetRear(Queue &Q,QType &result)/取出队尾元素 if(IsEmpty(Q) return false; result=Q.elementQ.rear; return true;bool EnQueue(Queue &Q,QType &x)/元素进队 if(IsFull(Q) return false; Q.rear=(Q.rear+1)%(Q.maxsize+1); Q.elementQ.rear=x; return true;
6、bool DeQueue(Queue &Q,QType &result)/元素出队 if(IsEmpty(Q) return false; Q.front=(Q.front+1)%(Q.maxsize+1); result=Q.elementQ.front; return true;BinaryTreeNode *MakeNode(EType &x)/构造节点 BinaryTreeNode *ptr; ptr=new BinaryTreeNode; if(!ptr) return NULL; ptr-data=x; ptr-LChild=NULL; ptr-RChild=NULL; retur
7、n ptr;void MakeBinaryTree(BinaryTreeNode *root, BinaryTreeNode *left,BinaryTreeNode *right)/构造二叉树之间的关系 root-LChild=left; root-RChild=right;void OutputBinaryTreeNode(BinaryTreeNode *p)/输出节点 cout data.number data.name data.sex data.age data.placeendl; coutendl;void LevelOrder_LtoR_UtoD(BinaryTreeNode
8、*BT)/从左至右,从上至下按层次遍历一棵二叉树 Queue Q; QType temp; BinaryTreeNode *p; int maxqueuesize=50; CreatQueue(Q,maxqueuesize); p=BT; temp.ptr=p; EnQueue(Q,temp); coutendl; cout 学号 姓名 性别 年龄 住址 endl; cout =LChild) temp.ptr=p-LChild; EnQueue(Q,temp); if(p-RChild) temp.ptr=p-RChild; EnQueue(Q,temp); void LevelOrder_
9、RtoL_UtoD(BinaryTreeNode *BT)/从右至左,从上至下按层次遍历一棵二叉树 Queue Q; QType temp; BinaryTreeNode *p; int maxqueuesize=50; CreatQueue(Q,maxqueuesize); p=BT; temp.ptr=p; EnQueue(Q,temp); coutendl; cout 学号 姓名 性别 年龄 住址 endl; cout =RChild) temp.ptr=p-RChild; EnQueue(Q,temp); if(p-LChild) temp.ptr=p-LChild; EnQueue(
10、Q,temp); void LevelOrder_LtoR_DtoU(BinaryTreeNode *BT)/从左至右,从下至上按层次遍历一棵二叉树 Queue Q; QType temp; BinaryTreeNode *p; int maxqueuesize=50; CreatQueue(Q,maxqueuesize); int frontkeep=Q.front; p=BT; temp.ptr=p; EnQueue(Q,temp); coutendl; cout 学号 姓名 性别 年龄 住址 endl; cout =RChild) temp.ptr=p-RChild; EnQueue(Q
11、,temp); if(p-LChild) temp.ptr=p-LChild; EnQueue(Q,temp); for(int i=Q.rear;ifrontkeep;i-) OutputBinaryTreeNode(Q.elementi.ptr);void LevelOrder_RtoL_DtoU(BinaryTreeNode *BT)/从右至左,从下至上按层次遍历一棵二叉树 Queue Q; QType temp; BinaryTreeNode *p; int maxqueuesize=50; CreatQueue(Q,maxqueuesize); int frontkeep=Q.fro
12、nt; p=BT; temp.ptr=p; EnQueue(Q,temp); coutendl; cout 学号 姓名 性别 年龄 住址 endl; cout =LChild) temp.ptr=p-LChild; EnQueue(Q,temp); if(p-RChild) temp.ptr=p-RChild; EnQueue(Q,temp); for(int i=Q.rear;ifrontkeep;i-) OutputBinaryTreeNode(Q.elementi.ptr);int BinaryHeight(BinaryTreeNode *BT)/返回二叉树的高度 if(!BT) ret
13、urn 0; int HighL=BinaryHeight(BT-LChild); int HighR=BinaryHeight(BT-RChild); if(HighLHighR) return +HighL; else return +HighR;void BinaryDelete(BinaryTreeNode *BT)/二叉树删除算法 if(BT) BinaryDelete(BT-LChild); BinaryDelete(BT-RChild); delete BT; int BinaryNodeSum(BinaryTreeNode *BT)/计算二叉树中的节点数 if(!BT) ret
14、urn 0; Queue Q; QType temp; BinaryTreeNode *p; int maxqueuesize=50; int index=0; CreatQueue(Q,maxqueuesize); p=BT; temp.ptr=p; EnQueue(Q,temp); while(p) if(!DeQueue(Q,temp) break; p=temp.ptr; /出队成功 index+; if(p-LChild) temp.ptr=p-LChild; EnQueue(Q,temp); if(p-RChild) temp.ptr=p-RChild; EnQueue(Q,tem
15、p); return index;void DigitalToString(char str,int n) char temp; char k=1; int i=0; while (n & i80) k=n%10+48; n=n/10; stri=k; i+; stri=0; int len=strlen(str); for (i=0;ilen/2;i+) temp=stri; stri=strlen-i-1; strlen-i-1=temp; char *GetOuputInformationString(int WidthOfData, char *OutputInformation, c
16、har *outputstring)/将一个元素的字符串OutputInformation转换为宽度为WidthOfData的等长字符串outputstring /例如,姓名是由四个字符组成,而WidthOfData为8,则在姓名字符串的两边分别连接两个字符,形成8个长度的字符串 int left_space,right_space; int i; char left_space_string16=; char right_space_string16=; int add_space; int len_OutputInformation=strlen(OutputInformation); /
17、计算OutputInformation的字符个数 add_space=WidthOfData - len_OutputInformation; /计算需要补充的字符个数 left_space=add_space/2; /计算OutputInformation左边需要补充的字符个数 right_space=add_space-left_space; /计算OutputInformation右边需要补充的字符个数 for(i=1;i=left_space;i+) /形成OutputInformation左边需要补充的空格字符串 strcat(left_space_string, ); for(i=
18、1;idata.name); break; case 2: strcat(outputInformation,elementk.ptr-data.number); break; case 3: strcat(outputInformation,elementk.ptr-data.place); break; case 4: strcat(outputInformation,elementk.ptr-data.sex); break; case 5: DigitalToString(outputInformation,elementk.ptr-data.age); break; default:
19、 break; return outputInformation;/输出二叉树void OutputBinaryTree(BinaryTreeNode *BT) Node_Ptr temp,*element; BinaryTreeNode *p; int MaxSize; int BinaryTreeHigh; int i,j,k; BinaryTreeHigh=BinaryHeight(BT); MaxSize=(int) pow(2,BinaryTreeHigh); element = new Node_Ptr MaxSize+1; /定义一个用于存放二叉树结点指针的数组 for (i=1
20、;i=MaxSize;i+) /对指针数组初始化,初值设为NULL elementi.ptr=NULL; p = BT; temp.ptr = p; if (p) element1=temp; for (i=1;iLChild ) /&!p-Lflag/线索树特有的处理与一般二叉树不同之处 temp.ptr = p-LChild; element2*i=temp; if (p-RChild ) /&!p-Rflag/线索树特有的处理与一般二叉树不同之处 temp.ptr = p-RChild; element2*i+1=temp; int WidthOfData=5; int Interval
21、OfData=3;/ coutWidthOfData=WidthOfDataendl;/ coutIntervalOfData=IntervalOfDataendl;/ coutBinaryTreeHigh=BinaryTreeHighendl; int position_of_central617; /存放每一元素输出位置中心(距离屏幕左边界的字符数) int row,col; /二维数组的行列下标变量 for(i=0;i=BinaryTreeHigh;i+) /存放每一元素输出位置中心(距离屏幕左边界的字符数),初值为0 for(j=0;j=pow(2,BinaryTreeHigh-1);
22、j+) position_of_centralij=0; for(i=1;i=pow(2,BinaryTreeHigh)-1;i+) /对深度为BinaryTreeHigh的满二叉树的所有结点计算每个结点输出的中心点位置 k=i*2; /k指向i的左子树根结点 while (k=pow(2,BinaryTreeHigh)-1) /k不断推进到i编号的结点左子树中最右子孙结点 k=2*k+1; k=k/2; /k的值就是i编号的结点左子树中最右子孙结点的编号 j=(int)(k-(pow(2,(BinaryTreeHigh-1)-1); /由k编号的结点换算出该结点是底层中从左至右第j个结点右上
23、方 /即编号为k的结点中心点垂直线左边最底层上有j个结点 row=(int)(log10(i)/log10(2)+1); /计算中心点值存放在position_of_centralrowcol中的row col=(int)(i-(pow(2,(row-1)-1); /计算中心点值存放在position_of_centralrowcol中的col if(row=BinaryTreeHigh) /计算底层i结点距离左边界的字符数,左边无间隙 position_of_centralrowcol= (j-1)*WidthOfData + (j-1)*IntervalOfData + (WidthOfData/2 +1); else /计算非底层i结点距离左边界的字符数, position_of_centralrowcol=j*WidthOfData + (j-1)*IntervalOfData + (IntervalOfData/2 +1); char prespace100; int m; int kk; int kkk; int item_max=5; coutendl; for(i=1;i=BinaryTreeHigh;i+) kkk=(i
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1