北京交通大学数据结构上机实验5.docx
- 文档编号:28090423
- 上传时间:2023-07-08
- 格式:DOCX
- 页数:40
- 大小:25.02KB
北京交通大学数据结构上机实验5.docx
《北京交通大学数据结构上机实验5.docx》由会员分享,可在线阅读,更多相关《北京交通大学数据结构上机实验5.docx(40页珍藏版)》请在冰豆网上搜索。
北京交通大学数据结构上机实验5
数据结构上机实验五
实验内容:
查找表和内部排序的基本算法
实验要求:
1)基本操作要作为函数被调用,做到模块化.
2)基本上实现每个实验题目的要求.
分组要求:
可单独完成,也可两人一组。
实验目的:
1)熟悉C/C++基本编程,培养动手能力.
2)通过实验,加深对查找算法的理解.
评分标准:
1)只完成第一和第二题,根据情况得4,5-分;
2)完成前3题,根据情况得5,6分;
3)在2)基础上,选做四)中题目,根据情况得6,7分。
题目:
一)顺序表与有序表的查找
(1)建立一个顺序表,利用教材9.1.1的顺序查找算法进行查找;
#include"stdio.h"
#include"stdlib.h"
typedefstructnode{
intkey;
}keynode;
typedefstructNode{
keynoder[50];
intlength;
}list,*sqlist;
intCreatesqlist(sqlists)
{
inti;
printf("请输入您要输入的数据的个数:
\n");
scanf("%d",&(s->length));
printf("请输入您想输入的%d个数据;\n",s->length);
for(i=0;i
scanf("%d",&(s->r[i].key));
printf("\n");
printf("您所输入的数据为:
\n\n");
for(i=0;i
printf("%-5d",s->r[i].key);
printf("\n\n");
return1;
}
intsearchsqlist(sqlists,intk)
{
inti=0;
s->r[s->length].key=k;
while(s->r[i].key!
=k)
{
i++;
}
if(i==s->length)
{
printf("该表中没有您要查找的数据!
\n");
return0;
}
else
returni+1;
}
sqlistInitlist(void)
{
sqlistp;
p=(sqlist)malloc(sizeof(list));
if(p)
returnp;
else
returnNULL;
}
main()
{
intkeyplace,keynum;//
sqlistT;//
T=Initlist();
Createsqlist(T);
printf("请输入您想要查找的数据的关键字:
\n\n");
scanf("%d",&keynum);
printf("\n");
keyplace=searchsqlist(T,keynum);
if(keyplace>0)
printf("您要查找的数据的位置为:
\n\n%d\n\n",keyplace);
return2;
}
(2)建立一个有序表,利用折半法进行查找;
#include"stdio.h"
#include"stdlib.h"
typedefstructnode{
intkey;
}keynode;
typedefstructNode{
keynoder[50];
intlength;
}list,*sqlist;
intCreatesqlist(sqlists)
{
inti;
printf("请输入您要输入的数据的个数:
\n");
scanf("%d",&(s->length));
printf("请由小到大输入%d个您想输入的个数据;\n\n",s->length);
for(i=0;i
scanf("%d",&(s->r[i].key));
printf("\n");
printf("您所输入的数据为:
\n\n");
for(i=0;i
printf("%-5d",s->r[i].key);
printf("\n\n");
return1;
}
intsearchsqlist(sqlists,intk)
{
intlow,mid,high;
low=0;
high=s->length-1;
while(low<=high)
{
mid=(low+high)/2;
if(s->r[mid].key==k)
returnmid+1;
elseif(s->r[mid].key>k)
high=mid-1;
else
low=mid+1;
}
printf("该表中没有您要查找的数据!
\n");
return0;
}
sqlistInitlist(void)
{
sqlistp;
p=(sqlist)malloc(sizeof(list));
if(p)
returnp;
else
returnNULL;
}
main()
{
intkeyplace,keynum;//
sqlistT;//
T=Initlist();
Createsqlist(T);
printf("请输入您想要查找的数据的关键字:
\n\n");
scanf("%d",&keynum);
printf("\n");
keyplace=searchsqlist(T,keynum);
if(keyplace>0)
printf("您要查找的数据的位置为:
\n\n%d\n\n",keyplace);
}
(3)试将把
(2)中的折半法改用递归算法实现;
#include"stdio.h"
#include"stdlib.h"
typedefstructnode{
intkey;
}keynode;
typedefstructNode{
keynoder[50];
intlength;
}list,*sqlist;
intCreatesqlist(sqlistT)
{
inti;
printf("请输入您要输入的数据的个数:
\n");
scanf("%d",&(T->length));
printf("请由小到大输入%d个您想输入的个数据;\n\n",T->length);
for(i=0;i
scanf("%d",&(T->r[i].key));
printf("\n");
printf("您所输入的数据为:
\n\n");
for(i=0;i
printf("%-5d",T->r[i].key);
printf("\n\n");
return1;
}
intsearchsqlist(sqlistT,intkey,intlow,inthigh)
{
if(low>high)return0;//查找不到时返回0
intmid=(low+high)/2;
if(T->r[mid].key==key)
returnmid;
elseif(T->r[mid].key>key)
returnsearchsqlist(T,key,low,mid-1);
else
returnsearchsqlist(T,key,mid+1,high);
}
sqlistInitlist(void)
{
sqlistT;
T=(sqlist)malloc(sizeof(list));
if(T)
returnT;
else
returnNULL;
}
main()
{
intkeyplace;//
intkey,low,high;
sqlistT;//
T=Initlist();
Createsqlist(T);
printf("请输入您想要查找的数据的关键字:
\n\n");
scanf("%d",&key);
printf("\n");
low=0;
high=T->length;
keyplace=searchsqlist(T,key,low,high);
if(keyplace>0)
printf("您要查找的数据的位置为:
\n\n%d\n\n",keyplace+1);
elseprintf("该表中没有您要查找的数据!
\n");
}
二)二叉排序树的一些基本操作
(1)利用二叉链表的存储形式,从键盘输入建立一棵二叉排序树;
(2)对
(1)中建立的二叉排序树进行中序遍历并打印;
#include
usingnamespacestd;
typedefintKeyType;
typedefstructNode{
KeyTypekey;
structNode*lchild,*rchild;
}BSTNode,*BSTree;
voidInsertBST(BSTree*bst,KeyTypekey){
BSTrees;
if(*bst==NULL)/*递归结束条件*/
{
s=newBSTNode;
s->key=key;
s->lchild=NULL;
s->rchild=NULL;
*bst=s;
}
else
if(key<(*bst)->key)
InsertBST(&((*bst)->lchild),key);/*将s插入左子树*/
else
if(key>(*bst)->key)
InsertBST(&((*bst)->rchild),key);/*将s插入右子树*/
}
voidCreateBST(BSTree*bst){
KeyTypekey;
*bst=NULL;
scanf("%d",&key);
while(key!
=0)
{
InsertBST(bst,key);
scanf("%d",&key);
}
}
voidInOrder(BSTreeroot){
if(root!
=NULL)
{
InOrder(root->lchild);
printf("%d",root->key);
InOrder(root->rchild);
}
}
BSTNode*DelBST(BSTreeT,KeyTypex){
BSTNode*p,*f,*s,*q;
p=T;
f=NULL;
while(p)/*查找关键字为x的待删结点p*/
{
if(p->key==x)break;
f=p;/*f指向p结点的双亲结点*/
if(p->key>x)
p=p->lchild;
else
p=p->rchild;
}
if(p==NULL)returnT;/*若找不到,返回原来的二叉排序树*/
if(p->lchild==NULL)/*p无左子树*/
{
if(f==NULL)
T=p->rchild;
else
if(f->lchild==p)
f->lchild=p->rchild;
else
f->rchild=p->rchild;
deletep;
}
else/*p有左子树*/
{
q=p;
s=p->lchild;
while(s->rchild)/*在p的左子树中查找最右下结点*/
{
q=s;
s=s->rchild;
}
if(q==p)
q->lchild=s->lchild;/*将s的左子树链到q上*/
else
q->rchild=s->lchild;
p->key=s->key;
deletes;
}
returnT;
}
voidmain(){
BSTreeT;
intx;
BSTreeresult;
printf("建立二叉排序树,请输入序列L:
\n");
CreateBST(&T);
printf("中序遍历输出序列为:
");
InOrder(T);
cin>>x;
result=DelBST(T,x);
if(result!
=NULL){
InOrder(result);
}
else
printf("无x\n");
}
(3)编写算法,判断一棵二叉树是否为二叉排序树。
#include
#include
#definemax10
typedefstructnode{
intdata;
node*lchild,*rchild;
}Bitree;
Bitree*B[max];
inttemp=0;
intBtree[max];
Bitree*Creatree(){//建立二叉树
Bitree*T,*S;
intch;
intfront,rear,sign;
sign=0;
front=0;
rear=-1;
T=NULL;
printf("建立二叉树(1表示虚结点,0表示输入结束):
\n");
scanf("%d",&ch);
while(ch!
=0){
if(ch!
=1){//输入结点不是虚结点
S=(Bitree*)malloc(sizeof(Bitree));
S->data=ch;
S->lchild=S->rchild=NULL;
rear++;
B[rear]=S;
if(rear==front){
T=S;
sign++;
}
else{
if(sign%2==1)//寻找父结点
B[front]->lchild=S;
if(sign%2==0){
B[front]->rchild=S;
front++;
}
sign++;
}
}
else{//输入结点为虚结点
if(sign%2==0)
front++;
sign++;
}
scanf("%d",&ch);
}
returnT;
}
voidInorder(Bitree*T){//中序遍历二叉树,并将每个结点数据存入数组中
if(T!
=NULL){
Inorder(T->lchild);
printf("%d\t",T->data);
Btree[temp]=T->data;
temp++;
Inorder(T->rchild);
}
}
intJudgesort_bitree(intBtree[]){//判断是否是二叉树
inti,sign=1;
for(i=0;i if(Btree[i]>Btree[i+1]){ sign=0; break; } } returnsign; } voidJudgeout(inta){//判断输出 if(a==1) printf("给定二叉树是二叉排序树! \n"); if(a==0) printf("给定二叉树不是二叉排序树! \n"); } voidmain(){ Bitree*T; T=Creatree(); printf("中序遍历: \n"); Inorder(T); printf("\n"); Judgeout(Judgesort_bitree(Btree)); } (4)在 (1)建立的二叉排序树中,查找一个树中不存在的关键字后并插入,之后打印该树; #include usingnamespacestd; typedefintKeyType; typedefstructtree//声明树的结构 { structtree*left;//存放左子树的指针 structtree*right;//存放又子树的指针 KeyTypekey;//存放节点的内容 }BSTNode,*BSTree;//声明二叉树的链表 BSTreeinsertBST(BSTreetptr,KeyTypekey)//在二叉排序树中插入结点 {//若二叉排序树tptr中没有关键字为key的结点,则插入,否则直接返回 BSTreef,p=tptr;//p的初值指向根结点 while(p)//查找插入位置,循环结束时,p是空指针,f指向待插入结点的双亲 { if(p->key==key)//树中已有key,无须插入 returntptr; f=p;//f保存当前查找的结点,即f是p的双亲 p=(key p->left: p->right; } p=(BSTree)malloc(sizeof(BSTNode));//生成新结点 p->key=key;p->left=p->right=NULL; if(tptr==NULL)//原树为空,新插入的结点为新的根 tptr=p; else if(key f->left=p; else f->right=p; returntptr; } BSTreecreateBST()//建立二叉树 { BSTreet=NULL;//根结点 KeyTypekey; cin>>key; while(key! =-1) { t=insertBST(t,key); cin>>key; } returnt; } voidinorder_btree(BSTreeroot)//中序遍历打印二叉排序树 { BSTreep=root; if(p! =NULL){ inorder_btree(p->left); cout<<""< inorder_btree(p->right); } } intmain() { KeyTypekey; intflag,test; charcmd; BSTreeroot; do { cout<<"C.创建一棵二叉排序树\n"; cout<<"E.结束本程序\n"; flag=0; do { if(flag! =0) cout<<"选择操作错误! 请重新选择! \n"; fflush(stdin); cin>>cmd; flag++; }while(cmd! ='c'&&cmd! ='C'&&cmd! ='a'&&cmd! ='A'); if(cmd=='c'||cmd=='C') { cout<<"请输入你所要创建的二叉树的结点的值,以-1结束: \n"; root=createBST(); do { flag=0; cout<<"\n\n中序遍历二叉树: "< inorder_btree(root); cout<<"\n"< cout<<"请选择你要对这棵二叉树所做的操作: "< cout<<"I......插入你想要插入的结点"< cout<<"Q......结束对这棵二叉树的操作"< do{ if(flag! =0) cout<<"选择操作错误! 请重新选择! \n"; fflush(stdin); scanf("%c",&cmd); flag++; }while(cmd! ='i'&&cmd! ='I'&&cmd! ='q'&&cmd! ='Q'); switch(cmd) { case'i': case'I': cout<<"请输入你要插入结点的关键字: \n"; cin>>key; root=insertBST(root,key);//注意必须将值传回根 break; } }while(cmd! ='q'&&cmd! ='Q'); } }while(cmd! ='e'&&cmd! ='E'); return0; } 三)排序 (1)插入排序——已知序列{17,18,60,40,7,32,73,65,85} 建立一个顺序表,采用插入排序算法的实现升序排序,打印排序结果; #include #include typedefintKeyType; typedefintOtherType; typedefstruct { KeyTypekey; OtherTypeother_data; }RecordType; voidInsSort(RecordTyper[],intlength) /*对记录数组r做直接插入排序,length为数组中待排序记录的数目*/ { inti,j; for(i=2;i<=length;i++) { r[0]=r[i];/*将待插入记录存放到监视哨r[0]中*/ j=i-1; while(r[0].key { r[j+1]=r[j]; j=j-1; } r[j+1]=r[0];/*将待插入记录插入到已排序的序列中*/ } }/*InsSort*/ voidmain() { inti,j; RecordTyper[20]; intlen; printf("请输入待排序记录的长度: "); scanf("%d",&len); for(i=1;i<=len;i++) { printf("请输入第%d个
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 北京 交通大学 数据结构 上机 实验
