1 / 12
文档名称:

数据结构实验报告..docx

格式:docx   大小:118KB   页数:12页
下载后只包含 1 个 DOCX 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

分享

预览

数据结构实验报告..docx

上传人:q1188830 2018/6/3 文件大小:118 KB

下载得到文件列表

数据结构实验报告..docx

相关文档

文档介绍

文档介绍:数据结构实验报告
题目要求
1) 编程实现二叉排序树,包括生成、插入,删除;
2) 对二叉排序树进行先根、中根、和后根非递归遍历;
3) 每次对树的修改操作和遍历操作的显示结果都需要在屏幕上用树的形状表示出来。
4)分别用二叉排序树和数组去存储一个班(50人以上)的成员信息(至少包括学号、姓名、成绩3项),对比查找效率,并说明在什么情况下二叉排序树效率高,为什么?
解决方案
对于前三个题目要求,我们用一个程序实现代码如下
#include<>
#include <>
#include <>
#include "" //栈的头文件,没有用上
typedef int ElemType; //数据类型
typedef int Status; //返回值类型
//定义二叉树结构
typedef struct BiTNode{
ElemType data; //数据域
struct BiTNode *lChild, *rChild;//左右子树域
}BiTNode, *BiTree;
int InsertBST(BiTree &T,int key){//插入二叉树函数
if(T==NULL)
{
T = (BiTree)malloc(sizeof(BiTNode));
T->data=key;
T->lChild=T->rChild=NULL;
return 1;
}
else if(key<T->data){
InsertBST(T->lChild,key);
}
else if(key>T->data){
InsertBST(T->rChild,key);
}
else
return 0;
}
BiTree CreateBST(int a[],int n){//创建二叉树函数
BiTree bst=NULL;
int i=0;
while(i<n){
InsertBST(bst,a[i]);
i++;
}
return bst;
}
int Delete(BiTree &T)
{
BiTree q,s;
if(!(T)->rChild){ //右子树为空重接它的左子树
q=T;
T=(T)->lChild;
free(q);
}else{
if(!(T)->lChild){ //若左子树空则重新接它的右子树
q=T;
T=(T)->rChild;
}else{
q=T;
s=(T)->lChild;
while(s->rChild){
q=s; s=s->rChild;
}
(T)->data=s->data; //s指向被删除结点的前驱
if(q!=T)
q->rChild=s->lChild;
else
q->lChild=s->lChild;
free(s);
}
}
return 1;
}
//删除函数,在T中删除key元素
int DeleteBST(BiTree &T,int key){
if(!T) return 0;
else{
if(key==(T)->data) return Delete(T);
else{
if(key<(T)->data)
return DeleteBST(T->lChild,key);
else
return DeleteBST(T->rChild,key);
}
}
}
int PosttreeDepth(BiTree T){//求深度
int hr,hl,max;
if(!T==NULL){
hl=PosttreeDepth(T->lChild);
hr=PosttreeDepth(T->rChild);
max=hl>hr?hl:hr;
return max+1;
}
else
return 0;

}
void printtree(BiTree T,int nlayer){//打印二叉树
if(T==NULL) return ;
printtree(T->rChild,nlayer+1);
for(int i=0;i<nlayer;i++){
printf(" ");
}
printf("%d\n",T->data);
printtree(T->lChild,nlayer+1);
}
void PreOrderNoRec(BiTree root)//先