哈希表(hashtable)

HashTable:  哈希表/散列表,是根据关键字(key)直接访问数据的数据结构。

它通过一个关键值的函数将所需的数据映射到表中的位置来访问数据,这个映射函数叫做散列函数,存放记录的数组叫做散列表。

构造哈希表的几种方法
1.直接定址法--取关键字的某个线性函数为散列地址,Hash(Key)= Key 或 Hash(Key)= A*Key + B,A、B为常数。
2.除留余数法--取关键值被某个不大于散列表长m的数p除后的所得的余数为散列地址。Hash(Key)= Key % P。
3.平方取中法
4.折叠法
5.随机数法
6.数学分析法

哈希冲突/哈希碰撞
不同的Key值经过哈希函数Hash(Key)处理以后可能产生相同的值哈希地址,我们称这种情况为哈希冲突。任意的散列函数都不能避免产生冲突。

处理哈希冲突的闭散列方法有:1.线性探测 2.二次探测

线性探测实现代码如下:
#include <iostream>
using namespace std;

enum Status
{
	EXIT,
	DELETE,
	EMPTY,
};

template<class K>
class HashTable
{
public:
	HashTable()
		:_table(NULL)
		, _size(0)
		, _cpacity(0)
		, _status(0)
	{}

	HashTable(size_t capacity)
		:_table(new K[capacity])
		, _size(0)
		, _capacity(capacity)
		, _status(new Status[capacity])
	{
		for (size_t i = 0; i < _capacity; i++)
		{
			_status[i] = EMPTY;
		}
	}

	~HashTable()
	{
		if (_table)
		{
			delete[] _table;
		}
		if (_status)
		{
			delete[] _status;
		}
	}

public:
	bool Insert(const K& x)
	{
		if ((_size * 10 / _capacity) > 8)
		{
			HashTable ht(_capacity * 2);
			int i = 0;
			Swap(ht);
			while (i < ht._capacity)
			{
				if (ht._status[i] == EXIT)
				{
					Insert(ht._table[i]);
				}
				i++;
			}
		}
		int index = HashFunc(x);
		while (_status[index] == EXIT)
		{	
			index++;
			if (index == _capacity)
			{
				index = 0;
			}
		}
		_table[index] = x;
		_status[index] = EXIT;
		_size++;
		return true;
	}

	bool Remove(const K& x)
	{
		int index = HashFunc(x);
		while (_status[index] != EMPTY)
		{
			if (_table[index] == x)
			{
				_status[index] = DELETE;
				return true;
			}
			index++;
			if (index == _capacity)
			{
				index = 0;
			}
		}
		return false;
	}

	bool Find(const K& x)
	{
		int index = HashFunc(x);
		while (_status[index] != EMPTY)
		{
			if (_table[index] == x)
			{
				return true;
			}
			index++;
			if (index == _capacity)
			{
				index = 0;
			}
		}
		return false;
	}

	void Print()
	{
		for (size_t i = 0; i < _capacity; ++i)
		{
			cout << _status[i] << ":";
			cout << _table[i] << " ";
		}
		cout << endl;
	}

protected:

	void Swap(HashTable<int>& ht)
	{
		swap(_table, ht._table);
		swap(_status, ht._status);
		swap(_size, ht._size);
		swap(_capacity, ht._capacity);
	}

	size_t HashFunc(const K& x)
	{
		return (x % _capacity);
	}

private:
	K* _table;
	size_t _size;
	size_t _capacity;
	Status* _status;
};

int main()
{
	HashTable<int> ht1(10);
	ht1.Insert(89);
	ht1.Insert(18);
	ht1.Insert(49);
	ht1.Insert(58);
	ht1.Insert(9);
	ht1.Insert(55);
	ht1.Insert(34);
	ht1.Insert(44);
	ht1.Insert(99);
	ht1.Insert(22);
	ht1.Print();
	ht1.Remove(49);
	ht1.Print();
	ht1.Find(58);
	return 0;
}





以下是使用Python语言实现的哈希表HashTable: ```python class HashTable: def __init__(self, size): self.size = size self.slots = [None] * self.size self.data = [None] * self.size def hashfunction(self, key): return key % self.size def rehash(self, oldhash): return (oldhash + 1) % self.size def put(self, key, data): hashvalue = self.hashfunction(key) if self.slots[hashvalue] == None: self.slots[hashvalue] = key self.data[hashvalue] = data else: if self.slots[hashvalue] == key: self.data[hashvalue] = data else: nextslot = self.rehash(hashvalue) while self.slots[nextslot] != None and self.slots[nextslot] != key: nextslot = self.rehash(nextslot) if self.slots[nextslot] == None: self.slots[nextslot] = key self.data[nextslot] = data else: self.data[nextslot] = data def get(self, key): startslot = self.hashfunction(key) data = None stop = False found = False position = startslot while self.slots[position] != None and not found and not stop: if self.slots[position] == key: found = True data = self.data[position] else: position = self.rehash(position) if position == startslot: stop = True return data ``` 接下来,我们可以按照题目要求,将关键字为(12,9,15,10.8.21, 3,29,30,11,40.7的序列次存入哈希表中: ```python H = HashTable(11) H.put(12, 'Twelve') H.put(9, 'Nine') H.put(15, 'Fifteen') H.put(10, 'Ten') H.put(8, 'Eight') H.put(21, 'Twenty-One') H.put(3, 'Three') H.put(29, 'Twenty-Nine') H.put(30, 'Thirty') H.put(11, 'Eleven') H.put(40, 'Forty-Seven') ``` 最后,我们可以使用哈希表查找的算法查找关键字为15的数据元素: ```python print(H.get(15)) ``` 运行结果为: ``` Fifteen ``` 说明我们成功地在哈希表中找到了关键字为15的数据元素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值