C++二叉树基本操作实验报告.docx

上传人:b****8 文档编号:10696003 上传时间:2023-02-22 格式:DOCX 页数:15 大小:25.03KB
下载 相关 举报
C++二叉树基本操作实验报告.docx_第1页
第1页 / 共15页
C++二叉树基本操作实验报告.docx_第2页
第2页 / 共15页
C++二叉树基本操作实验报告.docx_第3页
第3页 / 共15页
C++二叉树基本操作实验报告.docx_第4页
第4页 / 共15页
C++二叉树基本操作实验报告.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

C++二叉树基本操作实验报告.docx

《C++二叉树基本操作实验报告.docx》由会员分享,可在线阅读,更多相关《C++二叉树基本操作实验报告.docx(15页珍藏版)》请在冰豆网上搜索。

C++二叉树基本操作实验报告.docx

C++二叉树基本操作实验报告

一、实验目的

选择二叉链式存储结构作为二叉树的存储结构,设计一个程序实现二叉树的基本操作(包括建立、输出、前序遍历、中序遍历、后序遍历、求树高、统计叶子总数等)

二、实验开发环境

Windows8.1中文版

MicrosoftVisualStudio6.0

三、实验内容

程序的菜单功能项如下:

1------建立一棵二叉树

2------前序遍历递归算法

3------前序遍历非递归算法

4------中序遍历递归算法

5------中序遍历非递归算法

6------后序遍历递归算法

7------后序遍历非递归算法

8------求树高

9------求叶子总数

10-----输出二叉树

11-----退出

四、实验分析

1、建立一棵二叉树

2、输入二叉树各节点数据

cout<<"请按正确顺序输入二叉树的数据:

";

cin.getline(t,1000);//先把输入的数据输入到一个t数组

3、递归前序遍历

voidBL1(ECS_data*t)

{

if(NULL!

=t)

{

cout<data<<",";

BL1(t->l);

BL1(t->r);

}

}

4、非递归前序遍历

voidpreOrder2(ECS_data*t)

{

stacks;

ECS_data*p=t;

while(p!

=NULL||!

s.empty())

{

while(p!

=NULL)

{

cout<data<<"";

s.push(p);

p=p->l;

}

if(!

s.empty())

{

p=s.top();

s.pop();

p=p->r;

}

}

}

5、递归中序遍历

voidBL2(ECS_data*t)

{

if(NULL!

=t)

{

BL2(t->l);

cout<data<<",";

BL2(t->r);

}

}

6、非递归中序遍历

voidinOrder2(ECS_data*t)//非递归中序遍历

{

stacks;

ECS_data*p=t;

while(p!

=NULL||!

s.empty())

{

while(p!

=NULL)

{

s.push(p);

p=p->l;

}

if(!

s.empty())

{

p=s.top();

cout<data<<"";

s.pop();

p=p->r;

}

}

}

7、递归后序遍历

voidBL3(ECS_data*t)

{

if(NULL!

=t)

{

BL3(t->l);

BL3(t->r);

cout<data<<",";

}

}

8、非递归后序遍历

voidpostOrder3(ECS_data*t)

{

stacks;

ECS_data*cur;//当前结点

ECS_data*pre=NULL;//前一次访问的结点

s.push(t);

while(!

s.empty())

{

cur=s.top();

if((cur->l==NULL&&cur->r==NULL)||

(pre!

=NULL&&(pre==cur->l||pre==cur->r)))

{

cout<data<<"";//如果当前结点没有孩子结点或者孩子节点都已被访问过

s.pop();

pre=cur;

}

else

{

if(cur->r!

=NULL)

s.push(cur->r);

if(cur->l!

=NULL)

s.push(cur->l);

}

}

}

9、求树高

intHeight(ECS_data*t)

{

if(t==NULL)return0;

else

{

intm=Height(t->l);

intn=Height(t->r);

return(m>n)?

(m+1):

(n+1);

}

}

10、求叶子总数

intCountLeaf(ECS_data*t)

{

staticintLeafNum=0;//叶子初始数目为0,使用静态变量

if(t)//树非空

{

if(t->l==NULL&&t->r==NULL)//为叶子结点

LeafNum++;//叶子数目加1

else//不为叶子结点

{

CountLeaf(t->l);//递归统计左子树叶子数目

CountLeaf(t->r);//递归统计右子树叶子数目

}

}

returnLeafNum;

}

五、运行结果

附:

完整程序源代码:

//二叉树链式存储的实现

#include

#include

#include

usingnamespacestd;

structECS_data//先定义好一个数据的结构

{

chardata;

ECS_data*l;

ECS_data*r;

};

classECS

{

private:

//intlevel;//树高

intn;//表示有多少个节点数

intn1;//表示的是数组的总长度值,(包括#),因为后面要进行删除判断

ECS_data*temp[1000];

public:

ECS_data*root;

ECS()//初始化

{

ECS_data*p;

chart[1000];inti;

intfront=0,rear=1;//front表示有多少个节点,rear表示当前插入的点的父母

cout<<"请按正确顺序输入二叉树的数据:

";

cin.getline(t,1000);//先把输入的数据输入到一个t数组

//cout<

intn1=strlen(t);//测量数据的长度

n=0;

for(i=0;i

{

if(t[i]!

='#')

{

p=NULL;

if(t[i]!

=',')//满足条件并开辟内存

{

n++;

p=newECS_data;

p->data=t[i];

p->l=NULL;

p->r=NULL;

}

front++;

temp[front]=p;

if(1==front){root=p;}

else

{

if((p!

=NULL)&&(0==front%2))

{

temp[rear]->l=p;//刚开始把这里写成了==

}

if((p!

=NULL)&&(1==front%2))

{

temp[rear]->r=p;

}

if(1==front%2)rear++;//就当前的数据找这个数据的父母

}

}

}

}

~ECS()//释放内存

{

inti;

for(i=1;i<=n;i++)

if(temp[i]!

=NULL)

deletetemp[i];

}

voidJS()//记录节点的个数

{

ints;

s=n;

cout<<"该二叉树的节点数为:

"<

}

voidBL1(ECS_data*t)//递归前序遍历

{

if(NULL!

=t)

{

cout<data<<",";

BL1(t->l);

BL1(t->r);

}

}

voidpreOrder2(ECS_data*t)//非递归前序遍历

{

stacks;

ECS_data*p=t;

while(p!

=NULL||!

s.empty())

{

while(p!

=NULL)

{

cout<data<<"";

s.push(p);

p=p->l;

}

if(!

s.empty())

{

p=s.top();

s.pop();

p=p->r;

}

}

}

voidBL2(ECS_data*t)//递归中序遍历

{

if(NULL!

=t)

{

BL2(t->l);

cout<data<<",";

BL2(t->r);

}

}

voidinOrder2(ECS_data*t)//非递归中序遍历

{

stacks;

ECS_data*p=t;

while(p!

=NULL||!

s.empty())

{

while(p!

=NULL)

{

s.push(p);

p=p->l;

}

if(!

s.empty())

{

p=s.top();

cout<data<<"";

s.pop();

p=p->r;

}

}

}

voidBL3(ECS_data*t)//递归后序遍历

{

if(NULL!

=t)

{

BL3(t->l);

BL3(t->r);

cout<data<<",";

}

}

voidpostOrder3(ECS_data*t)//非递归后序遍历

{

stacks;

ECS_data*cur;//当前结点

ECS_data*pre=NULL;//前一次访问的结点

s.push(t);

while(!

s.empty())

{

cur=s.top();

if((cur->l==NULL&&cur->r==NULL)||

(pre!

=NULL&&(pre==cur->l||pre==cur->r)))

{

cout<data<<"";//如果当前结点没有孩子结点或者孩子节点都已被访问过

s.pop();

pre=cur;

}

else

{

if(cur->r!

=NULL)

s.push(cur->r);

if(cur->l!

=NULL)

s.push(cur->l);

}

}

}

intHeight(ECS_data*t)//求树高

{

if(t==NULL)return0;

else

{

intm=Height(t->l);

intn=Height(t->r);

return(m>n)?

(m+1):

(n+1);

}

}

intCountLeaf(ECS_data*t)//求叶子总数

{

staticintLeafNum=0;//叶子初始数目为0,使用静态变量

if(t)//树非空

{

if(t->l==NULL&&t->r==NULL)//为叶子结点

LeafNum++;//叶子数目加1

else//不为叶子结点

{

CountLeaf(t->l);//递归统计左子树叶子数目

CountLeaf(t->r);//递归统计右子树叶子数目

}

}

returnLeafNum;

}

};

intmain()

{

ECSa;

a.JS();

cout<<"递归前序遍历:

";

a.BL1(a.root);

cout<

cout<<"非递归前序遍历:

";

a.preOrder2(a.root);

cout<

cout<<"递归中序遍历:

";

a.BL2(a.root);

cout<

cout<<"非递归中序遍历:

";

a.inOrder2(a.root);

cout<

cout<<"递归后序遍历:

";

a.BL3(a.root);

cout<

cout<<"非递归后序遍历:

";

a.postOrder3(a.root);

cout<

cout<<"树高为:

"<

cout<<"叶子总数为:

"<

return0;

}

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

当前位置:首页 > 考试认证 > 财会金融考试

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

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