数据结构课程设计二叉树的遍历Word文档格式.docx

上传人:b****2 文档编号:14977700 上传时间:2022-10-26 格式:DOCX 页数:15 大小:137.46KB
下载 相关 举报
数据结构课程设计二叉树的遍历Word文档格式.docx_第1页
第1页 / 共15页
数据结构课程设计二叉树的遍历Word文档格式.docx_第2页
第2页 / 共15页
数据结构课程设计二叉树的遍历Word文档格式.docx_第3页
第3页 / 共15页
数据结构课程设计二叉树的遍历Word文档格式.docx_第4页
第4页 / 共15页
数据结构课程设计二叉树的遍历Word文档格式.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

数据结构课程设计二叉树的遍历Word文档格式.docx

《数据结构课程设计二叉树的遍历Word文档格式.docx》由会员分享,可在线阅读,更多相关《数据结构课程设计二叉树的遍历Word文档格式.docx(15页珍藏版)》请在冰豆网上搜索。

数据结构课程设计二叉树的遍历Word文档格式.docx

该程序集成了如下功能:

(1)二叉树的建立

(2)递归和非递归先序,中序和后序遍历二叉树

(3)按层次遍历二叉树

(4)交换二叉树的左右子树

(5)输出叶子结点

(6)递归和非递归计算叶子结点的数目

1.2需求分析

分先序遍历,中序遍历和后序遍历三种情况考虑。

1.先序遍历,当二叉树非空时按以下顺序遍历,否则结束操作:

1 访问根结点;

2 按先序遍历规则遍历左子树;

3 按先序遍历规则遍历右子树;

2.中序遍历,当二叉树非空时按以下顺序遍历,否则结束操作:

1 按中序遍历规则遍历左子树;

2 访问根结点;

3 按中序遍历规3遍历右子树。

3.后序遍历,当二叉树非空时按以下顺序遍历,否则结束操作:

1 按后序遍历规则遍历左子树;

2 按后序遍历规则遍历右子树;

3 访问根结点。

1.3设计内容和要求

对任意给定的二叉树(顶点数自定)建立它的二叉链表存贮结构,并利用栈的五种基本运算(清空堆栈、压栈、弹出、取栈顶元素、判栈空)实现二叉树的先序、中序、后序三种周游,输出三种周游的结果。

1.4流程图及结构图

YES

NO

图1.1流程图

b

c

d

e

f

a

图1.2二叉链表存储结构模拟图

2.概要设计

2.1数据结构设计:

1. 

二叉树结点数据类型定义为:

template 

<

typename 

T>

struct 

BiNode 

BiNode<

*rchild,*lchild;

//指向左孩子的指针

data;

//结点数据信息 

};

2. 

二叉树数据类型定义为:

class 

BiTree 

friend 

ostream 

&

operator 

(ostream 

os 

BiTree<

bt);

public:

BiTree();

//无参构造函数 

BiTree(int 

m){};

//有参空构造函数 

BiTree(T 

ary[],int 

num,T 

none);

//有参构造函数 

//析构函数 

void 

preorder();

//递归前序遍历 

inorder();

//递归中序遍历 

postorder();

//递归后续遍历 

levelorder();

//层序遍历 

int 

count();

//计算二叉树的结点数 

display(ostream 

os);

//打印二叉树,有层次 

LevelNum();

//计算每一层结点数 

PreOrder();

//非递归前序遍历 

PostOrder();

//非递归后序遍历 

creat();

//创建二叉树 

protected:

//以下函数供上面函数调用 

//对应相同功能 

Voidcreat(BiNode<

*&

root);

//创建 

release(BiNode<

//删除 

Build(T 

none,int 

idx);

//用数组创建二叉树 

PreOrder(BiNode<

//前序遍历 

PostOrder(BiNode<

//后续遍历 

LevelNum(BiNode<

preorder(BiNode<

inorder(BiNode<

postorder(BiNode<

levelorder(BiNode<

*root);

count(BiNode<

//计算结点数 

display(ostream&

os,BiNode<

root,int 

dep);

//打印 

static 

bool 

leastCommanAncestor(BiNode<

*root, 

va, 

vb, 

private:

*rootptr;

2.2源程序代码

#include<

iostream>

usingnamespacestd;

//*************************************************************************************

//二叉树结点类的定义

template<

classT>

structBTNode

{

Tdata;

BTNode<

*Lchild,*Rchild;

BTNode(TnodeValue=T(),BTNode<

*leftNode=NULL,BTNode<

*rightNode=NULL)

:

data(nodeValue),Lchild(leftNode),Rchild(rightNode){}//可选择参数的默认构造函数

//**************************************************************************************

//二叉树的建立

template<

voidcreateBinTree(BTNode<

*&

root)

BTNode<

*p=root;

*k;

TnodeValue;

cin>

>

nodeValue;

if(nodeValue==-1)

{

root=NULL;

}

else

root=newBTNode<

();

root->

data=nodeValue;

createBinTree(root->

Lchild);

Rchild);

}

//************************************************************************************

//二叉树的先序遍历

voidpreOrder(BTNode<

p)

if(p)

cout<

p->

data<

"

"

;

preOrder(p->

//二叉树的中序遍历

voidinOrder(BTNode<

inOrder(p->

//二叉树的后序遍历

voidlevelOrder(BTNode<

*&

levelOrder(p->

//统计二叉树中结点的个数

intcountNode(BTNode<

if(p==NULL)return0;

return1+countNode(p->

Lchild)+countNode(p->

//***********************************************************************************

//求二叉树的深度

intdepth(BTNode<

if(p==NULL)

return-1;

inth1=depth(p->

inth2=depth(p->

if(h1>

h2)return(h1+1);

returnh2+1;

//二叉树的消毁操作

BTNode<

*destroy(BTNode<

*p)//消毁函数,用来消毁二叉树中的各个结点

returndestroy(p->

deletep;

//

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

当前位置:首页 > 人文社科

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

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