数据结构课程设计二叉树遍历查找
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
课程设计任务书
2011 —2012 学年第一学期
电子与信息工程系计算机专业09计算机一班班级
课程设计名称:数据结构课程设计
设计题目:排序二叉树的遍历
完成期限:自2012 年 1 月 2 日至2012 年 1 月 6 日共 1 周
设计依据、要求及主要内容(可另加附页):
一、设计目的
熟悉各种数据结构和运算,会使用数据结构的基本操作解决一些实际问题。
二、设计要求
(1)重视课程设计环节,用严谨、科学和踏实的工作态度对待课程设计的每一项任务;
(2)按照课程设计的题目要求,独立地完成各项任务,严禁抄袭;凡发现抄袭,抄袭者与被抄袭者皆以零分计入本课程设计成绩。凡发现实验报告或源程序雷同,涉及的全部人员皆以零分计入本课程设计成绩;
(3)学生在接受设计任务后,首先要按设计任务书的要求编写设计进程表;
(4)认真编写课程设计报告。
三、设计内容
排序二叉树的遍历(用递归或非递归的方法都可以)
1)问题描述
输入树的各个结点,建立排序二叉树,对建立的排序二叉树进行层次、先序、中序和后序遍历并统计该二叉树中叶子结点的数目。
2)基本要求
(1)用菜单实现
(2)能够输入树的各个结点,并能够输出用不同方法遍历的遍历序列和叶子结点的数目。
四、参考文献
1.王红梅.数据结构.清华大学出版社
2.王红梅.数据结构学习辅导与实验指导.清华大学出版社3.严蔚敏,吴伟民.数据结构(C语言版).清华大学出版社
#include
using namespace std;
int num;
//-----------排序二叉树节点--------------//
struct tree //定义二叉树节点结构
{
int data; //节点数据域
tree *right,*left; //右,左子树指针
};
//-----------排序二叉树类----------------//
class Btree
{
tree *root;//根节点
public:
Btree()
{
root=NULL;//根节点在构造函数里初始化}
void create_btree(int);//创建排序二叉树void display1()
{
preorder(root);//前序
cout< } void display2() { inorder(root);//中序 cout< } void display3() { postorder(root);//后序 cout< } void display4() { leverorder(root);//层序 cout< } void display5() { leafnum(root);//结点 cout< } void preorder(tree *);//前序 void inorder(tree *);//中序 void postorder(tree *);//后序 void leverorder(tree *);//层序 void leafnum(tree *);//结点 void empty( ); int printnum( ); }; void Btree::create_btree(int t) { tree *newnode=new tree;//创建新的节点存入数据并插入二叉树中newnode->data =t; //存入数据t newnode->left =NULL; newnode->right =NULL; if(root==NULL) //当是根节点为空时即二叉树中没有任何数据时{ root=newnode; //根节点为新节点 } else //当有二叉树中拥有数据后 { tree *back; tree *current; current=root; while(current!=NULL) { back=current; //记住current的父节点 if(current->data>t) current=current->left; else current=current->right ; } if(back->data>t) back->left =newnode; else back->right =newnode; } } void Btree::preorder (tree* tmp)//前序 { if(tmp!=NULL) { cout< preorder(tmp->left); preorder(tmp->right); } } void Btree::inorder (tree* tmp)//中序{ if(tmp!=NULL) { inorder(tmp->left); cout< inorder(tmp->right ); } } void Btree::postorder (tree* tmp)//后序{ if(tmp!=NULL) { postorder(tmp->left); postorder(tmp->right); cout< } } void Btree::leverorder (tree* tmp)//层序{ const int maxsize = 100; int front =0; int rear = 0;