利用随机函数产生30000个随机整数,进行顺序查找、折半查找(静态)、以及二叉排序树查找(动态)(VisualC++)

  • 设计方案 

  1. 数据结构的选择与设计

静态查找选择顺序查找及折半查找,动态查找选择二叉排序树。

  1. 总体设计方案

     首先,构建模型,初始化数据;其次,为三种需求设计算法并实现;最后,检验结果并进行算法的简化、优化。

  1. 详细设计方案
  1. 顺序查找算法流程图
  2. 折半查找算法流程图

3.二叉排序树查找流程图

系统实现(附源代码)与测试(附截图及说明)

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

#include<time.h>

typedef int KeyType;

typedef struct{

KeyType key;

}ElemType;

typedef struct{

   ElemType *elem;

   int length;

}SSTable;

typedef struct BiTNode{

ElemType data;

    struct BiTNode *lchild,*rchild;

}BiTNode,*BiTree;

int Create(SSTable *ST,KeyType **data){

  int i,k,n,temp;

  printf("\n请输入表长:");

  scanf("%d",&n);

   *data=(KeyType*)calloc(n,sizeof(KeyType));

   ST->elem=(ElemType*)calloc((n+1),sizeof(ElemType));

  if(!ST->elem) return 0;

  srand((int)time(0));

  for(i=1;i<=n;i++)

  {(*data)[i-1]=rand()%100;

  (ST->elem+i)->key=(*data)[i-1];}

  ST->length=n;

  

  for(i=1;i<n;i++)

  {

    for(k=i+1;k<=n;k++)

if(ST->elem[k].key<ST->elem[i].key)

{  temp=ST->elem[i].key;

   ST->elem[i].key=ST->elem[k].key;

   ST->elem[k].key =temp;

}

  }

  return 1;

}/*在顺序表查找其关键字等于key的数据元素,若找到,则函数值为该元素在表中的位置,*/

/*否则为0,指针变量time记录所需关键字进行比较的次数*/

int Search(SSTable ST,KeyType key,int *time)

{ int i;

 ST.elem[0].key=key;

 *time=0;

 for(i=1;ST.elem[i].key!=key&&i<=ST.length;i++,++*time);

 return i;

}

int BinSearch(SSTable ST,KeyType key,int *time)

{ int low,high,mid;

low=1;high=ST.length; *time=1;

ST.elem[0].key=key;

while(low<=high)

{mid=(low+high)/2;

  if (key==ST.elem[mid].key)

 return(mid);

  else

    if(key<ST.elem[mid].key)

{ high=mid-1;

++*time;}

      else

  {  low=mid+1;

  ++*time; }

}

return (0);

}

/*在二叉排序树中插入一个新结点*/

void InsertBST(BiTree *bt,BiTree s)

{

if(*bt==NULL) *bt=s;

   else if(s->data.key<(*bt)->data.key)

   InsertBST(&((*bt)->lchild),s);

       else

   if(s->data.key>(*bt)->data.key)

   InsertBST(&((*bt)->rchild),s);

}

/*对已经建立好的二叉排序树进行中序遍历,将得到一个按关键字有序的元素序列*/

void Inorder(BiTree bt)

{

if(bt!=NULL)

{

Inorder(bt->lchild);

printf("%5d",(bt->data).key);

Inorder(bt->rchild);

}

}

/*在二叉排序树bt中查找其关键字等于给定值的结点是否存在,并输出相应信息*/

BiTree SearchBST(BiTree bt,KeyType key)

{

if(bt==NULL) return NULL;

else if(bt->data.key==key) return bt;

else if(key<bt->data.key) return SearchBST(bt->lchild,key);

else return SearchBST(bt->rchild,key);

}

void main()

{ SSTable ST;

BiTNode *bt=NULL,*s;

KeyType key;

KeyType *data;

int i,j,mid,time;

char ch;

printf("进行静态查找");

if(Create(&ST,&data))

{ printf("创建成功");

  /*创建成功,可重复查询*/

do{

    printf("输入你想要的关键字:");

scanf("%d",&key);

    i=Search(ST,key,&time);/*顺序查找*/

if(i!=0&&i<=ST.length)/*查找成功,输出所在位置及key与关键字比较的次数*/

{printf("顺序查找成功,位置为%d",i);

printf("\n 与关键字比较次数为%d",time);

}

    else /*查询失败,输出key与关键字比较的次数*/

    { printf("顺序查找查询失败!");

printf("\n 与关键字的比较次数为%d",time);

 }

 mid=BinSearch(ST,key,&time);/*折半查找*/

if(mid!=0)/*折半查找成功,输出所在位置及key与关键字比较的次数*/

{printf("\n折半查找成功,位置为%d",mid);

printf("\n 与关键字比较次数为%d",time);

}

    else /*查询失败,输出key与关键字比较的次数*/

    { printf("\n折半查找查询失败!");

printf("\n 与关键字的比较次数为%d",time);

 }

    printf("\n 继续吗(y/n):\n");

/*是否继续查找*/

ch=getch();

}while(ch=='y'||ch=='Y');

}

  else /*表ST建立失败,输出内存溢出信息*/

  printf("\n 溢出");

  /******************************************************************************************************/

  printf("进行动态查找");

  for(j=0;j<ST.length;j++){

  s=(BiTree)malloc(sizeof(BiTNode));

  (s->data).key=data[j];

  s->lchild=s->rchild=NULL;

  InsertBST(&bt,s);

  

  }

  printf("\n 二叉排序树创建完成!\n");

  Inorder(bt);

  do{ /*二叉排序树的查找,可多次查找,并输出查找的结果*/

  printf("\n\n 请输入你想要查找的关键字:");

  scanf("%d",&key);

  s=SearchBST(bt,key);

  if(s!=NULL)printf("  查找成功!");

  else printf("  查找不成功!");

  printf("\n\n 继续吗(y/n):\n");

  ch=getch();

  }while(ch=='y'||ch=='Y');

  

}

  • 28
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值