码迷,mamicode.com
首页 > 其他好文 > 详细

《软件技术基础》实验指导 实验八

时间:2017-12-25 23:17:16      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:一个   算法   max   arch   roo   attr   程序   binary   二叉排序树   

查找

实验八 查找

一、实验目的

  1. 熟悉线性表、二叉排序树和散列表的查找
  2. 能够编写一些查找的算法

二、 实验内容

  1. 18个记录的关键字如下,编写分块查找的算法进行查找。

    22、12、13、8、9、20、33、42、44、38、24、48、60、58、74、49、86、53

  2. 编写一个判别给定的二叉树是否为二叉排序树的算法,设二叉树以二叉链表存储表示,结点的数据域只存放正整数。

Tips

  1. 8.1 分块查找 http://student.zjzk.cn/course_ware/data_structure/web/chazhao/chazhao9.2.3.htm
  2. 8.2 二叉排序树 https://en.wikipedia.org/wiki/Binary_search_tree

Answer

8.1

//分块查找的程序代码
#include<stdio.h>
//类型定义
typedef int keytype;
typedef struct
{
    keytype key;
    int low,high;
}index;
typedef struct
{
    keytype key;
}record;
const int recN=18;
const int idxN=3;

int blksearch(record[],index[],keytype,int);
int main()
{
    record r[recN]={22,12,13,8,9,20,33,42,44,38,24,48,60,58,74,49,86,53};
    index idx[idxN]={{22,0,5},{48,6,11},{86,12,17}};
    keytype key;
    int loc,i;
    printf("待查找的记录关键字表:\n");
    for(i=0;i<recN;i++)
        printf("%5d",r[i]);
    printf("\n");
    printf("输入所要查找记录的关键字:");
    scanf("%d",&key);
    loc=blksearch(r,idx,key,idxN);
    if(loc!=-1) printf("查找到,是第%d个记录。\n",loc+1);
    else printf("记录查找不到!\n");
    return 0;
}
//折半查找索引表,块内顺序查找
//分块查找
int blksearch(record r[],index idx[],keytype k,int n)
{
    int i,low=0,high=n-1,mid,bh,find=0;
    //折半查找索引表
    while(low<=high&&!find)
    {
        mid=(low+high)/2;
        if(k<idx[mid].key)
        {
            high=mid-1;
        }
        else if(k>idx[mid].key)
        {
            low=mid+1;
        }
        else
        {
            high=mid-1;
            find=1;
        }
    }
    if(low<n)
    {
        i=idx[low].low;//块的起始地址
        bh=idx[low].high;//块的终止地址
    }
    //块内顺序查找
    while(i<bh&&r[i].key!=k)
    {
        i++;
    }
    if(r[i].key!=k)
    {
        i=-1;
    }
    return i;
}

8.2

//判断二叉排序树的程序代码
#include<stdio.h>
#include<stdlib.h>
//#include<malloc.h>
//二叉链表的结构类型定义
const int maxsize=1024;
typedef int keytype;
typedef struct node
{
    keytype key;
    struct node *lchild,*rchild;
}bitree;

bitree*creattree();
void preorder(bitree*);
void inorder(bitree*);

//void main()
int main()
{
    bitree*pb;
    pb=creattree();
    preorder(pb);
    printf("\n");
    inorder(pb);
    printf("是二叉排序树!\n");
    return 0;
}

//二叉树的建立
bitree*creattree()
{
    keytype x;
    bitree*Q[maxsize];
    int front,rear;
    bitree*root,*s;
    root=NULL;
    front=1;rear=0;
    printf("按层次输入二叉排序树的整型结点数据,0表示虚结点,-1表示结束:\n");
    scanf("%d",&x);//输入0表示虚结点,-1表示结束
    while(x!=-1)
    {
        s=NULL;
        if(x!=0)
        {
            s=(bitree*)malloc(sizeof(bitree));
            s->key=x;
            s->lchild=NULL;
            s->rchild=NULL;
        }
        rear++;
        Q[rear]=s;
        if(rear==1)root=s;
        else
        {
            if(s&&Q[front])
                if(rear%2==0)Q[front]->lchild=s;
                else Q[front]->rchild=s;
            if(rear%2==1)front++;
        }
        scanf("%d",&x);;
    }
    return root;
}

//二叉树的输出
void preorder(bitree*p)
{
    if(p!=NULL)
    {
        printf("%d",p->key);
        if(p->lchild!=NULL||p->rchild!=NULL)
        {
            printf("(");
            preorder(p->lchild);
            if(p->rchild!=NULL) printf(",");
            preorder(p->rchild);
            printf(")");
        }
    }
}

//判断二叉排序树
keytype k =32768;
void inorder(bitree *p)
{
    if(p!=NULL)
    {
        inorder(p->lchild);
        if(p->key>k)
        {
            k=p->key;
        }
        else
        {
            printf("不是二叉排序树\n");
            exit(0);
        }
        inorder(p->rchild);
    }
}

《软件技术基础》实验指导 实验八

标签:一个   算法   max   arch   roo   attr   程序   binary   二叉排序树   

原文地址:https://www.cnblogs.com/vanlion/p/datastructure-exp-8.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!