c++哈希(哈希表闭散列线性探测实现)

0. 前言

  • 闭散列:也叫开放定址法,当发生哈希冲突时,如果哈希表未被装满,说明在哈希表中必然还有空位置,那么可以把key存放到冲突位置中的“下一个” 空位置中去。那如何寻找下一个空位置呢?

1. 线性探测

  • 线性探测:从发生冲突的位置开始,依次向后探测,直到寻找到下一个空位置为止。
  • 插入
    • 通过哈希函数获取待插入元素在哈希表中的位置。
    • 如果该位置中没有元素则直接插入新元素,如果该位置中有元素发生哈希冲突,使用线性探测找到下一个空位置,插入新元素。
  • 删除
    采用闭散列处理哈希冲突时,不能随便物理删除哈希表中已有的元素,若直接删除元素会影响其他元素的搜索。比如删除元素4,如果直接删除掉,44查找起来可能会受影响。因此线性探测采用标记的伪删除法来删除一个元素。
    我们需要借助枚举函数来完成
	enum State
	{
		EMPTY,	//空标记
		EXIST,	//存在
		DELETE	//删除
	};

2. 线性探测的代码实现

2.0 定义

	enum State
	{
		EMPTY,	//空标记
		EXIST,	//存在
		DELETE	//删除
	};

	template<class K, class V>
	struct HashDate
	{
		pair<K, V> _kv;
		State _state = EMPTY;

	};


	template<class K, class V>
	class HashTable
	{
	public:
		// 插入
		bool Insert(const pair<K, V>& kv);
 
		// 查找
		HashDate<K, V>* Find(const K& key);
 
		// 删除
		bool Erase(const K& key);
 
		size_t Size()const
		{
			return _size;
		}
 
		bool Empty() const
		{
			return _size == 0;
		}
	private:
		vector<Node*> _tables;
		size_t _size = 0;		// 存储有效数据个数
	};

2.1 插入实现–Insert

思路::hash(key) = key % capacity;我们根据取模来确定映射的相对位置。
注意:扩容的方法我们采用负载因子的方法
在这里插入图片描述
只要负载因子到0.7,我们就扩容;扩容后我们又要重新线性探索太麻烦了,我们可以复用一下Insert;把线性探索任务交给在栈上开辟的类实例化的新对象去作插入重新映射;然后再进行交换即可。具体看下面代码:

	bool Insert(const pair<K, V>& kv)
		{
			if (Find(kv.first))
			{
				return false;
			}

			//负载因子到了就扩容
			if (_tables.size() == 0 || 10 * _size / _tables.size() >= 7)  //扩容
			{
				size_t newSize = _tables.size() == 0 ? 10 : _tables.size() * 2;
				HashTable<K, V> newHT;
				newHT._tables.resize(newSize);
				//旧表数据映射到新表内
				for (auto e : _tables)
				{
					if (e._state == EXIST)
					{
						newHT.Insert(e._kv);
					}
				}

				_tables.swap(newHT._tables);
			}

			size_t hashi = kv.first % _tables.size();
			//线性探测
			while (_tables[hashi]._state == EXIST)
			{
				++hashi;
				hashi %= _tables.size();
			}

			//找到位置了
			_tables[hashi]._kv = kv;
			_tables[hashi]._state = EXIST;
			++_size;

			return true;
		}

2.2 查找实现–Find

根据负载因子扩容;我们知道一定有一部分的位置是空也就是被标记为EMPTY。
思路:根据映射关系取查找值的模,找到相对位置;然后往后一次查找知道遇到EMPTY标记的位置。

// 查找
		HashDate<K, V>* Find(const K& key)
		{
			if (_tables.size()==0)
			{
				return nullptr;
			}

			size_t hashi =key % _tables.size();
			while (_tables[hashi]._state != EMPTY)
			{
				if (_tables[hashi]._state != DELETE && key == _tables[hashi]._kv.first)
				{
					return  &_tables[hashi];
				}
				++hashi;
				hashi %= _tables.size();
			}

			//没找到
			return nullptr;
		}

2.3 删除实现–Erase

我们这里的删除是标记型删除。

// 删除
		bool Erase(const K& key)
		{
			HashDate<K, V>* ret = Find(key);
			if (ret)
			{
				ret->_state = DELETE;
				--_size;
				return true;
			}
			else
			{
				return false;
			}

		}

2.4 仿函数

  • 我们发现我们现在实现的哈希只能存数字,字符串等不行;这个时候我们需要借助仿函数。
	template<class k>
	struct HashFunc
	{
		size_t operator()(const k& key)
		{
			return (size_t)key;
		}
	};
	//特化--string
	template<>
	struct HashFunc<string>
	{
		size_t operator()(const string& s)
		{
			size_t val = 0;
			for (const auto ch : s)	//迭代器
			{
				val *= 131;
				val += ch;
			}

			return val;
		}
	};

可能大家都很疑惑,string所有字母ASCII值相加理解用来比较;为什么乘于131,大佬经过反复推敲保证它们值稳定,就是下次映射的时候还是原来的数值。
在这里插入图片描述

3. 完整代码实现

#pragma once

namespace CloseHash
{
	enum State
	{
		EMPTY,	//空标记
		EXIST,	//存在
		DELETE	//删除
	};

	template<class K, class V>
	struct HashDate
	{
		pair<K, V> _kv;
		State _state = EMPTY;

	};

	template<class k>
	struct HashFunc
	{
		size_t operator()(const k& key)
		{
			return (size_t)key;
		}
	};
	//特化--string
	template<>
	struct HashFunc<string>
	{
		size_t operator()(const string& s)
		{
			size_t val = 0;
			for (const auto ch : s)	//迭代器
			{
				val *= 131;
				val += ch;
			}

			return val;
		}
	};

	template<class K, class V, class Hash = HashFunc<K>>
	class HashTable
	{
	public:
		bool Insert(const pair<K, V>& kv)
		{
			if (Find(kv.first))
			{
				return false;
			}

			//负载因子到了就扩容
			if (_tables.size() == 0 || 10 * _size / _tables.size() >= 7)  //扩容
			{
				size_t newSize = _tables.size() == 0 ? 10 : _tables.size() * 2;
				HashTable<K, V, Hash> newHT;
				newHT._tables.resize(newSize);
				//旧表数据映射到新表内
				for (auto e : _tables)
				{
					if (e._state == EXIST)
					{
						newHT.Insert(e._kv);
					}
				}

				_tables.swap(newHT._tables);
			}

			Hash hash;
			size_t hashi = hash(kv.first) % _tables.size();
			//线性探测
			while (_tables[hashi]._state == EXIST)
			{
				++hashi;
				hashi %= _tables.size();
			}

			//找到位置了
			_tables[hashi]._kv = kv;
			_tables[hashi]._state = EXIST;
			++_size;

			return true;
		}

		// 查找
		HashDate<K, V>* Find(const K& key)
		{
			if (Empty())
			{
				return nullptr;
			}

			Hash hash;
			size_t hashi = hash(key) % _tables.size();
			while (_tables[hashi]._state != EMPTY)
			{
				if (_tables[hashi]._state != DELETE && key == _tables[hashi]._kv.first)
				{
					return  &_tables[hashi];
				}
				++hashi;
				hashi %= _tables.size();
			}

			//没找到
			return nullptr;
		}

		// 删除
		bool Erase(const K& key)
		{
			HashDate<K, V>* ret = Find(key);
			if (ret)
			{
				ret->_state = DELETE;
				--_size;
				return true;
			}
			else
			{
				return false;
			}

		}

		bool Empty() const
		{
			return _size == 0;
		}

		size_t Size()const
		{
			return _size;
		}

		//打印一下,用来测试
		void Print()
		{
			for (size_t i = 0; i < _tables.size(); ++i)
			{
				if (_tables[i]._state == EXIST)
				{
					printf("[%d:%d] ", i, _tables[i]._kv.first);
				}
				else
				{
					printf("[%d:*] ", i);
				}
			}
			cout << endl;
		}

	private:
		vector<HashDate<K, V>> _tables;
		size_t _size = 0;	//存储多少个有效数据
	};


	void TestHT1()
	{
		//int a[] = { 1, 11, 4, 15, 26, 7, 44, 9 };
		int a[] = { 1, 11, 4, 15, 26, 7, 44 };
		HashTable<int, int> ht;
		for (auto e : a)
		{
			ht.Insert(make_pair(e, e));
		}

		ht.Print();

		ht.Erase(4);
		cout << ht.Find(44)->_kv.first << endl;
		cout << ht.Find(4) << endl;
		ht.Print();

		ht.Insert(make_pair(-2, -2));
		ht.Print();

		cout << ht.Find(-2)->_kv.first << endl;
	}

	void TestHT2()
	{
		string arr[] = { "苹果", "西瓜", "苹果", "西瓜", "苹果", "苹果", "西瓜", "苹果", "香蕉", "苹果", "香蕉" };

		//HashTable<string, int, HashFuncString> countHT;
		HashTable<string, int> countHT;
		for (auto& str : arr)
		{
			auto ptr = countHT.Find(str);
			if (ptr)
			{
				ptr->_kv.second++;
			}
			else
			{
				countHT.Insert(make_pair(str, 1));
			}
		}

		for (auto& str : arr)
		{
			cout << str << ":" << countHT.Find(str)->_kv.second << "---";
		}
		cout << endl;
	}

	void TestHT3()
	{
		HashFunc<string> hash;
		cout << hash("abcd") << endl;
		cout << hash("bcad") << endl;
		cout << hash("eat") << endl;
		cout << hash("ate") << endl;
		cout << hash("abcd") << endl;
		cout << hash("aadd") << endl << endl;

		cout << hash("abcd") << endl;
		cout << hash("bcad") << endl;
		cout << hash("eat") << endl;
		cout << hash("ate") << endl;
		cout << hash("abcd") << endl;
		cout << hash("aadd") << endl << endl;
	}
}

4. 代码测试并运行结果:

在这里插入图片描述

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
线性探测法是哈希表中的一种解决哈希冲突的方法。当哈希函数将两个不同的键映射到了同一个位置时,就会发生哈希冲突。线性探测法就是在这种情况下,顺序地检查下一个位置,直到找到一个空位置或者找遍整个哈希表为止。 以下是使用线性探测法创建人名哈希表的代码示例: ```c++ #include <iostream> #include <string> using namespace std; const int TABLE_SIZE = 10; class Person { public: string name; int age; Person(string n, int a) { name = n; age = a; } }; class HashTable { private: Person* table[TABLE_SIZE]; public: HashTable() { for (int i = 0; i < TABLE_SIZE; i++) { table[i] = nullptr; } } int hash(string key) { int hashVal = 0; for (int i = 0; i < key.length(); i++) { hashVal += key[i]; } return hashVal % TABLE_SIZE; } void insert(Person* p) { int index = hash(p->name); while (table[index] != nullptr) { index++; index %= TABLE_SIZE; } table[index] = p; } Person* search(string name) { int index = hash(name); while (table[index] != nullptr && table[index]->name != name) { index++; index %= TABLE_SIZE; } if (table[index] == nullptr) { return nullptr; } else { return table[index]; } } void remove(string name) { int index = hash(name); while (table[index] != nullptr && table[index]->name != name) { index++; index %= TABLE_SIZE; } if (table[index] != nullptr) { delete table[index]; table[index] = nullptr; } } void printTable() { for (int i = 0; i < TABLE_SIZE; i++) { if (table[i] != nullptr) { cout << "Index " << i << ": " << table[i]->name << endl; } else { cout << "Index " << i << ": Empty" << endl; } } } }; int main() { HashTable h; Person* p1 = new Person("Alice", 25); Person* p2 = new Person("Bob", 30); Person* p3 = new Person("Charlie", 35); Person* p4 = new Person("David", 40); h.insert(p1); h.insert(p2); h.insert(p3); h.insert(p4); h.printTable(); Person* p = h.search("Bob"); if (p != nullptr) { cout << "Bob is " << p->age << " years old." << endl; } else { cout << "Bob not found." << endl; } h.remove("Bob"); h.printTable(); return 0; } ```
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

昨天;明天。今天。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值