文档介绍:数据结构实验报告一. 题目要求 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)// 先序非递归遍历{ 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];