BX101021实验9文档格式.docx

上传人:b****8 文档编号:22901225 上传时间:2023-02-05 格式:DOCX 页数:17 大小:141.34KB
下载 相关 举报
BX101021实验9文档格式.docx_第1页
第1页 / 共17页
BX101021实验9文档格式.docx_第2页
第2页 / 共17页
BX101021实验9文档格式.docx_第3页
第3页 / 共17页
BX101021实验9文档格式.docx_第4页
第4页 / 共17页
BX101021实验9文档格式.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

BX101021实验9文档格式.docx

《BX101021实验9文档格式.docx》由会员分享,可在线阅读,更多相关《BX101021实验9文档格式.docx(17页珍藏版)》请在冰豆网上搜索。

BX101021实验9文档格式.docx

*2------查找结点*

*3------插入结点*

*4------删除结点*

*5------中序输出排序树*

请选择菜单号(0--5):

3.实验程序

#include<

string.h>

stdio.h>

#defineSEARCHMAX100

#defineN10

voidSeqSearch()

{

inta[N],i,x,y;

charch;

printf("

\n\t\t建立一个整数的顺序表(以回车符为间隔,以-1结束):

\n"

);

for(i=0;

i<

SEARCHMAX;

i++)

{

\t\t"

scanf("

%d"

&

a[i]);

getchar();

if(a[i]==-1)

{y=i;

break;

}

}

\n\t\t需要查找请输入Y,否则输入N:

"

%c"

ch);

while(ch=='

y'

||ch=='

Y'

\n\t\t请输入要查找的数据:

x);

getchar();

i=y-1;

while(i>

=0&

&

a[i]!

=x)

i--;

if(i==-1)

printf("

\n\t\t抱歉!

没有你要查找的数据。

else

\n\t\t你要查找的数据在第%d个位置上。

i+1);

\n\t\t继续查找输入Y,否则输入N:

}

voidBinSearch()

intR[SEARCHMAX],i,k,low,mid,high,m,nn;

\n\t\t建立递增有序的查找顺序表(以回车符间隔,以-1结束):

R[i]);

if(R[i]==-1)

{nn=i;

\n\t\t查找请输入Y,退出输入N:

"

\n\t\t请输入要查找的数据:

k);

low=0;

high=nn-1;

m=0;

while(low<

=high)

mid=(low+high)/2;

m++;

if(R[mid]>

k)

high=mid-1;

if(R[mid]<

low=mid+1;

else

break;

if(low>

high)

没有你要查找的数据,\n"

\n\t\t共进行%d次比较。

m);

if(R[mid]<

mid++;

\n\t\t可将此数插入到第%d个位置上,\n"

mid+1);

\n\t\t要找的数据%d在第%d个位置上。

k,mid+1);

\n\t\t继续查找输入Y,否则输入N;

typedefintKeyType;

typedefstructnode

KeyTypekey;

structnode*lchild,*rchild;

}BSTNode;

typedefBSTNode*BSTree;

BSTreeCreateBST(void);

voidSearchBST(BSTreeT,KeyTypeKey);

voidInsBST(BSTree*Tptr,KeyTypeKey);

voidDelBSTNode(BSTree*Tptr,KeyTypeKey);

voidInorderBST(BSTreeT);

voidBTSearch()

BSTreeT;

charch1,ch2;

KeyTypeKey;

\n\t\t建立一棵二叉树的二叉链表存储\n"

T=CreateBST();

ch1='

;

while(ch1=='

||ch1=='

\n\t\t二叉排序树"

\n\t\t**************************"

\n\t\t1----更新二叉排序树*"

\n\t\t2----查找结点*"

\n\t\t3----插入结点*"

\n\t\t4----删除结点*"

\n\t\t5----中序输出排序树*"

\n\t\t0----返回*"

\n\t\t请选择菜单号(--5):

ch2);

switch(ch2)

case'

1'

:

T=CreateBST();

2'

printf("

scanf("

Key);

getchar();

SearchBST(T,Key);

\n\t\t查找完毕。

3'

\n\t\t请输入要插入的数据:

InsBST(&

T,Key);

\n\t\t插入完毕。

4'

\n\t\t请输入要删除的数据。

DelBSTNode(&

\n\t\t删除完毕。

5'

\n\t\t"

InorderBST(T);

\n\n\t\t二叉排序树输出完毕。

0'

ch1='

n'

return;

default:

\n\t\t输入错误!

请重新输入。

BSTreeCreateBST(void)

T=NULL;

\n\t\t请输入一个整数关键字(输入时结束输入):

while(Key)

InsBST(&

\n\t\t请输入下一个整数关键字(输入时结束输入):

returnT;

voidSearchBST(BSTreeT,KeyTypeKey)

BSTNode*p=T;

while(p)

if(p->

key==Key)

\n\t\t已经找到你输入的数据。

return;

p=(Key<

p->

key)?

lchild:

rchild;

\n\t\t没有找到你输入的数据。

voidInsBST(BSTree*T,KeyTypeKey)

BSTNode*f,*p;

p=(*T);

\n\t\t树中已有%d,不需插入。

Key);

f=p;

p=newBSTNode;

p->

key=Key;

lchild=p->

rchild=NULL;

if((*T)==NULL)

(*T)=p;

if(Key<

f->

key)

f->

lchild=p;

rchild=p;

voidDelBSTNode(BSTree*T,KeyTypeKey)

BSTNode*parent=NULL,*p,*q,*child;

p=*T;

key==Key)break;

parent=p;

if(!

p)

\n\t\t没有找到你要删除的结点。

q=p;

if(q->

lchild&

q->

rchild)

for(parent=q,p=q->

lchild;

parent=p,p=p->

lchild);

child=(p->

lchild)?

parent)*T=child;

else

if(p==parent->

lchild)

parent->

lchild=child;

rchild=child;

if(p!

=q)q->

key=p->

key;

}

delete(p);

voidInorderBST(BSTreeT)

if(T!

=NULL)

InorderBST(T->

\t%d"

T->

key);

rchild);

voidmain()

intchoice;

ch='

\n\t\t查找子系统"

\n\t\t*************************"

\n\t\t*1----顺序查找*"

\n\t\t*2----二分查找*"

\n\t\t*3----二叉排序树*"

\n\t\t*0----返回*"

\n\t\t请选择菜单号(--3):

choice);

switch(choice)

case1:

SeqSearch();

case2:

BinSearch();

case3:

BTSearch();

case0:

ch='

\n\t\t菜单选择错误!

请重新选择。

4.程序运行

1.顺序查找

2.二分查找

3.二叉排序树

3.

5.实验小结

通过本次实验主要了解查找的算法,并且熟悉各种查找的方法在什么时候比较适用,并且掌握了静态和动态两种查找的区别,进一步熟悉了几种查找方法的思想。

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

当前位置:首页 > 求职职场 > 面试

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

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