您的位置:首页 > 其它

Hash表算法实现

2008-12-28 00:43 197 查看
实验七:折半查找、Hash查找算法的程序实现

一、实验目的

1 .熟练掌握折半查找算法并能在有序表中进行查找操作。

2. 掌握Hash表的相关算法。

、实验要求

1.认真阅读和掌握和本实验相关的教材内容。

2.复习顺序表及二叉树的基本操作过程。

3.编写完整程序完成下面的实验内容并上机运行。

4.整理并上交实验报告。

三、实验内容

1.折半查找又称为二分查找,它要求要查找的顺序表必须是有序表,即表中结点按关键字有序.并且要用顺序存储结构。

基本思想是:首先将给定值key与表中中间位置记录的关键字相比较,若二者相等,则查找成功,否则根据比较的结果确定下次查找的范围是在中间记录的前半部分还是后半部分,然后在新的查找范围内进行同样的查找,如此重复下去,直到在表中找到关键字与给定值相等的记录,或者确定表中没有这样的记录。

编写程序构造一个有序表La,从键盘接收一个关键字key,用二分查找法在La 中查找key,若找到则提示查找成功并输出key所在的位置,否则提示没有找到信息。

2.编写程序实现Hash表的建立、删除、插入以及查找操作。

   程序应包含的主要功能函数有:

     Hash( ):计算哈希地址

     InitialHash( ):初始化哈希表

SearchHash( ):在哈希表中查找关键字

InsertHash( ):向哈希表中插入关键字

DeleteHash( ):删除哈希表中某一关键字

PrintHash ( ):打印输出哈希表   

四、思考与提高

        如何利用折半查找算法在一个有序表中插入一个元素x,并保持表的有序性?

-----------------------------------------------------------------------------------------------------------

折半查找:

 

太简单了,我已经写过折半插入排序,这个直接无视了..。

 

HashTable操作(封装了一个CHashTable类):

Code: 

  1 // Status.h   状态定义头文件

  2 #pragma once

  3 

  4 #define SUCCESS 1

  5 #define UNSUCCESS 0

  6 #define DUPLICATE -1

  7 

  8 typedef int Status;

  9 typedef int ElemType;

 10 

 11 

 12 //HashTable.h     CHashTable类声明

 13 #include "Status.h"

 14 #include <iostream>

 15 using namespace std;

 16 

 17 class CHashTable

 18 {

 19 public:

 20     CHashTable(ElemType* ary,int length);

 21     CHashTable();

 22     //计算哈希地址

 23     int Hash(ElemType k);

 24     //初始化哈希表

 25     Status InitialHash(ElemType* ary,int length);

 26     //在哈希表中查找关键字

 27     Status SearchHash(ElemType k,int& i,int& c);

 28     int SearchHash(ElemType k);

 29     //在哈希表中插入关键字

 30     Status InsertHash(ElemType e);

 31     //打印哈希表

 32     Status PrintHash();

 33 private:

 34     //再探测法

 35     int collision(int index,int count);

 36 private:

 37     ElemType* m_elem;

 38     int m_key;

 39     int m_length,m_count;

 40 };

 41 

 42 

 43 //CHashTable类定义

 44 #include "HashTable.h"

 45 

 46 int HashKeyArray[10] = {11 , 23 ,  31 ,  53 , 67 ,  71,89  , 101 , 151 ,199    };

 47 

 48 CHashTable::CHashTable(ElemType* ary,int length)

 49 {

 50     if (length)

 51     {

 52         int i = 0;

 53         for (i=9;i>=0&&length<HashKeyArray[i];--i);

 54         int len = i==9?length:HashKeyArray[i+1];

 55         this->m_elem = new ElemType[len];

 56         for (int i=0;i<len;i++)

 57         {

 58             m_elem[i]  = -1;

 59         }

 60         if (i==-1)

 61         {

 62             this->m_key = 3;//suiyi

 63         }

 64         else

 65             this->m_key = HashKeyArray[i];

 66         this->m_length = len;

 67         this->m_count = 0;

 68     }

 69 

 70     this->InitialHash(ary,length);

 71 }

 72 CHashTable::CHashTable()

 73 {

 74     cout<<"构造哈希表(-1结束"<<endl;

 75 

 76     this->m_key = 13;

 77     this->m_elem = new ElemType[31];

 78     this->m_length = 31;

 79     for (int i=0;i<31;i++)

 80     {

 81         m_elem[i] = -1;

 82     }

 83     this->m_count = 0;

 84 

 85     ElemType num = 0;

 86     int count = 0;

 87     cin>>num;

 88     while (num!=-1 && count<31)

 89     {

 90         this->InsertHash(num);

 91         cin>>num;

 92         count++;

 93     }

 94 }

 95 //计算哈希地址

 96 int CHashTable::Hash(ElemType k)

 97 {

 98     return k%this->m_key;

 99 }

 //初始化哈希表

 Status CHashTable::InitialHash(ElemType* ary,int length)

 {

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

     {

         this->InsertHash(ary[i]);

     }

 

     return SUCCESS;

 }

 //在哈希表中查找关键字

 Status CHashTable::SearchHash(ElemType k,int& index,int& count)

 {

     index = Hash(k);

     while (m_elem[index]!=-1 && m_elem[index]!=k)

         index = collision(index,++count);

     if (k == m_elem[index])

         return SUCCESS;

     else

         return UNSUCCESS;

 }

 int CHashTable::SearchHash(ElemType k)

 {

     int i,c=0;

     if (SearchHash(k,i,c))

         return i;

     else

         return -1;

 }

 //在哈希表中插入关键字

 Status CHashTable::InsertHash(ElemType e)

 {

     int index = 0;

     int count = 0;

     if (!SearchHash(e,index,count) && this->m_count<this->m_length)

     {

         m_elem[index] = e;

         this->m_count++;

         return SUCCESS;

     }

 

     return DUPLICATE;

 }

 //打印哈希表

 Status CHashTable::PrintHash()

 {

     cout<<"Hash:\n";

     for (int i=0;i<this->m_length;i++)

     {

         cout<<m_elem[i]<<"\t";

     }

     cout<<endl;

     

     return SUCCESS;

 }

 //再探测法

 int CHashTable::collision(int index,int count)

 {

     return (index+count)%m_length;

 }

 

 

 

 //测试main函数入口点

 #include "HashTable.h"

 

 

 int main()

 {

     int ary[8] = {49,38,65,97,76,13,27,49};

 

     CHashTable H(ary,8);

 

     H.PrintHash();

 

     cout<<H.SearchHash(65)<<endl;

 

     H.InsertHash(98);

 

     H.PrintHash();

 

     cout<<H.SearchHash(98)<<endl;

     cout<<H.SearchHash(99)<<endl;

 }

 

 

 

代码下载:HashTable.rar

 

数据结构越来越复杂了,我不能保证我的代码是正确的..

我实在是没时间进行大规模变态数据测试.我很忙..大家都很忙...

 

 

 

        NewSketcher

                                                08.12.28  00:34 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: