大大数据结构实验报告材料

  • 格式:doc
  • 大小:109.16 KB
  • 文档页数:12

下载文档原格式

  / 12
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

数据结构实验报告

一.题目要求

1)编程实现二叉排序树,包括生成、插入,删除;

2)对二叉排序树进行先根、中根、和后根非递归遍历;

3)每次对树的修改操作和遍历操作的显示结果都需要在屏幕上用树的形状表示出来。4)分别用二叉排序树和数组去存储一个班(50人以上)的成员信息(至少包括学号、姓名、成绩3项),对比查找效率,并说明在什么情况下二叉排序树效率高,为什么?

二.解决方案

对于前三个题目要求,我们用一个程序实现代码如下

#include

#include

#include

#include "Stack.h" //栈的头文件,没有用上

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(keydata){

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

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

printf(" ");

}

printf("%d\n",T->data);

printtree(T->lChild,nlayer+1);

}

void PreOrderNoRec(BiTree root)//先序非递归遍历{

BiTree p=root;

BiTree stack[50];

int num=0;

while(NULL!=p||num>0)

{

while(NULL!=p)

{

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

stack[num++]=p;

p=p->lChild;

}

num--;

p=stack[num];

p=p->rChild;

}

printf("\n");

}

void InOrderNoRec(BiTree root)//中序非递归遍历{

BiTree p=root;