实验五 查找的有关操作(数据结构)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/CJL2313/article/details/76094054

实验内容:

1.输入一批有序的整形数值,利用折半插找的算法实现查找某数的过程。

2.利用一批数据(不超过13个数),构建一棵二叉排序树,实现二叉排序树的插入、删除、查找运算,并实现中序遍历该树。

3.将上面的数据利用长度为15的哈希表存储,输出存储后的哈希表。哈希函数采用key%13,用线性探测再散列解决冲突,设计并实现查找运算。

*4.根据全班学生的姓名,用拉链法解决碰撞的方法构造一个散列表,选择适当的散列函数,设计并实现插入、删除和查找算法。

5.设计主函数,上机实现

代码:

#include <iostream>

#include <string.h>

#include <algorithm>

#include <cstdio>

#include <cstdlib>

#define  KeyType int

#define  TABLESIZE 15

using namespace std;

int a[200],y=0;

KeyType dp[TABLESIZE];

typedef struct node{

    KeyType key;

    struct node *lchild,*rchild;

}BSTNode,*BSTree;

typedef struct elem{

    int empt;

    KeyType data;

}ElemType;

ElemType ht[TABLESIZE];

int Search(KeyType k){

    int i=k%13,j=i;

    while(ht[j].data!=-1&&ht[j].data!=k){

        j=(j+1)%TABLESIZE;

        if(j==i)

            return -TABLESIZE;

    }

    if(ht[j].data==k)

        return j;

    else

        return -j;

}

void InsertBST(BSTree *bt,KeyType key){

    BSTree s;

    if(*bt==NULL){

        s=(BSTree)malloc(sizeof(BSTNode));

        s->key=key;

        s->lchild=NULL;

        s->rchild=NULL;

        *bt=s;

    }

    else if(key<(*bt)->key)

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

    else

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

}

void CreateBST(BSTree *bt,int n){

    KeyType key;

    *bt=NULL;

    int k=0;

    while(n>0){

            cin>>key;

        InsertBST(bt,key);

        n--;

    }

}

BSTree SearchBST(BSTree bt,KeyType key){

    if(!bt) return NULL;

    if(bt->key==key)

        return bt;

    else if(bt->key>key)

        return SearchBST(bt->lchild,key);

    else

        return SearchBST(bt->rchild,key);

}

void Inorder(BSTree bt){//中序

    if(bt==NULL) return;

    Inorder(bt->lchild);

    cout<<bt->key<<" ";dp[y++]=bt->key;

    Inorder(bt->rchild);

}

BSTree DelBST(BSTree bt,KeyType k){

    BSTree p,f,s,q;

    p=bt;f=NULL;

    while(p){

        if(p->key==k) break;

        f=p;

        if(p->key>k) p=p->lchild;

        else p=p->rchild;

    }

    if(p==NULL) return bt;

    if(p->lchild&&p->rchild){

        q=p;

        s=p->lchild;

        while(s->rchild){

            q=s;

            s=s->rchild;

        }

        p->key=s->key;

        if(q!=p) q->rchild=s->lchild;

        else q->rchild=s->lchild;

        free(s);

    }

    else{

        if(!p->rchild){

            q=p;

            p=p->lchild;

        }

        else{

            q=p;

            p=p->rchild;

        }

        if(!f) bt=p;

        else if(q==f->lchild) f->lchild=p;

        else f->rchild=p;

        free(q);

    }

    return bt;

}

int BinarySearch2(int length, int value)

{

    if(NULL == a || 0 == length)

        return -1;

    int start = 0;

    int endd = length -1;

    int middle =0;

    while(start <= endd){

        middle = start + ((endd - start) >> 1);

        if(value == a[middle])

            return middle;

        else if(value > a[middle]){

            start = middle + 1;

        }

        else{

            endd = middle -1;

        }

    }

    return -1;

}

void Insert(KeyType k){

    int i,adr;

    adr = k%13;

    if (ht[adr].data==-1 || ht[adr].data==-2)  //x[j]可以直接放在哈希表中

    {

        ht[adr].data=k;

        ht[adr].empt=1;

    }

    else      //发生冲突时,采用线性探查法解决冲突

    {

        i=1;  //i记录x[j]发生冲突的次数

        do

        {

            adr = (adr+1)%13;

            i++;

        } while (ht[adr].data!=-1 && ht[adr].data!=-2);

        ht[adr].data=k;

        ht[adr].empt=i;

    }

}

void cread(){

    for(int i=0;i<TABLESIZE;i++){

        //printf("%4d",i+1);

        ht[i].empt=0;

        ht[i].data=-1;

    }

    for(int i=0;i<y;i++)

        Insert(dp[i]);

    //cout<<endl;

}

void show(){

    int i;

    printf(" 哈希表地址:\t");

    for(i=0;i<15;i++)

        printf("%4d",i);

    printf("\n");

    printf(" 哈希表关键字:\t");

    for(i=0;i<15;i++)

        if(ht[i].data==-1 || ht[i].data==-2)

            printf("    "); //输出3个空格

        else

            printf("%4d",ht[i].data);

    printf(" \n");

}

int main()

{

    int n,x,log;

    cout<<"请输入序列总个数n:";

    cin>>n;

    cout<<"请输入n个有序的整形数值:"<<endl;

    for(int i=0;i<n;i++)

        cin>>a[i];

    //sort(a,a+n);

    cout<<"请输入要查找的值:";

    cin>>x;

    log=BinarySearch2(n,x)+1;

    if(log==0)

    cout<<"查找失败"<<endl;

    else

        cout<<log<<endl;

    int ch;

    BSTree bst,p;

    cout<<"请输入二叉排序树数值(<13)总个数n:";

    cin>>ch;

    cout<<"请输入n个二叉排序树的数值:"<<endl;

    CreateBST(&bst,ch);

    cout<<"中序遍历二叉排序树:"<<endl;

    Inorder(bst);

    cout<<endl<<"请输入要查找的二叉排序树数值:";

    cin>>ch;

    p=SearchBST(bst,ch);

    if(p!=NULL)

        cout<<"查找成功";

    else cout<<"查找失败";

    cout<<endl<<"请输入要插入的二叉排序树数值:";

    cin>>ch;

    InsertBST(&bst,ch);

    Inorder(bst);

    cout<<endl<<"请输入要删除的二叉排序树数值:";

    cin>>ch;

    DelBST(bst,ch);y=0;

    Inorder(bst);

    cout<<endl;

    cout<<"               ********************** 哈希表 **********************"<<endl;

    cread();

    show();

    cout<<"请输入要查找的数值:";

    cin>>ch;

    cout<<Search(ch)<<endl;

    return 0;

}

测试截图:

猜你喜欢

转载自blog.csdn.net/CJL2313/article/details/76094054
今日推荐