数据结构实验——查找的有关操作

来源:互联网 发布:淘宝旺旺怎么改名字 编辑:程序博客网 时间:2024/06/09 11:37
#include <iostream>#include <algorithm>#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <string.h>#include <map>#include <set>#include <queue>#include <deque>#include <list>#include <bitset>#include <stack>#include <stdlib.h>using namespace std;/*实验五  查找的有关操作实验学时 2学时背景知识:顺序查找、树表查找、散列查找。目的要求:1.掌握折半查找算法的思想及程序实现。2.掌握二叉排序树、AVL树的查找、插入、删除、建立算法的思想及程序实现。3.掌握散列存储结构的思想,能选择合适散列函数,实现不同冲突处理方法的散列表的查找、建立。实验内容:1.利用实验一建立有序表,采用折半查找实现某一已知的关键字的查找。2.随机产生一组关键字,利用二叉排序树的插入算法建立二叉排序树,然后删除某一指定关键字元素。*3.建立AVL树并实现删除某一指定关键字元素。4.已知散列函数为H(key)=key%p(p为自定的常数),冲突处理方法分别为线性探测法、外拉链法实现散列表的建立(利用插入算法实现)。实验说明1.存储定义(散列表的外拉链法)#define n 9typedef struct node{int key;    struct node *next;}NODE;NODE *HashTable[9];算法1、2、3可以参考顺序表,二叉链表的存储实现。2.各种关键字数据输入可利用随机函数自动产生,以便节省上机时间。3.算法1存储在文件seqlist.h中,算法2、3存储在文件bintree.h中,算法4存储在文件hash.h中注意问题1.注意理解折半查找的适用条件(链表能否实现折半查找?)。2.注意建立二叉排序树、散列表时相同元素的处理。3.注意理解静态查找、动态查找概念。4.比较各种查找算法的各自特点,能够根据实际情况选择合适的查找方法。*/const int MAXN = 100;int a[MAXN];int binary_search(int n,int x){    int mid;    int l = 0;    int r = n-1;    while(l<=r)    {        mid = (l+r)>>1;        if(a[mid] == x)        {            cout<<"The number is on "<<mid+1<<"th place"<<endl;            return 1;        }        if(a[mid] > x)            r = mid - 1;        else            l = mid + 1;    }    return 0;}typedef struct node{    int key;    node *l,*r;}node;node *Insert_BST(node *T,int key){    node *root = T , *p = T;    while(p)    {        if(p->key == key)            return T;        root = p;        p = p->key > key ? p->l : p->r ;    }    p = new node();    p->key = key;    p->l = p->r = NULL;    if(T == NULL)        T = p;    else        root->key > key ? root->l = p : root->r = p ;    return T;}node *Create_BST(int n){    node *bt = NULL;    for(int i=0;i<n;i++)        bt = Insert_BST(bt,a[i]);;    return bt;}void Inordered_Travel(node *T){    if(T)    {        Inordered_Travel(T->l);        cout<<T->key<<" ";        Inordered_Travel(T->r);    }}int data[MAXN];void hash_lined(int p,int n){    memset(data,0,sizeof(data));    for(int i=0;i<n;i++)    {        int t = a[i]%p;        if(!data[t])            data[t] = a[i];        else        {            for(int j=t+1;j<n;j++)            {                if(j == MAXN)                    j = 0;                if(!data[j])                {                    data[j] = a[i];                    break;                }            }        }    }}vector<int>v[MAXN];void Hash_Wai_La_Lian(int n,int p){    for(int i=0;i<n;i++)        v[a[i]%p].push_back(a[i]);}int main(){    //binary_search    int n;    cout<<"Please input a Integer n: ";    cin>>n;    cout<<"Please input n ordered numbers: ";    for(int i=0;i<n;i++)        cin>>a[i];    //sort(a,a+n);    int x;    cout<<"Please input a number which you want to search: ";    while(cin>>x && x!=-1)    {        int flag = binary_search(n,x);//binary_search        if(!flag)            cout<<"Can not find this number"<<endl;    }        //Create_BST;    cout<<"Please input n:";    cin>>n;    cout<<"Please input n disordered numbers: ";    for(int i=0;i<n;i++)        cin>>a[i];    node *bt;    bt = Create_BST(n);    //cout<<endl;    cout<<"Inordered Travel:";    Inordered_Travel(bt);    cout<<endl;        cout<<"Now create a hash_table H(key)=key%p,please input p: ";    int p;    cin>>p;    cout<<endl<<"下面进行线性探测法:";    hash_lined(p,n);    cout<<"线性表存储的位置为: ";    for(int i=0;i<n;i++)        cout<<data[i]<<' ';    cout<<endl;        cout<<"下面进行外拉链法:";    Hash_Wai_La_Lian(n,p);    cout<<n<<"个数外拉链法对应的余数为:"<<endl;    for(int i=0;i<p;i++)    {        int ans = v[i].size();        cout<<' '<<i;        for(int j=0;j<ans;j++)            cout<<"--->"<<v[i][j];        cout<<endl;    }        return 0;}