09级软件工程一班数据结构课程设计.docx

上传人:b****8 文档编号:10982284 上传时间:2023-02-24 格式:DOCX 页数:27 大小:162.79KB
下载 相关 举报
09级软件工程一班数据结构课程设计.docx_第1页
第1页 / 共27页
09级软件工程一班数据结构课程设计.docx_第2页
第2页 / 共27页
09级软件工程一班数据结构课程设计.docx_第3页
第3页 / 共27页
09级软件工程一班数据结构课程设计.docx_第4页
第4页 / 共27页
09级软件工程一班数据结构课程设计.docx_第5页
第5页 / 共27页
点击查看更多>>
下载资源
资源描述

09级软件工程一班数据结构课程设计.docx

《09级软件工程一班数据结构课程设计.docx》由会员分享,可在线阅读,更多相关《09级软件工程一班数据结构课程设计.docx(27页珍藏版)》请在冰豆网上搜索。

09级软件工程一班数据结构课程设计.docx

09级软件工程一班数据结构课程设计

湖南人文科技学院计算机科学技术系

 

课程设计说明书

 

课程名称:

C++程序设计

题目:

二叉树操作

年级/专业/班:

09级计算机系软件工程一班

学生姓名:

赵杰唐磊刘文飞郑宇翔

学号:

094361100943610209436111

09436130

指导教师:

唐海波

开题时间:

2010年12月20日

完成时间:

2010年12月28日

 

目录

一设计课题-1-

二设计内容-2-

1、概要设计-2-

1.1头文件-2-

1.2宏定义-2-

1.3抽象数据类型定义-2-

1.4操作具体实现的函数-3-

1.5其他模块实现函数-3-

2、详细设计-4-

2.1将二叉树中所有结点的左右子树交换-4-

2.2求二叉树高度、分支结点数和叶子结点数-4-

2.4插入结点到指定位置、删除指定结点-5-

2.5对二叉树进行层序、非递归中序遍历-5-

三程序清单-6-

四程序调试与体会-6-

五运行结果-6-

一设计课题

树是一种重要的非线性数据结构,直观地看,它是数据元素(在树中称为结点)按分支关系组织起来的结构,很象自然界中的树那样。

二叉树是树形结构中的一种。

我们此次设计的课题就是二叉树的一些基本操作。

内容分别是:

已知二叉树的前序、中序序列,恢复此二叉树;求二叉树高度、分支结点数和叶子结点数;插入结点到指定位置、删除指定结点;将二叉树中所有结点的左右子树交换;对二叉树进行层序、非递归中序遍历。

DesignIssues

Treeisanimportantnon-lineardatastructure,intuitively,itisthedataelements(callednodesinthetree)organizedbythebranchbetweenthestructure,muchlikethetreeinthewayofnature.Binarytreeisatreestructure.Ourtopicisthedesignofsomeofthebasicbinaryoperations.Contentsare:

thefirstknownbinarysequence,thesequenceordertorestorethebinarytree;findbinarytreeheight,branchnodesandleafnodes;insertionnodetothespecifiedlocation,removethespecifiednode;willBinarytreeallnodesintheleftandrightsub-treeexchange;sequenceofbinarytreefornon-recursiveinordertraversal.

二设计内容

1、概要设计

1.1头文件

#include"math.h"

#include"malloc.h"

#include"stdio.h"

#include"conio.h"

#include"stdlib.h"

1.2宏定义

#definestackinitsize100

#defineOK1

#defineERROR0

#defineOVERFLOW-1

1.3抽象数据类型定义

typedefintTElemType;

typedefintStatus;

//一一一一一二叉树的二叉链表存储表示一一一一一

typedefstructBiTNode{//二叉树结构体

TElemTypedata;

structBiTNode*lchild,*rchild,*node;//左右孩子指针

}BiTNode,*SElemType,*BiTree;

typedefstruct{

//该堆栈的元素是指针类型的

//base和top均是指向指针的指针

SElemType*base;

SElemType*top;

intstacksize;

}sqstack;//堆栈结构

1.4操作具体实现的函数

T,Status(*Visit)(TElemTypee))

//层序遍历

StatusExchange(BiTNode*t)//交换左右子树

StatusLeaves_Num(BiTNode*t)//计算叶子结点个数

voidBranch_Num(BiTNode*t)//计算分支结点个数

Statusheight(BiTNode*t)//二叉树的高度

StatusInsert(BiTNode*T,chari,TElemTypee)//插入结点

voidDelete(BiTNode*T,chari)//删除结点

StatusInorderTraverseNoRecursion1(BiTreeT,Status(*Visit)(TElemTypee))//中序非递归算法

1.5其他模块实现函数

StatusInitStack(sqstack&s)//初始化堆栈

intStackEmpty(sqstack&s)//栈空判别

StatusPop(sqstack&s,SElemType&e)//弹栈(若栈不为空,删除栈顶元素,用e返回其值)

StatusGetTop(sqstack&s,SElemType&e)

StatusPush(sqstack&s,SElemTypee)//将元素压入堆栈

StatusPrintElement(TElemTypee)//应用函数

StatusPreOrderCreateBiTree(BiTree&T){//按先序次序构造二叉树

//按先序次序输入二叉树中结点的值(一个字符),空格字符表示空树。

//构造二叉链表表示的二叉树T.

2、详细设计

2.1将二叉树中所有结点的左右子树交换

//交换左右子树

StatusExchange(BiTNode*t)

{

BiTNode*temp;

if(t)

{

Exchange(t->lchild);

Exchange(t->rchild);

temp=t->lchild;

t->lchild=t->rchild;

t->rchild=temp;returnOK;

}

returnERROR;

}

2.2求二叉树高度、分支结点数和叶子结点数

//计算叶子结点个数

StatusLeaves_Num(BiTNode*t)

{if(t){if(t->lchild==NULL&&t->rchild==NULL)returnOK;

return(Leaves_Num(t->lchild)+Leaves_Num(t->rchild));

}

elsereturnERROR;

}

//计算叶子结点个数

StatusLeaves_Num(BiTNode*t)

{if(t){if(t->lchild==NULL&&t->rchild==NULL)returnOK;

return(Leaves_Num(t->lchild)+Leaves_Num(t->rchild));

}

elsereturnERROR;

}

//计算分支结点个数

inta=0;

voidBranch_Num(BiTNode*t){

if(t){

if(t->lchild!

=NULLt->rchild!

=NULL)a++,

Branch_Num(t->lchild),Branch_Num(t->rchild);

}

}

//计算二叉树的高度

Statusheight(BiTNode*t)

{if(!

t)returnERROR;

inthl=height(t->lchild);

inthr=height(t->rchild);

if(hl>hr)return++hl;

elsereturn++hr;

}

2.3已知二叉树的前序、中序序列,恢复此二叉树

2.4插入结点到指定位置、删除指定结点

2.5对二叉树进行层序、非递归中序遍历

StatusInorderTraverseNoRecursion1(BiTreeT,Status(*Visit)(TElemTypee))//中序非递归算法-------1

//采用二叉链表存储结构,visit是对数据元素操作的应用函数。

//中序遍历二叉树T的非递归算法,对每个数据元素调用函数visit。

{sqstacks;

BiTreep;

InitStack(s);p=T;

while(p||!

StackEmpty(s))

{

if(p){Push(s,p);p=p->lchild;}//根指针进栈,遍历左子树

else{//根指针退栈,访问根结点,遍历右子树

Pop(s,p);

if(!

Visit(p->data))returnERROR;

p=p->rchild;

}//else

}//while

returnOK;

}//InorderTraVerse1

 

三程序清单

#include"math.h"

#include"malloc.h"

#include"stdio.h"

#include"conio.h"

#include"stdlib.h"

#include

#definestackinitsize100

#defineOK1

#defineERROR0

#defineOVERFLOW-1

charprelist[stackinitsize];

charinlist[stackinitsize];

typedefintTElemType;

typedefintStatus;

//一一一一一二叉树的二叉链表存储表示一一一一一

typedefstructBiTNode{//二叉树结构体

chardata;

structBiTNode*lchild,*rchild,*node;//左右孩子指针

}BiTNode,*SElemType,*BiTree;

typedefstruct{

//该堆栈的元素是指针类型的

//base和top均是指向指针的指针

SElemType*base;

SElemType*top;

intstacksize;

}sqstack;//堆栈结构

StatusInitStack(sqstack&s)//初始化堆栈

{

s.base=(SElemType*)malloc(100*sizeof(SElemType));

if(!

s.base)returnNULL;

s.top=s.base;

s.stacksize=100;

returnOK;

}

intStackEmpty(sqstack&s)//栈空判别

{return(s.top==s.base);}

StatusPop(sqstack&s,SElemType&e)//弹栈(若栈不为空,删除栈顶元素,用e返回其值)

{if(s.top==s.base)returnERROR;

e=*--s.top;returnOK;}

StatusGetTop(sqstack&s,SElemType&e)//若栈不为空,用e返回栈顶元素

{if(s.top==s.base)returnERROR;

e=*(s.top-1);

returnOK;

}

StatusPush(sqstack&s,SElemTypee)//将元素压入堆栈

{if(s.top-s.base>=s.stacksize)//栈满,追加存储空间

{s.base=(SElemType*)realloc(s.base,(s.stacksize+10)*sizeof(SElemType));

if(!

s.base)exit(OVERFLOW);//存储分配失败

s.top=s.base+s.stacksize;

s.stacksize+=10;

}

*s.top++=e;

returnOK;

}

StatusPrintElement(TElemTypee)//应用函数

{//输出元素e的值

printf("%2c",e);

returnOK;

}

StatusPreOrderCreateBiTree(BiTree&T){//按先序次序构造二叉树

//按先序次序输入二叉树中结点的值(一个字符),空格字符表示空树。

//构造二叉链表表示的二叉树T.

charch;

ch=getchar();

if(ch=='')T=NULL;

else{

if(!

(T=(BiTNode*)malloc(sizeof(BiTNode))))exit(OVERFLOW);

T->data=ch;//生成根结点

PreOrderCreateBiTree(T->lchild);//构造左子树

PreOrderCreateBiTree(T->rchild);//构造右子树

}

returnOK;

}//CreateBiTree

BiTNode*preintotree(charpre[100],charin[100],inti,intj,intk,intl)//已知前序、后序序列恢复二叉树

{

intm;

BiTNode*p;

p=(BiTNode*)malloc(sizeof(BiTNode));

p->data=pre[i];

m=k;

while(in[m]!

=pre[i])

m++;

if(m==k)p->lchild=NULL;

elsep->lchild=preintotree(pre,in,i+1,i+m-k,k,m-1);

if(m==j)p->rchild=NULL;

elsep->rchild=preintotree(pre,in,i+m-k+1,j,m+1,l);

returnp;

}

StatusPreOrderTraverse(BiTreeT,Status(*Visit)(TElemTypee))//先序遍历

//采用二叉链表存储结构,visit是对数据元素操作的应用函数,

//先序遍历二叉树T的递归算法,对每个数据元素调用函数visit。

{

if(T)

{if(Visit(T->data))

if(PreOrderTraverse(T->lchild,Visit))

if(PreOrderTraverse(T->rchild,Visit))returnOK;

returnERROR;

}

elsereturnOK;

}//preOrderTraVerse

StatusInOrderTraverse(BiTreeT,Status(*Visit)(TElemTypee))//中序遍历

//采用二叉链表存储结构,visit是对数据元素操作的应用函数,

//中序遍历二叉树T的递归算法,对每个数据元素调用函数visit。

{

if(T){

if(InOrderTraverse(T->lchild,Visit))

if(Visit(T->data))

if(InOrderTraverse(T->rchild,Visit))returnOK;

returnERROR;

}

elsereturnOK;

//printf("\n");

}//preOrderTraVerse

StatusPostOrderTraverse(BiTreeT,Status(*Visit)(TElemTypee))//后序遍历

//采用二叉链表存储结构,visit是对数据元素操作的应用函数,

//后序遍历二叉树T的递归算法,对每个数据元素调用函数visit。

{

if(T){

if(PostOrderTraverse(T->lchild,Visit))

if(PostOrderTraverse(T->rchild,Visit))

if(Visit(T->data))returnOK;

returnERROR;

}

elsereturnOK;

//printf("\n");

}//preOrderTraVerse

voidLevelOrderTranslever(BiTNode*T)//层序遍历

{

structnode

{

BiTNode*vec[20];

intf,r;//r为队尾,f为队头

}queue;

BiTNode*p;

p=T;

queue.f=0;

queue.r=0;

if(T)

printf("%c",p->data);

queue.vec[queue.r]=p;

queue.r=queue.r+1;

while(queue.f

{

p=queue.vec[queue.f];

queue.f=queue.f+1;

if(p->lchild)

{

printf("%c",p->lchild->data);

queue.vec[queue.r]=p->lchild;

queue.r=queue.r+1;

}

if(p->rchild)

{

printf("%c",p->rchild->data);

queue.vec[queue.r]=p->rchild;

queue.r=queue.r+1;

}

}

printf("\n");

}

StatusExchange(BiTNode*t)//交换左右子树

{

BiTNode*temp;

if(t)

{

Exchange(t->lchild);

Exchange(t->rchild);

temp=t->lchild;

t->lchild=t->rchild;

t->rchild=temp;returnOK;

}

returnERROR;

}

StatusLeaves_Num(BiTNode*t)//计算叶子结点个数

{if(t){if(t->lchild==NULL&&t->rchild==NULL)returnOK;

return(Leaves_Num(t->lchild)+Leaves_Num(t->rchild));

}

elsereturnERROR;

}

inta=0;

voidBranch_Num(BiTNode*t)//计算分支结点个数

{

if(t){

if(t->lchild!

=NULL||t->rchild!

=NULL)a++,Branch_Num(t->lchild),Branch_Num(t->rchild);

}

}

Statusheight(BiTNode*t)//二叉树的高度

{if(!

t)returnERROR;

inthl=height(t->lchild);

inthr=height(t->rchild);

if(hl>hr)return++hl;

elsereturn++hr;

}

/*BiTNode*Search(BiTNode*T,chari)//查询结点

{if(T!

=NULL)

{if(T->contents==i)return1;

if(T->lchild!

=NULL)

if(T->lchild->contents==i)return2;

if(T->rchild!

=NULL)

if(T->rchild->contents==i)return3;

Search(T->lchild,i);

Search(T->rchild,i);

}

}

BiTNode*Search(BiTNode*T,inti)//按给定值在二叉排序树中查询

{

BiTNode*p;

if(T==NULL)p=NULL;

else{if(i==T->data)p=T;

elseif(i>T->data)p=Search(i,T->rchild);

elsep=Search(i,T->lchild);

returnp;

}

}

voidSearch_bst(BiTNode*T)

{ints;

BiTNode*p;

char[5];

printf("\n");

printf("请输入要查询的结点值:

");

scanf("%d",&s);

if(s!

=0)

{p=Search(T,s);

if(p=NULL)printf("该结点值不存在!

\n");

elseprintf("该结点值存在!

\n");

}

}

/*{if(!

T)returnERROR;

elseif(elem==T->data)returnOK;

elseif(elem%2==0)Search(T->rchild,elem,T,p);

elseSearch(T->rchild,elem,T,p);

}*/

/*StatusInsert(BiTNode*T,chari,TElemTypee)//插入结点

{

BiTNod

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

当前位置:首页 > 高等教育 > 经济学

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

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