哈希表(hashtable)

本文介绍了哈希表的基本概念,包括构造哈希表的方法、常见的哈希冲突解决策略,并提供了一个使用线性探测法处理冲突的哈希表C++实现示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值