Data structure (9)-search

Second, search in order

#include<cstdio>
#include<algorithm>
#include<cstdlib>
using namespace std;
const int maxSize = 30;
typedef struct {
	int key;
}DataType;
typedef struct {
	DataType* data;
	int maxSize, n;
}SeqList;
int SeqSearch(SeqList& L, DataType x) {
	L.data[L.n] = x;
	int i;
	for (i = 0; L.data[i].key != x.key; i++);
	return (i >= L.n) ? -1 : i;
}
int OrderSeqSearch(SeqList& L, DataType x, int& is) {
	L.data[L.n] = x;
	int i;
	for (i = 0; L.data[i].key < x.key; i++);
	is = i;
	return (i < L.n && L.data[i].key == x.key) ? i : -1;
}

Three, half search

int BinSearch(SeqList& L, DataType x, int& is) {
	int left = 0, right = L.n - 1, mid;
	while (left <= right) {
		mid = (left + right) / 2;
		is = mid;
		if (x.key > L.data[mid].key) left = mid + 1;
		else right = mid - 1;
	}
	is = left; 
	return -1;
}

Six, hashing method

Open address method for conflict resolution

1. Linear detection

//HashTable.h

#pragma once
#include<cstdio>
#include<cstdlib>
#define defaultSize 11
enum KindOfState{Active, Blank, Deleted};
typedef int KeyType;
typedef struct {
	KeyType key;
}HElemType;
typedef struct {
	int divisor;
	int n, m;
	HElemType* data;
	KindOfState* state;
	int* count;
}HashTable;
void initHashTable(HashTable& HT, int d) {
	HT.divisor = d;
	HT.m = defaultSize;
	HT.n = 0;
	HT.data = (HElemType*)malloc(HT.m * sizeof(HElemType));
	HT.state = (KindOfState*)malloc(HT.m * sizeof(KindOfState));
	HT.count = (int*)malloc(HT.m * sizeof(int));
	for (int i = 0; i < HT.m; i++) {
		HT.state[i] = Blank; 
		HT.count[i] = 0;
	}
}
//linearProbe.cpp

#include "HashTable.h"
int FindPos(HashTable& HT, KeyType x, int& i, int& num) {
	i = x % HT.divisor;
	num = 0;
	if (HT.state[i] == Active && HT.data[i].key == x) {
		num++;
		return true;
	}
	else {
		int j = i;
		do {
			num++;
			if (HT.state[i] == Active && HT.data[i].key == x) return true;
			else if (HT.state[i] != Active) return false;
			i = (i + 1) % HT.m;
		} while (j != i);
		i = -1;
		return false;
	}
}
bool Insert(HashTable& HT, HElemType elem) {
	int i, num;
	bool flag;
	flag = FindPos(HT, elem.key, i, num);
	if (flag) {
		printf("表中已有此元素,不插入!\n");
		return false;
	}
	else if (i = -1) {
		printf("表已满,不能插入!\n");
		return false;
	}
	HT.data[i] = elem;
	HT.state[i] = Active;
	HT.count[i] = num;
	HT.n++;
	return true;
}
void createHashTable(HashTable& HT, KeyType A[], int n) {
	HElemType elem;
	int i;
	for (i = 0; i < n; i++) {
		elem.key = A[i];
		Insert(HT, elem);
	}
}
bool Remove(HashTable& HT, KeyType k, HElemType& elem) {
	int i, num;
	bool flag;
	flag = FindPos(HT, k, i, num);
	if (!flag) {
		printf("表中没有找到被删元素!\n");
		return false;
	}
	else {
		HT.state[i] = Deleted;
		return true;
	}
}
void printHashTable(HashTable& HT) {
	for (int i = 0; i < HT.m; i++) {
		if (HT.state[i] == Active)
			printf("[%d] %d(%d)\n", i, HT.data[i].key, HT.count[i]);
	}
}

2. Square detection

//Quadratic.cpp

#include "HashTable.h"
int FindPos(HashTable& HT, KeyType x, int& i, int& num) {
	i = x % HT.divisor;
	num = 0;
	if (HT.state[i] == Active && HT.data[i].key == x) {
		num++;
		return true;
	}
	else {
		int j = i, k;
		for (int k = 1; k <= HT.m / 2; k++) {
			num++;
			if (HT.state[i] == Active && HT.data[i].key == x) return true;
			else if (HT.state[i] != Active) return false;
			i = (j + k * k) % HT.m;
			num++;
			if (HT.state[i] == Active && HT.data[i].key == x) return true;
			else if (HT.state[i] != Active) return false;
			i = (j - k * k) % HT.m;
			while (i < 0) i += HT.m;
		}
		i = -1;
		return false;
	}
}

3. Double hash method 

//doubleHashProbe.cpp

#include "HashTable.h"
int FindPos(HashTable& HT, KeyType x, int& i, int& num) {
	i = x % HT.divisor;
	num = 0;
	if (HT.state[i] == Active && HT.data[i].key == x) {
		num++;
		return true;
	}
	else {
		int j = i, gap = 1 + (x % (HT.m - 1));
		do {
			num++;
			if (HT.state[i] != Active) return false;
			i = (i + gap) % HT.m;
		} while (j != i);
		i = -1;
		return false;
	}
}

Chain address method for conflict resolution

//"ChainHashTable.h"

#pragma once
#include<cstdio>
#include<cstdlib>
#define defaultSize 12
typedef int KeyType;
typedef struct {
	KeyType key;
}HElemType;
typedef struct node {
	HElemType data;
	struct node* link;
}ChainNode;
typedef struct {
	int divisor;
	int m;
	ChainNode* elem[defaultSize];
}HashTable;
void InitHashTable(HashTable& LT, int d) {
	LT.divisor = d;
	LT.m = defaultSize;
	for (int i = 0; i < LT.m; i++) LT.elem[i] = NULL;
}
#include "ChainHashTable.h"

const int maxSize = 40;
ChainNode* FindPos(HashTable& LT, KeyType k, int& i) {
	i = k % LT.divisor;
	ChainNode* p = LT.elem[i];
	while (p != NULL && p->data.key != k) p = p->link;
	return p;
}
bool Insert(HashTable& LT, HElemType x) {
	int i;
	ChainNode* p, * q, * s;
	i = x.key % LT.divisor;
	p = LT.elem[i];
	q = NULL;
	while (p != NULL && p->data.key != x.key) {
		q = p;
		p = p->link;
	}
	if (p != NULL) return false;
	s = (ChainNode*)malloc(sizeof(ChainNode));
	s->data = x;
	s->link = NULL;
	if (q != NULL) q->link = s;
	else LT.elem[i] = s;
	return true;
}
void createHashTable(HashTable& LT, KeyType A[], int n) {
	for (int i = 0; i < n; i++) {
		HElemType x;
		x.key = A[i];
		Insert(LT, x);
	}
}
void DisplayHashTable(HashTable& LT, KeyType A[], int n) {
	for (int i = 0; i < n; i++) {
		HElemType x;
		x.key = A[i];
		Insert(LT, x);
	}
}
bool Remove(HashTable& LT, KeyType k) {
	int i = k % LT.divisor;
	ChainNode* p = LT.elem[i], * q = NULL;
	while (p != NULL && p->data.key != k) {
		q = p;
		p = p->link;
	}
	if (p == NULL) return false;
	if (q != NULL) q->link = p->link;
	else LT.elem[i] = p->link;
	free(p);
	return true;
}

 

Guess you like

Origin blog.csdn.net/qq_45812711/article/details/106188272