实验目的:
通过上机实验对各种静态、动态查找方法进行比较。
实验内容与要求:
⑴实现三种以上静态或动态查找方法,查找表中的数据随机生成;
⑵生成随机数,并执行查找,记录运行结果并加以分析。
#include<iostream>
#include<vector>
#include<iomanip>
#include<windows.h>
#include <algorithm>
#include<queue>
using namespace std;
const int length = 10;
const int sq = 4; //分块查找每块数据的个数
//生成任意类型,任意区间的随机数
template<typename T>
T randT(T Lower, T Upper)
{
T temp;
if (Lower > Upper)
{
temp = Upper;
Upper = Lower;
Lower = temp;
}
temp = static_cast<double>(Upper - Lower); //避免数据丢失或溢出
return rand() / (double)RAND_MAX * temp + Lower;
}
template<class T>
class Find_method {
private:
T* table = new T[length]; //查找表
T temp; //随机数
struct Bin {
T elem;
struct Bin* lchild, * rchild;
};
Bin *root; //根节点
public:
Find_method();
~Find_method();
void create_temp(); //生成随机数
void refresh_table(); //刷新查找表
void print_table(); //打印查找表
void half_table(); //折半法查找
void block_table(); //分块法查找
void hash_table(); //哈希表查找
void Insert(T e);
void CTree();
bool exists(T e) {
Bin* p = root;
while (p != NULL) {
if (p->elem == e)return true;
else if (p->elem < e)p = p->rchild;
else {
p = p->lchild;
}
}
return false;
}
};
template<class T>
Find_method<T>::Find_method()
{
srand(GetTickCount64()); //生成随机数种子
temp = randT<T>(0, 100);
for (int i = 0; i < length; i++) {
table[i] = randT<T>(0, 100);
}
root = NULL;
}
template<class T>
Find_method<T>::~Find_method()
{
delete[]table;
}
template<class T>
void Find_method<T>::create_temp()
{
temp = randT<T>(0, 100);
cout << endl << "此次生成的随机数是:" << temp << endl;
}
template<class T>
void Find_method<T>::refresh_table()
{
srand(GetTickCount64()); //生成随机数种子
for (int i = 0; i < length; i++) {
table[i] = randT<T>(0, 100);
}
}
template<class T>
void Find_method<T>::print_table()
{
cout << endl << "查找表为:" << endl;
cout.setf(ios::left);
for (int i = 0; i < length; i++) {
if (i % 10 == 0 && i != 0)cout << endl;
cout << setw(10) << table[i] << " ";
}
cout << endl;
}
template<class T>
void Find_method<T>::half_table()
{
cout << endl << "折半法查找";
T* half = new T[length]();
for (int i = 0; i < length; i++)half[i] = table[i];
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++) {
if (half[i] < half[j]) {
T temp = half[j];
half[j] = half[i];
half[i] = temp;
}
}
}
int left = 0;
int right = length - 1;
int middle;
bool h = false;
cout << endl << "寻找过程为:";
while (!h) {
middle = (left + right) / 2;
if (temp == half[middle]) {
cout << temp << endl << "找到了!" << endl;
h=true;
}
else if (temp < half[middle]) {
cout << half[middle] << " ";
right = middle - 1;
}
else {
cout << half[middle] << " ";
left = middle + 1;
}
if (left>right) {
h = true;
cout << endl << "找不到!" << endl;
}
}
delete[]half;
}
template<class T>
void Find_method<T>::block_table()
{
T* s = new int[length]();
for (int i = 0; i < length; i++)s[i] = table[i];
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++) {
if (s[i] < s[j]) {
T temp = s[j];
s[j] = s[i];
s[i] = temp;
}
}
}
cout << endl << "分块法查找";
struct block {
T elem = 0;
T* b = new T[sq]();
};
vector<block>BL;
for (int i = 0; i < length / sq; i++) {
block N;
N.elem = s[sq * i];
for (int j = 0; j < sq; j++) {
if (s[sq * i + j] > N.elem) {
N.elem = s[sq * i + j];
}
N.b[j] = s[sq * i + j];
}
BL.push_back(N);
}
//排序
block tp;
for (int i = 0; i < length / sq; i++) {
for (int j = 0; j < length / sq; j++) {
if (BL[i].elem < BL[j].elem) {
tp = BL[j];
BL[j] = BL[i];
BL[i] = tp;
}
}
}
bool judge = false;
cout << endl << "寻找过程为:";
for (int i = 0; i < length / sq; i++) {
if (temp == BL[i].elem) {
cout << temp << endl << "找到了!" << endl;
judge = true;
break;
}
else if (temp > BL[i].elem) {
cout << BL[i].elem << " ";
}
else {
cout << BL[i].elem << " ";
for (int j = 0; j < sq; j++) {
if (temp == BL[i].b[j]) {
cout << temp << endl << "找到了!" << endl;
judge = true;
break;
}
else {
cout << BL[i].b[j] << " ";
}
}
break;
}
}
if (!judge)cout << endl << "找不到!" << endl;
delete[]s;
}
template<class T>
void Find_method<T>::hash_table()
{
/*
H(key)=key%table_long
Hi=(H(key)+i)%table_long
*/
cout << endl << "哈希表查找";
T *hash = new T[length]();
bool *h = new bool[length + 1]();
int i = 1;
for (int j = 0; j < length; j++) {
if (h[static_cast<int>(table[j]) % length] == 0) {
hash[static_cast<int>(table[j]) % length] = table[j];
h[static_cast<int>(table[j]) % length] = 1;
}
else if (h[(static_cast<int>(table[j]) % length + i) % length] == 0 && h[(static_cast<int>(table[j]) % length + i - 1) % length] == 1) {
hash[(static_cast<int>(table[j]) % length + i) % length] = table[j];
h[(static_cast<int>(table[j]) % length + i) % length] = 1;
i = 1;
}
else {
i++;
j--;
}
}
cout << endl << "哈希表为:";
for (int i = 0; i < length; i++) {
cout << hash[i] << " ";
}
cout << endl << "寻找过程为:";
while (!h[length]) {
if (temp == hash[static_cast<int>(temp) % length] && i == 1) {
h[length] = 1;
cout << temp;
cout << endl <<"找到了!" << endl;
}
else if (temp == hash[(static_cast<int>(temp) % length + i) % length]) {
h[length] = 1;
cout << hash[static_cast<int>(temp) % length] <<" "<< temp;
cout << endl <<"找到了!" << endl;
i = 1;
}
else {
cout << hash[(static_cast<int>(temp) % length + i) % length] << " ";
i++;
}
if (i > length) {
cout << endl <<"找不到!" << endl;
break;
}
}
}
template<class T>
void Find_method<T>::Insert(T e)
{
if (!exists(e)) {
Bin* q = new Bin;
q->elem = e;
q->lchild = NULL;
q->rchild = NULL;
Bin* p = root;
while (p != NULL) {
if (e > p->elem && p->rchild != NULL)p = p->rchild;
else if (e > p->elem && p->rchild == NULL) {
p->rchild = q;
break;
}
else if (e < p->elem && p->lchild != NULL)p = p->lchild;
else if (e < p->elem && p->lchild == NULL) {
p->lchild = q;
break;
}
}
if (root == NULL) {
root = new Bin;
root->elem = e;
root->rchild = NULL;
root->lchild = NULL;
}
}
}
template<class T>
void Find_method<T>::CTree()
{
for (int i = 0; i < length; i++) {
Insert(table[i]);
}
cout << endl << "二叉树查找" << endl;
cout << "二叉树层次遍历为:";
//层次遍历
Bin* q = root;
queue<Bin*>que;
que.push(q);
while (!que.empty()) {
auto x = que.front();
cout << x->elem << " ";
que.pop();
if (x->lchild != NULL) {
que.push(x->lchild);
}
if (x->rchild != NULL) {
que.push(x->rchild);
}
}
cout << endl;
cout << "寻找过程为:";
Bin* p = root;
while (p != NULL) {
if (p->elem == temp) {
cout << temp << endl << "找到了!" << endl;
break;
}
else if (temp > p->elem) {
cout << p->elem << " ";
p = p->rchild;
}
else if (temp < p->elem) {
cout << p->elem << " ";
p = p->lchild;
}
}
if (p == NULL)cout << endl << "找不到!" << endl;
}
void show() {
cout << "---查找方法比较---" << endl;
cout << "1.刷新查找表" << endl;
cout << "2.查找" << endl;
cout << "0.退出" << endl;
}
int main() {
int n;
Find_method<int>f;
show();
f.print_table();
while (1) {
cin >> n;
if (n == 0)break;
else if (n == 1) {
f.refresh_table();
f.print_table();
}
else if (n == 2) {
f.create_temp(); //生成随机数
f.half_table(); //折半法查找
f.block_table(); //分块法查找
f.CTree(); //二叉树查找
f.hash_table(); //哈希表查找
}
else {
cout << "error" << endl;
}
}
}