Hash表算法实现
实验七:折半查找、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:
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 }
100 //初始化哈希表
101 Status CHashTable::InitialHash(ElemType* ary,int length)
102 {
103 for (int i=0;i<length;i++)
104 {
105 this->InsertHash(ary[i]);
106 }
107
108 return SUCCESS;
109 }
110 //在哈希表中查找关键字
111 Status CHashTable::SearchHash(ElemType k,int& index,int& count)
112 {
113 index = Hash(k);
114 while (m_elem[index]!=-1 && m_elem[index]!=k)
115 index = collision(index,++count);
116 if (k == m_elem[index])
117 return SUCCESS;
118 else
119 return UNSUCCESS;
120 }
121 int CHashTable::SearchHash(ElemType k)
122 {
123 int i,c=0;
124 if (SearchHash(k,i,c))
125 return i;
126 else
127 return -1;
128 }
129 //在哈希表中插入关键字
130 Status CHashTable::InsertHash(ElemType e)
131 {
132 int index = 0;
133 int count = 0;
134 if (!SearchHash(e,index,count) && this->m_count<this->m_length)
135 {
136 m_elem[index] = e;
137 this->m_count++;
138 return SUCCESS;
139 }
140
141 return DUPLICATE;
142 }
143 //打印哈希表
144 Status CHashTable::PrintHash()
145 {
146 cout<<"Hash:\n";
147 for (int i=0;i<this->m_length;i++)
148 {
149 cout<<m_elem[i]<<"\t";
150 }
151 cout<<endl;
152
153 return SUCCESS;
154 }
155 //再探测法
156 int CHashTable::collision(int index,int count)
157 {
158 return (index+count)%m_length;
159 }
160
161
162
163 //测试main函数入口点
164 #include "HashTable.h"
165
166
167 int main()
168 {
169 int ary[8] = {49,38,65,97,76,13,27,49};
170
171 CHashTable H(ary,8);
172
173 H.PrintHash();
174
175 cout<<H.SearchHash(65)<<endl;
176
177 H.InsertHash(98);
178
179 H.PrintHash();
180
181 cout<<H.SearchHash(98)<<endl;
182 cout<<H.SearchHash(99)<<endl;
183 }
184
185
186
代码下载:HashTable.rar
数据结构越来越复杂了,我不能保证我的代码是正确的..
我实在是没时间进行大规模变态数据测试.我很忙..大家都很忙...
NewSketcher
08.12.28 00:34