1、资料内容仅供您学习参考,如有不当之处,请联系改正或者删除。 信息科学与技术学院 数据结构课程设计报告 题目名称: 二叉树的应用 专业班级: 计算机科学与技术 学生姓名: 陈 杰 学生学号: 指导教师: 高 攀 完成日期: -04 目 录 1、 课程设计的目的、 课程设计题目、 题目要求 2 1.1课程设计的目的 3 1.2课程设计的题目 3 1.3题目要求 3 2课程设计的实验报告内容: 4 3课程设计的原程序代码: 4 4运行结果 16
2、5. 课程设计总结 21 6参考书目 22 1课程设计的目的 1.1课程设计的目的: 经过以前的学习以及查看相关资料,按着题目要求编写程序,进一步加强对编程的训练,使得自己掌握一些将书本知识转化为实际应用当中.在整个程序中,主要应用的是链表,可是也运用了类.经过两种方法解决现有问题. 1.2课程设计的题目: 二叉树的应用 1.3题目要求: 1. 建立二叉树的二叉链表存储算法 2. 二叉树的先序遍历, 中序遍历和后序遍历输出 3. 非递归的先序遍历, 中序遍历 4. 二叉树的层次遍历 5. 判断此二叉树是否是完全二叉树 6. 二叉树的左右孩子的交换
3、
2课程设计的实验报告内容:
7. 经过递归对二叉树进行遍历。二叉树的非递归遍历主要采用利用队进行遍历。此后的判断此二叉树是否是完全二叉树也才采用队, 而二叉树的左右孩子的交换则采用的是一个简单的递归。
3课程设计的原程序代码:
#include
4、e,*BiTree; int CreateBiTree(BiTree &T) //创立二叉树 { char ch; cout<<"请输入数据( #为结束) : "; cin>>ch; if(ch=='#') T=NULL; else { if(!(T=new BiTNode)){ cout<<"Overflow!"; //no alloction return 0; } T->data=ch; CreateBiTree(T->left_child); //c
5、reate leftchild CreateBiTree(T->right_child); //create rightchild } return 1; } //判断此树是否是完全二叉树 int LevelOrder1(BiTree &T) { BiTree stack[MAXSIZE]; BiTree p; int front,rear; front=-1,rear=0; stack[rear]=T; while(rear!=front) { front++; p=stack[front];
6、 if((p->left_child==NULL)&&(p->right_child)) sign=1; if(p->left_child) { rear++; stack[rear]=p->left_child; } if(p->right_child) { rear++; stack[rear]=p->right_child; } } return 1; } void Output(BiTree &T) //输出二叉树 { if(!T) { cout<<"空树!\n";
7、 return ;
} //空树
cout<
8、n (i>j?i:j) + 1; } int Node(BiTree &T)//求结点数 { if(!T) return 0; return 1+Node(T->left_child)+Node(T->right_child); } int Leaf(BiTree &T) //求叶子结点 { if(!T) return 0; if(!T->left_child&&!T->right_child) return 1;//仅有根结点 return Leaf(T->left_child)+Leaf(T->right_child);//返回叶子结点的数目 }
9、
void PreOrder(BiTree &T) //先序遍历算法 DLR
{
if(!T) return ; //递归调用的结束条件
cout<
10、
InOrder(T->right_child);
}
void PostOrder(BiTree &T)//后序遍历算法 LRD
{
if(!T) return ;
PostOrder(T->left_child);
PostOrder(T->right_child);
cout<
11、ULL&&top==-1)){
while(p!=NULL){
cout<
12、BiTree &T)
{BiTree stack[100],p;
int top;
if(T==NULL)
return 1;
top=-1;
p=T;
while(!(p==NULL&&top==-1)){
while(p!=NULL){
if(top<100-1)
{
top++;
stack[top]=p;
}
else{
cout<<"栈溢出"<
13、op];
cout<
14、LL){ rear++; queue[rear]=queue[front]->left_child; } if(queue[front]->right_child!=NULL) { rear++; queue[rear]=queue[front]->right_child; } } } //*******************************左右子树交换***************************** /*将结点的左右子树交换*/ /*BiTNode *swap(BiTNode &T) { BiTNode *t,*t1,*t2; if
15、T==NULL) t=NULL; else { t=(BiTNode *)malloc(sizeof(BiTNode)); t->data=T->data; t1=swap(T->left_child); t2=swap(T->right_child); t->left_child=t2; t->right_child=t1; } return(t); } void print(BiTNode &T) { if(T!=NULL) { printf("%c",T->data)
16、 if(T->left_child!=NULL||T->right_child!=NULL) { printf("("); print(b->left_child); if(b->right_child!=NULL)printf(", "); print(b->right_child); printf(")"); } } }*/ int PreOrderTraverse(BiTree T) { if(!T) return 0; BiTree t; if (T-
17、>left_child||T->right_child) { t=T->left_child; T->left_child=T->right_child; T->right_child=t; } PreOrderTraverse(T->left_child); PreOrderTraverse(T->right_child); return 0; } //菜单 void menu() { cout<<"***********************************************************************
18、"< 19、ut<<"<< 1.二叉树树深 >>"< 20、 4.二叉树的先序遍历 >>"< 21、 >>"< 22、 11.左右孩子交换 >>"< 23、Tree T;
br=CreateBiTree(T);
while(1){
menu();
cout<<"请输入选择的命令-->";
cin>>a;
if(a>=0||a<=12)
{
switch (a){
case 0:
{cout<<"建立后的二叉树为: \n";
Output(T);
cout< 24、<"该树的结点数: "< 25、use");break;
case 6:
{cout<<"该树以后序遍历输出为: \n";
PostOrder(T);
cout< 26、se");break;
case 9:
{cout<<"该树的层次遍历: \n";
LevelOrder(T);
cout< 27、
case 11:PreOrderTraverse(T);
cout<<"左右孩子已经替换成功"<






