二叉树的应用数据结构课程设计.docx
- 文档编号:9135914
- 上传时间:2023-02-03
- 格式:DOCX
- 页数:21
- 大小:161.32KB
二叉树的应用数据结构课程设计.docx
《二叉树的应用数据结构课程设计.docx》由会员分享,可在线阅读,更多相关《二叉树的应用数据结构课程设计.docx(21页珍藏版)》请在冰豆网上搜索。
二叉树的应用数据结构课程设计
信息科学与技术学院
数据结构课程设计报告
题目名称:
二叉树的应用
专业班级:
计算机科学与技术
学生姓名:
陈杰
学生学号:
200808261
指导教师:
高攀
完成日期:
2010-04
目录
1、课程设计的目的、课程设计题目、题目要求2
1.1课程设计的目的3
1.2课程设计的题目3
1.3题目要求3
2课程设计的实验报告内容:
4
3课程设计的原程序代码:
4
4运行结果16
5.课程设计总结21
6参考书目22
1课程设计的目的
1.1课程设计的目的:
通过以前的学习以及查看相关资料,按着题目要求编写程序,进一步加强对编程的训练,使得自己掌握一些将书本知识转化为实际应用当中.在整个程序中,主要应用的是链表,但是也运用了类.通过两种方法解决现有问题.
1.2课程设计的题目:
二叉树的应用
1.3题目要求:
1.建立二叉树的二叉链表存储算法
2.二叉树的先序遍历,中序遍历和后序遍历输出
3.非递归的先序遍历,中序遍历
4.二叉树的层次遍历
5.判断此二叉树是否是完全二叉树
6.二叉树的左右孩子的交换
2课程设计的实验报告内容:
7.通过递归对二叉树进行遍历。
二叉树的非递归遍历主要采用利用队进行遍历。
此后的判断此二叉树是否是完全二叉树也才采用队,而二叉树的左右孩子的交换则采用的是一个简单的递归。
3课程设计的原程序代码:
#include
usingnamespacestd;
#defineMAXSIZE100
intsign=0;
voidmenu();
//
typedefstructBiTNode
{
chardata;
BiTNode*left_child,*right_child;
}BiTNode,*BiTree;
intCreateBiTree(BiTree&T)//创建二叉树
{charch;
cout<<"请输入数据(#为结束):
";
cin>>ch;
if(ch=='#')T=NULL;
else
{
if(!
(T=newBiTNode)){
cout<<"Overflow!
";//noalloction
return0;
}
T->data=ch;
CreateBiTree(T->left_child);//createleftchild
CreateBiTree(T->right_child);//createrightchild
}
return1;
}
//判断此树是否是完全二叉树
intLevelOrder1(BiTree&T)
{
BiTreestack[MAXSIZE];
BiTreep;
intfront,rear;
front=-1,rear=0;
stack[rear]=T;
while(rear!
=front)
{
front++;
p=stack[front];
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;
}
}
return1;
}
voidOutput(BiTree&T)//输出二叉树
{
if(!
T){
cout<<"空树!
\n";
return;
}//空树
cout<
if(T->left_child)Output(T->left_child);//输出左子树
if(T->right_child)Output(T->right_child);//输出右子树
}
intDepth(BiTree&T)//求树深
{
inti,j;
if(!
T)return0;
i=Depth(T->left_child);
j=Depth(T->right_child);
return(i>j?
i:
j)+1;
}
intNode(BiTree&T)//求结点数
{
if(!
T)return0;
return1+Node(T->left_child)+Node(T->right_child);
}
intLeaf(BiTree&T)//求叶子结点
{
if(!
T)return0;
if(!
T->left_child&&!
T->right_child)return1;//仅有根结点
returnLeaf(T->left_child)+Leaf(T->right_child);//返回叶子结点的数目
}
voidPreOrder(BiTree&T)//先序遍历算法DLR
{
if(!
T)return;//递归调用的结束条件
cout<
PreOrder(T->left_child);//先序递归遍历T的左子树
PreOrder(T->right_child);//先序递归遍历T的右子树
}
voidInOrder(BiTree&T)//中序遍历算法LDR
{
if(!
T)return;
InOrder(T->left_child);
cout<
InOrder(T->right_child);
}
voidPostOrder(BiTree&T)//后序遍历算法LRD
{
if(!
T)return;
PostOrder(T->left_child);
PostOrder(T->right_child);
cout<
}
//非递归先序遍历
intNRPreOrder(BiTree&T)
{BiTreestack[100],p;
inttop;
if(T==NULL)
return1;
top=-1;
p=T;
while(!
(p==NULL&&top==-1)){
while(p!
=NULL){
cout<
if(top<100-1)
{
top++;
stack[top]=p;
}
else{
cout<<"栈溢出"< return0; } p=p->left_child; } if(top==-1) return1; else{ p=stack[top]; top--; p=p->right_child; } } return1;} //非递归中序遍历 intNRInOrder(BiTree&T) {BiTreestack[100],p; inttop; if(T==NULL) return1; top=-1; p=T; while(! (p==NULL&&top==-1)){ while(p! =NULL){ if(top<100-1) { top++; stack[top]=p; } else{ cout<<"栈溢出"< return0; } p=p->left_child; } if(top==-1) return1; else{ p=stack[top]; cout< top--; p=p->right_child; } } return1;} //层次遍历 voidLevelOrder(BiTree&T) {BiTreequeue[100]; intfront,rear; if(T==NULL) return; front=-1; rear=0; queue[rear]=T; while(front! =rear){ front++; cout< if(queue[front]->left_child! =NULL){ 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(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); } voidprint(BiTNode&T) { if(T! =NULL) { printf("%c",T->data); 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(")"); } } }*/ intPreOrderTraverse(BiTreeT) { if(! T) return0; BiTreet; if(T->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); return0; } //菜单 voidmenu() {cout<<"*****************************************************************************"< cout<<"<<(主菜单)>>"< cout<<"*****************************************************************************"< cout<<"<<0.建立二叉树>>"< cout<<"<<1.二叉树树深>>"< cout<<"<<2.二叉树结点数>>"< cout<<"<<3.二叉树的叶子结点>>"< cout<<"<<4.二叉树的先序遍历>>"< cout<<"<<5.二叉树的中序遍历>>"< cout<<"<<6.二叉树的后序遍历>>"< cout<<"<<7.二叉树的非递归先序遍历>>"< cout<<"<<8.二叉树的非递归中序遍历>>"< cout<<"<<9.二叉树的层次遍历>>"< cout<<"<<10.判断此树是否是完全二叉树>>"< cout<<"<<11.左右孩子交换>>"< cout<<"<<12.退出>>"< cout<<"*****************************************************************************"< } //主函数 voidmain() { intbr,a; BiTreeT; br=CreateBiTree(T); while (1){ menu(); cout<<"请输入选择的命令-->"; cin>>a; if(a>=0||a<=12) { switch(a){ case0: {cout<<"建立后的二叉树为: \n"; Output(T); cout< system("pause");break; case1: cout<<"该树的树深为: "< system("pause");break; case2: cout<<"该树的结点数: "< system("pause");break; case3: cout<<"该树的叶子结点为: "< system("pause");break; case4: {cout<<"该树以先序遍历输出为: \n"; PreOrder(T); cout< system("pause");break; case5: {cout<<"该树以中序遍历输出为: \n"; InOrder(T); cout< system("pause");break; case6: {cout<<"该树以后序遍历输出为: \n"; PostOrder(T); cout< system("pause");break; case7: {cout<<"该树的非递归先序遍历输出为: \n"; NRPreOrder(T); cout< system("pause");break; case8: {cout<<"该树的非递归中序遍历输出为: \n"; NRInOrder(T); cout< system("pause");break; case9: {cout<<"该树的层次遍历: \n"; LevelOrder(T); cout< system("pause");break; case10: { LevelOrder1(T); if(sign==1) { cout<<"这不是一个完全二叉树。 "< } else cout<<"这是一个完全二叉树。 "< //break; } system("pause");break; case11: PreOrderTraverse(T); cout<<"左右孩子已经替换成功"< case12: exit(-1); } } } } 4运行结果: 4.1用二叉链表创建二叉树: 4.2主菜单 4.3求二叉树树深: 4.4二叉树结点数: 4.5二叉树的中序遍历: 4.6二叉树的层次遍历: 4.7左右孩子交换: 4.8左右孩子交换后的中序遍历: 4.9左右孩子交换后的层次遍历: 5.课程设计总结: 此次课程设计使我对书本的知识有进一步了解。 同时也是我知道自己的一些不足,这次课程设计我是在书本与同学的帮助下完成的。 它并不难,但是我没有自己独自完成是我的错误。 6参考书目: 1.《数据结构》课本 2.《数据结构基本操作》
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 二叉 应用 数据结构 课程设计